Skip to content

Commit

Permalink
feat: support classes for HTTP management
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa committed Jan 4, 2024
1 parent 4d4d748 commit 4f34409
Show file tree
Hide file tree
Showing 12 changed files with 828 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.mockwebserver.http;

import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
* Compatibility layer for OkHttp.
*/
public class Buffer {

private final ByteArrayOutputStream byteStream;

public Buffer() {
this(new byte[0]);
}

public Buffer(byte[] bytes) {
byteStream = new ByteArrayOutputStream();
if (bytes != null) {
byteStream.write(bytes, 0, bytes.length);
}
}

public final byte[] getBytes() {
return byteStream.toByteArray();
}

public final long size() {
return byteStream.size();
}

public final String readUtf8() {
return new String(getBytes(), StandardCharsets.UTF_8);
}

public final Buffer write(byte[] bytes) {
return write(bytes, 0, bytes.length);
}

public Buffer write(byte[] bytes, int off, int len) {
byteStream.write(bytes, off, len);
return this;
}

public final Buffer writeString(String string, Charset charset) {
return write(string.getBytes(charset));
}

public final Buffer writeUtf8(String string) {
return writeString(string, StandardCharsets.UTF_8);
}

public final void flush() {
// NO-OP
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.mockwebserver.http;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
* Compatibility layer for OkHttp.
*/
public class ByteString {

private final byte[] data;

private ByteString(byte[] data) {
this.data = data;
}

public static ByteString of(byte... data) {
Objects.requireNonNull(data, "data === null");
return new ByteString(data.clone());
}

public static ByteString encodeUtf8(String s) {
Objects.requireNonNull(s, "s === null");
return new ByteString(s.getBytes(StandardCharsets.UTF_8));
}

public final String utf8() {
return new String(data, StandardCharsets.UTF_8);
}

public final byte[] toByteArray() {
return data.clone();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.mockwebserver.http;

public abstract class Dispatcher {

public abstract MockResponse dispatch(RecordedRequest request);

public void shutdown() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.mockwebserver.http;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Headers are case-insensitive for HTTP/1.1 and must be lower-cased for HTTP/2.
*/
public class Headers {

private final Map<String, List<String>> headerMap;

Headers(Map<String, List<String>> headerMap) {
this.headerMap = headerMap;
}

public final List<String> headers(String key) {
return Collections.unmodifiableList(headerMap.getOrDefault(keySanitize(key), Collections.emptyList()));
}

public final String header(String key) {
final List<String> values = headers(key);
if (values.size() == 1) {
return values.get(0);
}
if (values.isEmpty()) {
return null;
}
return String.join(",", values);
}

public final String get(String name) {
return header(name);
}

/**
* Compatibility layer for OkHttp
*
* @return a Map of the headers with lower-cased keys
*/
public final Map<String, List<String>> toMultimap() {
return headerMap.entrySet().stream()
.collect(Collectors.toMap(e -> keySanitize(e.getKey()), Map.Entry::getValue, (old, neu) -> neu));
}

private static String keySanitize(String header) {
return header.trim().toLowerCase(Locale.ROOT);
}

public final Builder newBuilder() {
return new Builder(new LinkedHashMap<>(headerMap));
}

public static Builder builder() {
return new Builder();
}

public static final class Builder {
private final Headers mockHttpHeaders;

public Builder() {
this(new LinkedHashMap<>());
}

private Builder(Map<String, List<String>> headers) {
this.mockHttpHeaders = new Headers(headers);
}

public Headers build() {
return mockHttpHeaders;
}

public Builder add(String key, String value) {
mockHttpHeaders.headerMap.compute(keySanitize(key), (k, values) -> {
if (values == null) {
values = new ArrayList<>();
}
values.add(value);
return values;
});
return this;
}

public Builder add(String header) {
final int index = header.indexOf(":");
if (index == -1) {
throw new IllegalArgumentException("Unexpected header: " + header);
}
return add(header.substring(0, index), header.substring(index + 1));
}

public Builder addAll(Iterable<Map.Entry<String, String>> headers) {
for (Map.Entry<String, String> header : headers) {
add(header.getKey(), header.getValue());
}
return this;
}

public Builder removeAll(String key) {
mockHttpHeaders.headerMap.remove(key);
return this;
}

public Builder set(String name, String value) {
return setHeader(name, value);
}

public Builder setHeader(String key, String value) {
final List<String> values = new ArrayList<>();
values.add(value);
mockHttpHeaders.headerMap.put(keySanitize(key), values);
return this;
}

public Builder setHeaders(Headers headers) {
this.mockHttpHeaders.headerMap.clear();
this.mockHttpHeaders.headerMap.putAll(headers.toMultimap());
return this;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.mockwebserver.http;

import io.netty.handler.codec.http.QueryStringDecoder;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
* Compatibility layer for OkHttp.
*/
public class HttpUrl {

private final URI uri;

public HttpUrl(URI uri) {
this.uri = uri;
}

public final URI uri() {
return uri;
}

public final String encodedPath() {
return uri().getRawPath();
}

public final String queryParameter(String name) {
return new QueryStringDecoder(uri()).parameters().get(name).get(0);
}

@Override
public final String toString() {
return uri().toString();
}

public static HttpUrl fromUrl(URL url) {
try {
return new HttpUrl(url.toURI());
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid URL: " + url, e);
}
}

public static HttpUrl parse(String url) {
try {
return fromUrl(new URL(url));
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Invalid URL: " + url, e);
}
}

}
Loading

0 comments on commit 4f34409

Please sign in to comment.