Skip to content

Completely fake out the network for simple tests - #9636

Merged
swankjesse merged 4 commits into
mainfrom
jwilson.0806.sockets
Aug 7, 2026
Merged

Completely fake out the network for simple tests#9636
swankjesse merged 4 commits into
mainfrom
jwilson.0806.sockets

Conversation

@swankjesse

Copy link
Copy Markdown
Collaborator

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.

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.
@swankjesse
swankjesse requested a review from yschimke August 6, 2026 23:45
* limitations under the License.
*/
package okhttp3
package okhttp3.sockets

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

finally!

* A complete in-memory socket system, suitable for testing OkHttp without using any operating
* system TCP sockets.
*/
class FakeNetwork {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just having PTSD flashbacks to my falled mocksocket attempt :)

#9338

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This was a bit less ambitious?


val timeout = Timeout()
.deadline(connectTimeoutMillis, TimeUnit.MILLISECONDS)
boundServer.enqueue(timeout, attempt)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I like this.

Comment thread okhttp-testing-support/src/main/kotlin/okhttp3/sockets/FakeNetwork.kt Outdated
import org.junit.jupiter.api.extension.RegisterExtension

open class FakeNetworkOkHttpTest {
private val network = FakeNetwork()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nice demo

@swankjesse
swankjesse merged commit 95e097c into main Aug 7, 2026
24 of 26 checks passed
@swankjesse
swankjesse deleted the jwilson.0806.sockets branch August 7, 2026 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants