* Completely fake out the network for simple tests
This covers the happy path of OkHttp + MockWebServer with no
TLS.
There's nothing too exciting here, other than using Okio's
inMemorySocketPair instead of a proper TCP socket for data.
In a follow-up PR I intend to layer on TLS including all the
exotic options like ECH and handshake failures. I won't do
any actual cryptography, just like how this doesn't do any
actual TCP packets.
* Update okhttp-testing-support/src/main/kotlin/okhttp3/sockets/FakeNetwork.kt
Co-authored-by: Yuri Schimke <yuri@schimke.ee>
* Spotless
* Track some API changes in Android tests
---------
Co-authored-by: Yuri Schimke <yuri@schimke.ee>
* New BufferedSocket class
I'm starting to push Okio's socket throughout OkHttp,
and it turns out what I usually want is a BufferedSocket
instead.
* Flush not emit
* Track exception messages
* Use flush more
* Use okio.Socket internally in OkHttp connections
* Spotless
* Fix connection close timing
* Accept 'Socket is closed' or 'Socket closed'
* Another 'Socket is closed' message difference
* Rollback attempt to defer buffering source and sink
With H2_PRIOR_KNOWLEDGE the server can send settings
before we've sent anything.
---------
Co-authored-by: Jesse Wilson <jwilson@squareup.com>
* New MockWebServerSocket helper class
This is work towards switching to use okio.Socket
as the implementation of web sockets.
* Fix RecordedRequestTest
---------
Co-authored-by: Jesse Wilson <jwilson@squareup.com>
* Give the CountDownLatch something to count
* Fix typo
* Connection Upgrade for HTTP1 status code 101
See https://datatracker.ietf.org/doc/html/rfc9110#name-101-switching-protocols
* use socket.asOkioSocket()
* add socket to api
* remove the specific check on tcp connection upgrades
* create a dedicated HttpUpgradesTest
* chore
* add more test cases
* spotlessApply
* Use more MockWebServer features in tests
* Forbid HTTP/2
* Hook into Exchange for connection pool management
* Test events on successful upgrade
* Release the request body after an upgrade fails
* Upgrades don't promise a response body
* SpotlessApply
---------
Co-authored-by: Jesse Wilson <jwilson@squareup.com>
* Change RecordedRequest.chunkSizes to be nullable
Now that it's all Kotlin we can usefully distinguish
between null (not chunked) and empty list (chunked with
zero bytes).
Also remove the experimental annotation from RecordedRequest
and delete the experimental annotation.
* apiDump
---------
Co-authored-by: Jesse Wilson <jwilson@squareup.com>
* Replace SocketPolicy with a new type, SocketEffect
SocketPolicy encapsulated two things:
- a trigger (request start, response body, etc)
- an effect (close the socket, close the stream, stall, etc.)
It also had some special cases for effects like
failing the TLS handshake.
With this PR there's a new class, SocketEffect, that determines
what to do. It's assigned to one of 5 fields for different
triggers.
# Conflicts:
# mockwebserver/src/main/kotlin/mockwebserver3/MockWebServer.kt
* apiDump
* Track API change
* Track more API changes
* Spotless
---------
Co-authored-by: Jesse Wilson <jwilson@squareup.com>
Also rename 'sequenceNumber' to 'exchangeIndex'.
This may be useful to test features like connection
reuse.
Co-authored-by: Jesse Wilson <jwilson@squareup.com>
* Permit RecordedRequest.body to be null or empty
Null if we didn't capture a body, and empty if we
captured an empty body.
* Track API change
* Fixup more tests
* spotless
---------
Co-authored-by: Jesse Wilson <jwilson@squareup.com>
* New @StartStop extension for JUnit 5
This is a new take on our JUnit 5 extension. Rather than creating and managing
the MockWebServer instance in the extension, we let the user create it and all
the extension does is manage the lifecycle.
Note that this annotation doesn't require any external configuration - it doesn't
need a system-property to opt-into 'automatic' extensions, and it doesn't require
a class-level extension either.
* apiDump
* Switch to @StartStop for JUnit 5 tests
* Fixup visibility
---------
Co-authored-by: Jesse Wilson <jwilson@squareup.com>
* Change some MockWebServer ergonomics
Now calling MockWebServer.port and similar functions will
not implicity start the server. It is instead necessary to
explicitly call the start() function.
The problem with the old behavior was that reading the
port field could fail with an IOException, which is weird
behavior for reading a property.
* Fixup test
* Fix another test
---------
Co-authored-by: Jesse Wilson <jwilson@squareup.com>
* Decompose the request line into individual properties
Replace the 'path' property with the URL's encodedPath
property.
Rename the 'requestUrl' property to 'url'.
* Spotless
* Fix up DnsOverHttpsTest
* Fix some test failures
* Fix a liveness bug in MockWebServer
It was possible to configure delays that would be honored
after MockWebServer was shut down. With this fix any
delay will be abandoned once the server is closed.
* Update mockwebserver/src/main/kotlin/mockwebserver3/internal/SleepNanos.kt
Co-authored-by: Jake Wharton <jw@squareup.com>
---------
Co-authored-by: Jesse Wilson <jwilson@squareup.com>
Co-authored-by: Jake Wharton <jw@squareup.com>
Also remove @ExperimentalOkHttpApi in a few places.
Also rename 'shutdown' to 'close' in MockWebServer
and Dispatcher.
Also rename QueueDispatcher.enqueueResponse() to
QueueDispatcher.enqueue.
Co-authored-by: Jesse Wilson <jwilson@squareup.com>
* Change MockWebServer to use Okio's new Socket interface
This replaces the Stream interface introduced in a recent
alpha release.
* apiDump
* Only buffer once
---------
Co-authored-by: Jesse Wilson <jwilson@squareup.com>
* Finalize the new API for MockResponse
It's a value object with a bunch of fields, and a builder.
* Tunnel then body
---------
Co-authored-by: Jesse Wilson <jwilson@squareup.com>
* TrailersSource, a new public API for HTTP trailers
This doesn't change much.
The new interface replaces a Kotlin lambda that had
similar behavior, though that lambda wasn't specified
as thoroughly.
We now permit calls to Response.trailers() before the
entire response body is consumed. But when such calls are
made the response body is discarded first. This could be
a bit of a surprise to users, especially users who do an
additional layer of buffering over their ResponseBody
because it could yield latent failures.
But I think it's better behavior overall, especially since
it's possible data binding layers might not consume the
entire response body in all cases. For example, a JSON
library might not read to the very end of the response
if it has received a terminating '}' byte.
The riskiest part of this change is when ResponseBodySource
self-reports as complete. Previously it would self-report
as complete when it returned as many bytes as promised
in the Content-Length. With this change it will now only
report itself complete when it receives a definitive EOF
on the stream, signaled by a -1 byte count on a read. This
is because only when the EOF is received can we be sure
that the trailers are received, and we must not unhook
the Exchange from the Call until that happens. The previous
behavior could make Call.cancel() no-op even if we were
blocked waiting on trailers.
* apiDump
* Attempt to get more tests passing
* Don't wait for stream EOF to release the stream
One of our tests caught a situation where we were waiting
for the caller to read the EOF when we didn't need to.
---------
Co-authored-by: Jesse Wilson <jwilson@squareup.com>
OkHttp has lots of constants defined from before it was a
Kotlin library, and I'd rather the library be self-consistent
than consistent with Kotlin idioms.
As a bonus we get to remain consistent with Java idioms.
I'd love a Kotlin mechanism to use @JvmField and @JvmName
at the same time, but I can't figure out how.
Co-authored-by: Jesse Wilson <jwilson@squareup.com>
* Update org.bouncycastle to v1.79
* Handle UnsupportedOperationException – if the underlying provider does not implement the operation
* Handle BC requirements on the key manager
---------
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Yuri Schimke <yuri@schimke.ee>
* Mechanically tag all new APIs as @ExperimentalOkHttpApi
In a follow-up PR I intend to remove the annotation on any
API that we're willing to commit to for OkHttp 5.0.0.
In a follow-up PR I'd also like to hide all remaining
APIs from Java langauge callers.
* Opt-in ExperimentalOkHttpApi for Android projects too
---------
Co-authored-by: Benoit Quenaudon <benoit@quenaudon.com>