1
0
mirror of https://github.com/square/okhttp.git synced 2025-08-08 23:42:08 +03:00

21 Commits

Author SHA1 Message Date
Jesse Wilson
f745695087 Replace SocketPolicy with a new type, SocketEffect (#8870)
* 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>
2025-06-20 07:46:46 -04:00
Jesse Wilson
36ed3452fe Add a connectionIndex field to RecordedRequest (#8866)
Also rename 'sequenceNumber' to 'exchangeIndex'.
This may be useful to test features like connection
reuse.

Co-authored-by: Jesse Wilson <jwilson@squareup.com>
2025-06-19 07:44:16 -04:00
Jesse Wilson
b9a1a5dd80 Change some MockWebServer ergonomics (#8861)
* 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>
2025-06-17 20:19:42 -04:00
Jesse Wilson
dfd1dffb3c Decompose the request line into individual properties (#8860)
* 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
2025-06-16 20:47:36 -04:00
Jesse Wilson
b4181838a3 Change RecordedRequest.body to a ByteString (#8856)
* Change RecordedRequest.body to a ByteString

Immutable is good.

* apiDump

---------

Co-authored-by: Jesse Wilson <jwilson@squareup.com>
2025-06-14 14:13:40 -04:00
Jesse Wilson
6b134f769a Enable explicit API mode in mockwebserver3. (#8854)
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>
2025-06-14 12:09:26 -04:00
Jesse Wilson
817b7a89d4 Change MockWebServer to use Okio's new Socket interface (#8853)
* 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>
2025-06-13 19:09:36 -06:00
Jesse Wilson
5eecd519aa Finalize the new API for MockResponse (#8837)
* 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>
2025-06-03 13:10:21 -04:00
Jesse Wilson
6de88ec621 TrailersSource, a new public API for HTTP trailers (#8829)
* 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>
2025-06-02 10:42:05 -04:00
Jesse Wilson
4d1f8add89 Confirm we send SNI server names on the JDK9 Platform (#7597)
This introduces a new API in MockWebServer's RecordedRequest to
capture the inbound server names.

Co-authored-by: Yuri Schimke <yuri@schimke.ee>
2023-01-04 07:59:45 +10:00
Jesse Wilson
ebb6e92d93 Use classes where appropriate in SocketPolicy (#7622)
* Use classes where appropriate in SocketPolicy

* Track API changes
2023-01-03 11:37:42 -05:00
Jesse Wilson
3ea6b81e5c Make SocketPolicy a sealed interface (#7610)
* Make SocketPolicy a sealed interface

* Dump APIs
2023-01-02 15:13:13 -05:00
Jesse Wilson
34bb12533b Public API for duplex streams in MockWebServer (#7595)
* Public API for duplex streams in MockWebServer

I'm not 100% on the name 'Stream' for the source+sink pair. It's
tempting to use 'Socket', though I think that's an implementation name
and this is an abstraction that uses a different implementation.

I've chosen Stream specifically 'cause it's the word used in the
HTTP/2 spec. My biggest gripe with it is that it's bidirectional
in the HTTP/2 spec, but Java InputStream and OutputStream are not
bidirectional.

* Dump APIs for streams

* Don't include a Content-Length header for chunked bodies

* Convert MockWebServerTest to Kotlin (#7596)

* Rename .java to .kt

* Convert Java to Kotlin

* Null isn't special for last-write wins

* Attempt to make NonCompletingRequestBody less flaky
2022-12-31 13:24:52 -05:00
Jesse Wilson
c30d3ce7b7 Create MockResponseBody (#7593)
* Create MockResponseBody

We'd previously only ever used Buffer to model the type
of the MockResponse body. This has proven inadequate, particularly
for features like duplex responses.

Most of the complexity in this PR is changing how throttling
and disconnect-during-body work when we don't necessarily have
a Buffer for the response body.

This is a first step, more capability to follow.

* Update API

* We flush fewer times now

* Attempt to make a test less flaky

* Try harder to make the test less flaky
2022-12-31 14:47:55 +10:00
Jesse Wilson
3ca2e64744 Clean up some Mockwebserver3 internals (#7582) 2022-12-29 06:27:59 +10:00
Jesse Wilson
fe15ccda5b Switch to a Builder for mockwebserver3.MockResponse (#7578)
* Switch to a Builder for mockwebserver3.MockResponse

* Migrate lots of tests to MockResponse.Builder

* Improve some code style

* Follow naming conventions in MockResponse.Builder

* Apply side-effects for inTunnel=true

* Update the API
2022-12-28 10:13:49 -05:00
Yuri Schimke
f27a291975 Add tests for multiple routes (#7563) 2022-12-24 10:26:45 +10:00
Jesse Wilson
afc7fa3700 Delete the unused tunnelProxy argument (#7317)
* Delete the unused tunnelProxy argument

* Update API spec
2022-06-09 13:14:05 -04:00
Jesse Wilson
fd6452596c Implement CONNECT tunnels for HTTP/2 prior knowledge (#7314)
* Implement CONNECT tunnels for HTTP/2 prior knowledge

Closes: https://github.com/square/okhttp/issues/7289

* Update the API spec for MockWebServer

* It is okay for HTTP/2 prior knowledge to send data early

Tests were failing due to an overly-pessimistic 'too much buffering'
exception. I moved the detector to where it's actually needed.
2022-06-08 13:55:16 -04:00
Jesse Wilson
513a5435c0 Change how HTTP 1xx responses work in MockWebServer (#7169)
* Change how HTTP 1xx responses work in MockWebServer

Previously this was a SocketPolicy. With this change each
response may carry 0 or more informational responses which
are transmitted before the ultimate response.

* Add a test for multiple 1xx responses

* Update API
2022-03-19 20:17:50 -07:00
Jesse Wilson
3e16ec28fe Adopt Kotlin's binary compatibility validator (#7112) 2022-02-26 14:17:33 -05:00