From 3428feb1403f4e72701f647f6d019bb1a5f96048 Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Sat, 2 Dec 2023 08:02:12 +0100 Subject: [PATCH] feat: support classes for HTTP management Signed-off-by: Marc Nuri --- .../io/fabric8/mockwebserver/http/Buffer.java | 73 ++++++++ .../mockwebserver/http/Dispatcher.java | 24 +++ .../fabric8/mockwebserver/http/Headers.java | 134 +++++++++++++++ .../fabric8/mockwebserver/http/HttpUrl.java | 69 ++++++++ .../mockwebserver/http/MockResponse.java | 158 ++++++++++++++++++ .../mockwebserver/http/QueueDispatcher.java | 57 +++++++ .../http/RecordedHttpConnection.java | 68 ++++++++ .../mockwebserver/http/RecordedRequest.java | 74 ++++++++ .../fabric8/mockwebserver/http/Response.java | 27 +++ .../fabric8/mockwebserver/http/WebSocket.java | 26 +++ .../mockwebserver/http/WebSocketListener.java | 54 ++++++ 11 files changed, 764 insertions(+) create mode 100644 junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Buffer.java create mode 100644 junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Dispatcher.java create mode 100644 junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Headers.java create mode 100644 junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/HttpUrl.java create mode 100644 junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/MockResponse.java create mode 100644 junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/QueueDispatcher.java create mode 100644 junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/RecordedHttpConnection.java create mode 100644 junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/RecordedRequest.java create mode 100644 junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Response.java create mode 100644 junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/WebSocket.java create mode 100644 junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/WebSocketListener.java diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Buffer.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Buffer.java new file mode 100644 index 00000000000..f63ee6722f2 --- /dev/null +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Buffer.java @@ -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 + } + +} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Dispatcher.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Dispatcher.java new file mode 100644 index 00000000000..14e4b6c28de --- /dev/null +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Dispatcher.java @@ -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() { + } +} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Headers.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Headers.java new file mode 100644 index 00000000000..299fd2e5a30 --- /dev/null +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Headers.java @@ -0,0 +1,134 @@ +/** + * 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> headerMap; + + Headers(Map> headerMap) { + this.headerMap = headerMap; + } + + public final List headers(String key) { + return Collections.unmodifiableList(headerMap.getOrDefault(keySanitize(key), Collections.emptyList())); + } + + public final String header(String key) { + final List values = headers(key); + if (values.size() == 1) { + return values.get(0); + } + if (values.isEmpty()) { + return null; + } + return String.join(",", values); + } + + /** + * Compatibility layer for OkHttp + * + * @return a Map of the headers with lower-cased keys + */ + public final Map> 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> 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> headers) { + for (Map.Entry header : headers) { + add(header.getKey(), header.getValue()); + } + return this; + } + + public Builder removeAll(String key) { + mockHttpHeaders.headerMap.remove(key); + return this; + } + + public Builder setHeader(String key, String value) { + final List 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; + } + + } +} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/HttpUrl.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/HttpUrl.java new file mode 100644 index 00000000000..548b03e574a --- /dev/null +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/HttpUrl.java @@ -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); + } + } + +} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/MockResponse.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/MockResponse.java new file mode 100644 index 00000000000..2b0c478307f --- /dev/null +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/MockResponse.java @@ -0,0 +1,158 @@ +/** + * 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.time.Duration; +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class MockResponse implements Response { + + private Headers.Builder headers; + private int code; + private Buffer body; + private Duration bodyDelay; + private WebSocketListener webSocketListener; + + public MockResponse() { + this.headers = Headers.builder(); + } + + @Override + public int code() { + return code; + } + + @Override + public Headers headers() { + return headers.build(); + } + + @Override + public Buffer getBody() { + return body; + } + + @Override + public WebSocketListener getWebSocketListener() { + return webSocketListener; + } + + public List headers(String key) { + return headers.build().headers(key); + } + + public String header(String key) { + return headers.build().header(key); + } + + public MockResponse setResponseCode(int code) { + this.code = code; + return this; + } + + public MockResponse setBody(Buffer body) { + this.body = body; + return this; + } + + public MockResponse setBody(byte[] body) { + return setBody(new Buffer(body)); + } + + public MockResponse setBody(String body) { + return setBody(new Buffer().writeUtf8(body)); + } + + public MockResponse setChunkedBody(Buffer body, int maxChunkSize) { + removeHeader("Content-Length"); + setHeader("Transfer-encoding", "chunked"); + + final Buffer chunkedBody = new Buffer(); + final byte[] bodyBytes = body.getBytes(); + int offset = 0; + while (offset < bodyBytes.length) { + final int chunkSize = Math.min(bodyBytes.length - offset, maxChunkSize); + chunkedBody.writeUtf8(Integer.toHexString(chunkSize)); + chunkedBody.writeUtf8("\r\n"); + chunkedBody.write(bodyBytes, offset, chunkSize); + chunkedBody.writeUtf8("\r\n"); + offset += chunkSize; + } + chunkedBody.writeUtf8("0\r\n"); // Last chunk. Trailers follow! + + this.body = chunkedBody; + return this; + } + + public MockResponse setChunkedBody(String body, int maxChunkSize) { + return setChunkedBody(new Buffer().writeUtf8(body), maxChunkSize); + } + + public Duration getBodyDelay() { + return bodyDelay; + } + + public MockResponse setBodyDelay(long delay, TimeUnit delayUnit) { + return setBodyDelay(Duration.ofMillis(delayUnit.toMillis(delay))); + } + + public MockResponse setBodyDelay(Duration bodyDelay) { + this.bodyDelay = bodyDelay; + return this; + } + + public MockResponse clearHeaders() { + headers = Headers.builder(); + return this; + } + + public MockResponse addHeader(String header) { + headers.add(header); + return this; + } + + public MockResponse addHeader(String name, Object value) { + headers.add(name, String.valueOf(value)); + return this; + } + + public MockResponse setHeaders(Headers headers) { + this.headers = headers.newBuilder(); + return this; + } + + public MockResponse setHeader(String name, Object value) { + headers.setHeader(name, String.valueOf(value)); + return this; + } + + public MockResponse removeHeader(String name) { + headers.removeAll(name); + return this; + } + + public MockResponse withWebSocketUpgrade(WebSocketListener 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; + } + +} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/QueueDispatcher.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/QueueDispatcher.java new file mode 100644 index 00000000000..19785ddfc10 --- /dev/null +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/QueueDispatcher.java @@ -0,0 +1,57 @@ +/** + * 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.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +/** + * Compatibility layer for OkHttp. + */ +public class QueueDispatcher extends Dispatcher { + + private static final MockResponse DEAD_LETTER = new MockResponse().setResponseCode(503); + private final BlockingQueue responseQueue; + + public QueueDispatcher() { + this.responseQueue = new LinkedBlockingQueue<>(); + } + + @Override + public MockResponse dispatch(RecordedRequest request) { + final MockResponse ret; + try { + ret = responseQueue.take(); + if (ret == DEAD_LETTER) { + // Re-add the dead letter to the queue (server is shutting down) + responseQueue.add(DEAD_LETTER); + } + return ret; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException("Interrupted while waiting for response", e); + } + } + + @Override + public void shutdown() { + responseQueue.add(DEAD_LETTER); + } + + public final void enqueueResponse(MockResponse response) { + responseQueue.add(response); + } +} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/RecordedHttpConnection.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/RecordedHttpConnection.java new file mode 100644 index 00000000000..9a066ae41b5 --- /dev/null +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/RecordedHttpConnection.java @@ -0,0 +1,68 @@ +/** + * 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.vertx.core.net.SocketAddress; + +import java.util.Objects; +import java.util.UUID; + +public class RecordedHttpConnection { + + private final UUID id; + private final SocketAddress remoteAddress; + private final SocketAddress localAddress; + private final boolean ssl; + + public RecordedHttpConnection(SocketAddress remoteAddress, SocketAddress localAddress, boolean ssl) { + this.remoteAddress = remoteAddress; + this.localAddress = localAddress; + this.ssl = ssl; + id = UUID.randomUUID(); + } + + public UUID id() { + return id; + } + + public SocketAddress getRemoteAddress() { + return remoteAddress; + } + + public SocketAddress getLocalAddress() { + return localAddress; + } + + public boolean isSsl() { + return ssl; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + RecordedHttpConnection that = (RecordedHttpConnection) o; + return ssl == that.ssl && Objects.equals(id, that.id) && Objects.equals(remoteAddress, that.remoteAddress) + && Objects.equals(localAddress, that.localAddress); + } + + @Override + public int hashCode() { + return Objects.hash(id, remoteAddress, localAddress, ssl); + } +} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/RecordedRequest.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/RecordedRequest.java new file mode 100644 index 00000000000..8896e115418 --- /dev/null +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/RecordedRequest.java @@ -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.fabric8.mockwebserver.dsl.HttpMethod; + +import java.util.UUID; + +public class RecordedRequest { + + private final UUID id; + private final HttpMethod method; + private final String path; + private final Headers headers; + private final Buffer body; + + public RecordedRequest(HttpMethod method, String path, Headers headers, Buffer body) { + this.id = UUID.randomUUID(); + this.method = method; + this.path = path; + this.headers = headers; + this.body = body; + } + + public UUID id() { + return id; + } + + public String getPath() { + return path; + } + + /** + * Compatibility layer for OkHttp + * + * @return a String with the HTTP method. + */ + public String getMethod() { + return method.toString(); + } + + public HttpMethod method() { + return method; + } + + public Buffer getBody() { + return body; + } + + public String getUtf8Body() { + return getBody() != null ? getBody().readUtf8() : null; + } + + public Headers getHeaders() { + return headers; + } + + public String getHeader(String key) { + return headers.header(key); + } +} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Response.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Response.java new file mode 100644 index 00000000000..a0ff5796435 --- /dev/null +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/Response.java @@ -0,0 +1,27 @@ +/** + * 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 Response { + + int code(); + + Headers headers(); + + Buffer getBody(); + + WebSocketListener getWebSocketListener(); +} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/WebSocket.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/WebSocket.java new file mode 100644 index 00000000000..41c0f6b00db --- /dev/null +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/WebSocket.java @@ -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 WebSocket { + RecordedRequest request(); + + boolean send(String text); + + boolean send(byte[] bytes); + + boolean close(int code, String reason); +} diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/WebSocketListener.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/WebSocketListener.java new file mode 100644 index 00000000000..ae9afb7551e --- /dev/null +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/http/WebSocketListener.java @@ -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 abstract class WebSocketListener { + + /** + * Invoked when a web socket has been accepted by the remote peer and may begin transmitting + * messages. + */ + public void onOpen(WebSocket webSocket, Response response) { + } + + public void onMessage(WebSocket webSocket, String text) { + } + + public void onMessage(WebSocket webSocket, byte[] bytes) { + } + + /** + * Invoked when the remote peer has indicated that no more incoming messages will be + * transmitted. + */ + public void onClosing(WebSocket 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. + */ + public void onClosed(WebSocket 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. + */ + public void onFailure(WebSocket webSocket, Throwable error, Response response) { + } +}