fabric8io / mockwebserver

An extension of okhttp's mockwebserver, that provides a DSL and is easier to use
Apache License 2.0
113 stars 38 forks source link

Support supplying headers in ResponseProvider #13

Open bowenwr opened 7 years ago

bowenwr commented 7 years ago

I have a Tyrus based Java websocket client. When attempting to make a connection like:

server.expect().get().withPath("/websocket").andUpgradeToWebSocket().open())

The client throws a handshake exception:

INFO: MockWebServer[57060] received request: GET /websocket HTTP/1.1 and responded: HTTP/1.1 404 Client Error
Apr 17, 2017 5:15:52 PM okhttp3.mockwebserver.MockWebServer$2 acceptConnections
INFO: MockWebServer[57060] done accepting connections: Socket closed

javax.websocket.DeploymentException: Handshake error.

When I attempt to mock out the HTTP side with a 101 response like:

server.expect().withPath("/websocket").andReturn(101, "").always();

It bombs because of a null upgrade header:

Caused by: org.glassfish.tyrus.core.HandshakeException: Invalid Upgrade header value: "null".

I couldn't find any way in ResponseProvider to set headers, only status and body content.

Thanks for a great project!

iocanel commented 6 years ago

Hmmm, the header should already be set.

iocanel commented 6 years ago

Yeah, this is handled by okhttp itself. Anyway, I'll add something on top of that just to make sure. Also, it will allow setting custom headers.