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 Dec 4, 2023
1 parent 56a6c2c commit 47be6c0
Show file tree
Hide file tree
Showing 6 changed files with 443 additions and 0 deletions.
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 MockHttpResponse dispatch(RecordedRequest request);

public void shutdown() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* 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.Map;

public class MockHttpHeaders {

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

MockHttpHeaders(Map<String, List<String>> headers) {
this.headers = headers;
}

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

public final Map<String, List<String>> headers() {
return Collections.unmodifiableMap(headers);
}

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 MockHttpHeadersBuilder newBuilder() {
return new MockHttpHeadersBuilder(new LinkedHashMap<>(headers));
}

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

public static final class MockHttpHeadersBuilder {
private final MockHttpHeaders mockHttpHeaders;

private MockHttpHeadersBuilder() {
this(new LinkedHashMap<>());
}

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

public MockHttpHeaders build() {
return mockHttpHeaders;
}

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

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

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

public MockHttpHeadersBuilder removeAll(String key) {
mockHttpHeaders.headers.remove(key);
return this;
}

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

public MockHttpHeadersBuilder setHeaders(MockHttpHeaders headers) {
this.mockHttpHeaders.headers.clear();
this.mockHttpHeaders.headers.putAll(headers.headers());
return this;
}

}
}
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.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.List;
import java.util.Map;

public class MockHttpResponse {

private MockHttpHeaders.MockHttpHeadersBuilder headers;
private int code;
private byte[] body;
private Duration bodyDelay;
private MockWebSocketListener webSocketListener;

public MockHttpResponse() {
this.headers = MockHttpHeaders.builder();
}

public List<String> headers(String key) {
return headers.build().headers(key);
}

public Map<String, List<String>> headers() {
return headers.build().headers();
}

public String header(String key) {
return headers.build().header(key);
}

public int code() {
return code;
}

public byte[] body() {
return body;
}

public MockHttpResponse setResponseCode(int code) {
this.code = code;
return this;
}

public MockHttpResponse setBody(byte[] body) {
this.body = body;
return this;
}

public MockHttpResponse setChunkedBody(byte[] body, int maxChunkSize) {
removeHeader("Content-Length");
setHeader("Transfer-encoding", "chunked");

try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
int offset = 0;
while (offset < body.length) {
final int chunkSize = Math.min(body.length - offset, maxChunkSize);
baos.write(Integer.toHexString(chunkSize).getBytes(StandardCharsets.UTF_8));
baos.write("\r\n".getBytes(StandardCharsets.UTF_8));
baos.write(body, offset, chunkSize);
baos.write("\r\n".getBytes(StandardCharsets.UTF_8));
offset += chunkSize;
}
baos.write("0\r\n".getBytes(StandardCharsets.UTF_8)); // Last chunk. Trailers follow!

this.body = baos.toByteArray();
return this;
} catch (IOException ex) {
throw new IllegalArgumentException("Invalid chunked body provided", ex);
}
}

public Duration getBodyDelay() {
return bodyDelay;
}

public MockHttpResponse setBodyDelay(Duration bodyDelay) {
this.bodyDelay = bodyDelay;
return this;
}

public MockHttpResponse clearHeaders() {
headers = MockHttpHeaders.builder();
return this;
}

public MockHttpResponse addHeader(String header) {
headers.add(header);
return this;
}

public MockHttpResponse addHeader(String name, Object value) {
headers.add(name, String.valueOf(value));
return this;
}

public MockHttpResponse setHeaders(MockHttpHeaders headers) {
this.headers = headers.newBuilder();
return this;
}

public MockHttpResponse setHeader(String name, Object value) {
headers.setHeader(name, String.valueOf(value));
return this;
}

public MockHttpResponse removeHeader(String name) {
headers.removeAll(name);
return this;
}

public MockHttpResponse withWebSocketUpgrade(MockWebSocketListener listener) {
// TODO: Check if this is necessary with Vert.x
// setStatus("HTTP/1.1 101 Switching Protocols");
// setHeader("Connection", "Upgrade");
// setHeader("Upgrade", "websocket");
body = null;
webSocketListener = listener;
return this;
}

public MockWebSocketListener getWebSocketListener() {
return webSocketListener;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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 interface MockWebSocket {
RecordedRequest request();

boolean send(String text);

boolean send(byte[] bytes);

boolean close(int code, String reason);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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 interface MockWebSocketListener {

/**
* Invoked when a web socket has been accepted by the remote peer and may begin transmitting
* messages.
*/
default void onOpen(MockWebSocket webSocket, MockHttpResponse response) {
}

default void onMessage(MockWebSocket webSocket, String text) {
}

default void onMessage(MockWebSocket webSocket, byte[] bytes) {
}

/**
* Invoked when the remote peer has indicated that no more incoming messages will be
* transmitted.
*/
default void onClosing(MockWebSocket webSocket, int code, String reason) {
}

/**
* Invoked when both peers have indicated that no more messages will be transmitted and the
* connection has been successfully released. No further calls to this listener will be made.
*/
default void onClosed(MockWebSocket webSocket, int code, String reason) {
}

/**
* Invoked when a web socket has been closed due to an error reading from or writing to the
* network. Both outgoing and incoming messages may have been lost. No further calls to this
* listener will be made.
*/
default void onFailure(MockWebSocket webSocket, Throwable error, MockHttpResponse response) {
}
}

0 comments on commit 47be6c0

Please sign in to comment.