Skip to content
This repository was archived by the owner on Sep 25, 2023. It is now read-only.

Commit 664adac

Browse files
Donnerbartmanusa
authored andcommitted
fix: missing sec-websocket-protocol response header in andUpgradeToWebSocket()
1 parent fbd29ff commit 664adac

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/main/java/io/fabric8/mockwebserver/internal/SimpleResponse.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
public class SimpleResponse implements ServerResponse {
3030

31+
private static final String HTTP_HEADER_SEC_WEBSOCKET_PROTOCOL = "sec-websocket-protocol";
32+
3133
private final ResponseProvider<String> bodyProvider;
3234

3335
private final WebSocketSession webSocketSession;
@@ -67,6 +69,14 @@ public MockResponse toMockResponse(RecordedRequest request) {
6769

6870
if (webSocketSession != null) {
6971
mockResponse.withWebSocketUpgrade(webSocketSession);
72+
// see https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism
73+
// see https://github.com/netty/netty/blob/4.1/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java#L366
74+
String requestWebsocketProtocol = request.getHeaders().get(HTTP_HEADER_SEC_WEBSOCKET_PROTOCOL);
75+
if (requestWebsocketProtocol != null
76+
// only add the response header if it's not set, to prevent changing custom response headers
77+
&& mockResponse.getHeaders().get(HTTP_HEADER_SEC_WEBSOCKET_PROTOCOL) == null) {
78+
mockResponse.addHeader(HTTP_HEADER_SEC_WEBSOCKET_PROTOCOL, requestWebsocketProtocol);
79+
}
7080
} else {
7181
mockResponse.setBody(bodyProvider.getBody(request));
7282
}

src/test/groovy/io/fabric8/mockwebserver/DefaultMockServerWebSocketTest.groovy

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package io.fabric8.mockwebserver
1717

1818
import okhttp3.OkHttpClient
1919
import okhttp3.Request
20+
import okhttp3.Response
2021
import okhttp3.WebSocket
2122
import okhttp3.WebSocketListener
2223
import spock.lang.Shared
@@ -119,4 +120,48 @@ class DefaultMockServerWebSocketTest extends Specification {
119120
cleanup:
120121
wss.forEach(ws -> ws.close(1000, "Test finished"))
121122
}
123+
124+
// https://github.com/fabric8io/mockwebserver/issues/77
125+
def "andUpgradeToWebSocket, with request header 'sec-websocket-protocol', should create response with matching header"() {
126+
given:
127+
server.expect()
128+
.withPath("/websocket")
129+
.andUpgradeToWebSocket().open().done().always()
130+
def future = new CompletableFuture()
131+
when:
132+
def ws = client.newWebSocket(new Request.Builder().url(server.url("/websocket")).header("sec-websocket-protocol", "v4.channel.k8s.io").build(), new WebSocketListener() {
133+
@Override
134+
void onOpen(WebSocket webSocket, Response response) {
135+
future.complete(response.header("sec-websocket-protocol"))
136+
}
137+
})
138+
then:
139+
assert future.get(100L, TimeUnit.MILLISECONDS) == "v4.channel.k8s.io"
140+
cleanup:
141+
ws.close(1000, "Test finished")
142+
}
143+
144+
// https://github.com/fabric8io/mockwebserver/issues/77
145+
def "andUpgradeToWebSocket, with request header 'sec-websocket-protocol', should not change existing response header"() {
146+
given:
147+
server.expect()
148+
.withPath("/websocket")
149+
.andUpgradeToWebSocket()
150+
.open()
151+
.done()
152+
.withHeader("sec-websocket-protocol", "v3.channel.k8s.io,v4.channel.k8s.io")
153+
.always()
154+
def future = new CompletableFuture()
155+
when:
156+
def ws = client.newWebSocket(new Request.Builder().url(server.url("/websocket")).header("sec-websocket-protocol", "v4.channel.k8s.io").build(), new WebSocketListener() {
157+
@Override
158+
void onOpen(WebSocket webSocket, Response response) {
159+
future.complete(response.header("sec-websocket-protocol"))
160+
}
161+
})
162+
then:
163+
assert future.get(100L, TimeUnit.MILLISECONDS) == "v3.channel.k8s.io,v4.channel.k8s.io"
164+
cleanup:
165+
ws.close(1000, "Test finished")
166+
}
122167
}

0 commit comments

Comments
 (0)