Skip to content

Commit

Permalink
[4.x] Add test for 103 handling (#8055)
Browse files Browse the repository at this point in the history
* Add test for 103 handling

* Minimise changes

* Simplify name
  • Loading branch information
yschimke authored Oct 17, 2023
1 parent a6e54bf commit ea720d3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class MockResponse : Cloneable {
@set:JvmName("status")
var status: String = ""

internal var informationalResponses: List<MockResponse> = listOf()

private var headersBuilder = Headers.Builder()
private var trailersBuilder = Headers.Builder()

Expand Down Expand Up @@ -347,6 +349,10 @@ class MockResponse : Cloneable {

override fun toString() = status

fun addInformationalResponse(informationalResponse: MockResponse): MockResponse = apply {
informationalResponses += informationalResponse
}

companion object {
private const val CHUNKED_BODY_HEADER = "Transfer-encoding: chunked"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,13 @@ class MockWebServer : ExternalResource(), Closeable {
readBody = true
}

peek.informationalResponses.forEach {
val informationalHeader =
listOf(Header(Header.RESPONSE_STATUS, it.status.replace("HTTP/1.1 ", "")))
stream.writeHeaders(informationalHeader, outFinished = false, flushHeaders = true)
stream.connection.flush()
}

val body = Buffer()
val requestLine = "$method $path HTTP/1.1"
var exception: IOException? = null
Expand Down
37 changes: 37 additions & 0 deletions okhttp/src/test/java/okhttp3/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2812,6 +2812,43 @@ public void cancelWhileRequestHeadersAreSent_HTTP_2() throws Exception {
assertThat(recordedRequest.getBody().readUtf8()).isEqualTo("abc");
}

@Test
public void serverRespondsWithEarlyHintsHttp2() throws Exception {
enableProtocol(Protocol.HTTP_2);
server.enqueue(
new MockResponse()
.addInformationalResponse(
new MockResponse().setResponseCode(103).addHeader("Link", "</style.css>; rel=preload; as=style")
)
);
Request request = new Request.Builder()
.url(server.url("/"))
.post(RequestBody.create("abc", MediaType.get("text/plain")))
.build();
executeSynchronously(request)
.assertFailure(SocketTimeoutException.class);
}

@Test
public void serverRespondsWithEarlyHintsHttp1() throws Exception {
server.enqueue(
new MockResponse()
.addInformationalResponse(
new MockResponse().setResponseCode(103).addHeader("Link", "</style.css>; rel=preload; as=style")
)
);
Request request = new Request.Builder()
.url(server.url("/"))
.post(RequestBody.create("abc", MediaType.get("text/plain")))
.build();
executeSynchronously(request)
.assertSuccessful()
.assertCode(200);
RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getBody().readUtf8()).isEqualTo("abc");
assertThat(recordedRequest.getHeader("Link")).isNull();
}

@Test public void serverRespondsWithUnsolicited100Continue_HTTP2() throws Exception {
enableProtocol(Protocol.HTTP_2);
serverRespondsWithUnsolicited100Continue();
Expand Down

0 comments on commit ea720d3

Please sign in to comment.