-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support classes for HTTP management
Signed-off-by: Marc Nuri <[email protected]>
- Loading branch information
Showing
14 changed files
with
1,046 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Buffer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/ByteString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Dispatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Headers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/HttpUrl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* 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; | ||
import java.util.List; | ||
|
||
/** | ||
* 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) { | ||
final List<String> values = new QueryStringDecoder(uri()).parameters().get(name); | ||
if (values == null || values.isEmpty()) { | ||
return null; | ||
} | ||
return values.iterator().next(); | ||
} | ||
|
||
@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); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.