Class MockHttpTransport
- All Implemented Interfaces:
Closeable,AutoCloseable,HttpTransport
HttpTransport for unit testing.
Register one or more route handlers, or a catch-all handler, and the transport will dispatch
each TransportRequest to the appropriate handler without any real network activity.
Example:
See Also:
- Since:
- 9.2.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classFluent builder forMockHttpTransport.static interfaceA handler that maps an incomingTransportRequestto aTransportResponse. -
Method Summary
Modifier and TypeMethodDescriptionstatic MockHttpTransport.Builderbuilder()Returns a newMockHttpTransport.Builder.voidClears the list of recorded requests.execute(TransportRequest request) Executes the given request and returns the response.Returns an unmodifiable view of all requests received since creation (only available when recording is enabled).static MockHttpTransportCreates a simple mock transport that always returns the given status code and body string.booleanReturnstrueif this transport can honor the SSRF guardrail's connect-time contract for a policy-covered request (TransportRequest.isSsrfGuardActive()): pin-on-connect to a resolved address that has passed the deny-private policy, and Juneau-controlled redirect revalidation (no fallback to the transport's own auto-follow).Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.juneau.rest.client.HttpTransport
close
-
Method Details
-
builder
Returns a newMockHttpTransport.Builder. -
of
Creates a simple mock transport that always returns the given status code and body string.- Parameters:
statusCode- The HTTP status code to return.body- The body string to return (UTF-8 encoded). May benull .- Returns:
- A new instance. Never
null .
-
execute
Description copied from interface:HttpTransportExecutes the given request and returns the response.The caller is responsible for closing the returned
TransportResponseafter consuming the body.- Specified by:
executein interfaceHttpTransport- Parameters:
request- The fully-resolved request. Must not benull .- Returns:
- The response. Never
null . - Throws:
TransportException- If a network-level error occurs.
-
supportsUrlPolicy
Description copied from interface:HttpTransportReturnstrueif this transport can honor the SSRF guardrail's connect-time contract for a policy-covered request (TransportRequest.isSsrfGuardActive()): pin-on-connect to a resolved address that has passed the deny-private policy, and Juneau-controlled redirect revalidation (no fallback to the transport's own auto-follow).The default implementation returns
false(fail closed): an unknown/caller-supplied transport that has not affirmatively opted in is presumed unable to honor the contract, soRestRequest#run()rejects policy-covered requests against it rather than silently connecting without the guardrail. First-party transports that construct and fully control their own underlying HTTP client override this to returntrue.- Specified by:
supportsUrlPolicyin interfaceHttpTransport- Returns:
trueif this transport supports pin-on-connect + redirect revalidation for policy-covered requests.
-
getRecordedRequests
Returns an unmodifiable view of all requests received since creation (only available when recording is enabled).- Returns:
- The recorded requests. Empty if recording is disabled.
-
clearRecordedRequests
Clears the list of recorded requests.
-