Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Add test for 103 handling #8055

Merged
merged 5 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I dislike forEach as an alternative to for(...). It’s needlessly complex to step-debug!

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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Showing we don't handle this well.

cc @swankjesse

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

}

@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