Package net.officefloor.server.http.mock
Class MockHttpServer
- java.lang.Object
-
- net.officefloor.server.http.mock.MockHttpServer
-
- All Implemented Interfaces:
HttpServerImplementation
,HttpServerLocation
- Direct Known Subclasses:
MockWoofServer
public class MockHttpServer extends java.lang.Object implements HttpServerLocation, HttpServerImplementation
MockHttpServer
.- Author:
- Daniel Sagenschneider
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static interface
MockHttpServer.MockHttpRequest
HTTP request details for theMockHttpResponse
.protected static class
MockHttpServer.MockHttpResponseImpl
MockHttpResponse
implementation.static class
MockHttpServer.MockServerHttpConnectionImpl
MockServerHttpConnection
implementation.
-
Field Summary
-
Fields inherited from interface net.officefloor.server.http.HttpServerLocation
PROPERTY_CLUSTER_HOST_NAME, PROPERTY_CLUSTER_HTTP_PORT, PROPERTY_CLUSTER_HTTPS_PORT, PROPERTY_DOMAIN, PROPERTY_HTTP_PORT, PROPERTY_HTTPS_PORT
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
MockHttpServer()
Instantiated via static methods or extending.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
configureHttpServer(HttpServerImplementationContext context)
Configures theHttpServer
.static MockHttpServer
configureMockHttpServer(DeployedOfficeInput input)
Configures theMockHttpServer
to be serviced by theDeployedOfficeInput
.protected static void
configureMockHttpServer(MockHttpServer httpServer, DeployedOfficeInput input)
Enables extending implementations to configure themselves as theMockHttpServer
.java.lang.String
createClientUrl(boolean isSecure, java.lang.String path)
Creates the client URL to call theHttpServer
.protected MockHttpResponse
createMockHttpResponse(MockHttpServer.MockHttpRequest request, java.lang.Throwable failure)
Creates theMockHttpResponse
.protected MockHttpResponse
createMockHttpResponse(MockHttpServer.MockHttpRequest request, HttpVersion version, HttpStatus status, java.util.List<WritableHttpHeader> headers, java.util.List<WritableHttpCookie> cookies, java.io.InputStream entityInputStream)
Creates theMockHttpResponse
.java.lang.String
getClusterHostName()
Obtains the name of the host for the server within the cluster.int
getClusterHttpPort()
The cluster may be behind a load balancer and the server may be running on a different port than expected by the client.int
getClusterHttpsPort()
The cluster may be behind a load balancer and the server may be running on a different port than expected by the client.java.lang.String
getDomain()
Obtains the domain for the server.int
getHttpPort()
Obtains the HTTP port.int
getHttpsPort()
Obtains the HTTPS port.static MockServerHttpConnection
mockConnection()
Convenience method to create theMockServerHttpConnection
for GET / with no headers nor entity.static MockServerHttpConnection
mockConnection(MockHttpRequestBuilder request)
Creates theMockServerHttpConnection
.static MockHttpRequestBuilder
mockRequest()
Creates theMockHttpRequestBuilder
.static MockHttpRequestBuilder
mockRequest(java.lang.String requestUri)
Convenience method to create aMockHttpRequestBuilder
.static MockHttpResponseBuilder
mockResponse()
Creates theMockHttpRequestBuilder
.static WritableHttpCookie
mockResponseCookie(java.lang.String name, java.lang.String value)
Creates a mockHttpResponseCookie
.MockHttpResponse
send(MockHttpRequestBuilder request)
Sends theMockHttpRequestBuilder
and blocks waiting for theMockHttpResponse
.void
send(MockHttpRequestBuilder request, MockHttpRequestCallback callback)
Sends theMockHttpRequestBuilder
.MockHttpResponse
sendFollowRedirect(MockHttpRequestBuilder request)
Sends theMockHttpRequestBuilder
and blocks following the redirect.MockHttpServer
timeout(int timeout)
Specifies the timeout for synchronous send.
-
-
-
Method Detail
-
configureMockHttpServer
public static MockHttpServer configureMockHttpServer(DeployedOfficeInput input) throws java.lang.Exception
Configures theMockHttpServer
to be serviced by theDeployedOfficeInput
.- Parameters:
input
-DeployedOfficeInput
.- Returns:
MockHttpServer
to sendHttpRequest
instances.- Throws:
java.lang.Exception
- If fails to configureMockHttpServer
.
-
configureMockHttpServer
protected static void configureMockHttpServer(MockHttpServer httpServer, DeployedOfficeInput input) throws java.lang.Exception
Enables extending implementations to configure themselves as theMockHttpServer
.- Parameters:
httpServer
-MockHttpServer
.input
-DeployedOfficeInput
.- Throws:
java.lang.Exception
- If fails to configureMockHttpServer
.
-
mockRequest
public static MockHttpRequestBuilder mockRequest()
Creates theMockHttpRequestBuilder
.- Returns:
MockHttpRequestBuilder
.
-
mockRequest
public static MockHttpRequestBuilder mockRequest(java.lang.String requestUri)
Convenience method to create aMockHttpRequestBuilder
.- Parameters:
requestUri
- Request URI.- Returns:
MockHttpRequestBuilder
.
-
mockResponse
public static MockHttpResponseBuilder mockResponse()
Creates theMockHttpRequestBuilder
.- Returns:
MockHttpRequestBuilder
.
-
mockResponseCookie
public static WritableHttpCookie mockResponseCookie(java.lang.String name, java.lang.String value)
Creates a mockHttpResponseCookie
.- Parameters:
name
- Name.value
- value.- Returns:
HttpResponseCookie
.
-
mockConnection
public static MockServerHttpConnection mockConnection()
Convenience method to create theMockServerHttpConnection
for GET / with no headers nor entity.- Returns:
MockServerHttpConnection
.
-
mockConnection
public static MockServerHttpConnection mockConnection(MockHttpRequestBuilder request)
Creates theMockServerHttpConnection
.- Parameters:
request
-MockHttpRequestBuilder
for theHttpRequest
of theMockServerHttpConnection
.- Returns:
MockServerHttpConnection
.
-
timeout
public MockHttpServer timeout(int timeout)
Specifies the timeout for synchronous send.- Parameters:
timeout
- Timeout in milliseconds.- Returns:
this
.
-
send
public void send(MockHttpRequestBuilder request, MockHttpRequestCallback callback)
Sends theMockHttpRequestBuilder
.- Parameters:
request
-MockHttpRequestBuilder
.callback
-MockHttpRequestCallback
to receive theMockHttpResponse
.
-
send
public MockHttpResponse send(MockHttpRequestBuilder request)
Sends theMockHttpRequestBuilder
and blocks waiting for theMockHttpResponse
.- Parameters:
request
-MockHttpRequestBuilder
.- Returns:
MockHttpResponse
.
-
sendFollowRedirect
public MockHttpResponse sendFollowRedirect(MockHttpRequestBuilder request)
Sends theMockHttpRequestBuilder
and blocks following the redirect.- Parameters:
request
-MockHttpRequestBuilder
.- Returns:
- Redirect
MockHttpResponse
.
-
createMockHttpResponse
protected MockHttpResponse createMockHttpResponse(MockHttpServer.MockHttpRequest request, HttpVersion version, HttpStatus status, java.util.List<WritableHttpHeader> headers, java.util.List<WritableHttpCookie> cookies, java.io.InputStream entityInputStream)
Creates theMockHttpResponse
.- Parameters:
request
-MockHttpServer.MockHttpRequest
.version
-HttpVersion
.status
-HttpStatus
.headers
-WritableHttpHeader
instances.cookies
-WritableHttpCookie
instances.entityInputStream
- Entity.- Returns:
MockHttpResponse
.
-
createMockHttpResponse
protected MockHttpResponse createMockHttpResponse(MockHttpServer.MockHttpRequest request, java.lang.Throwable failure)
Creates theMockHttpResponse
.- Parameters:
request
-MockHttpServer.MockHttpRequest
.failure
- Failure in servicing.- Returns:
MockHttpResponse
.
-
getDomain
public java.lang.String getDomain()
Description copied from interface:HttpServerLocation
Obtains the domain for the server.
This is as the client will see the server.
- Specified by:
getDomain
in interfaceHttpServerLocation
- Returns:
- Domain for the server.
-
getHttpPort
public int getHttpPort()
Description copied from interface:HttpServerLocation
Obtains the HTTP port.
This is as the client will see the server.
- Specified by:
getHttpPort
in interfaceHttpServerLocation
- Returns:
- HTTP port.
-
getHttpsPort
public int getHttpsPort()
Description copied from interface:HttpServerLocation
Obtains the HTTPS port.
This is as the client will see the server.
- Specified by:
getHttpsPort
in interfaceHttpServerLocation
- Returns:
- HTTPS port.
-
getClusterHostName
public java.lang.String getClusterHostName()
Description copied from interface:HttpServerLocation
Obtains the name of the host for the server within the cluster. This name should be understood by all nodes within the cluster.- Specified by:
getClusterHostName
in interfaceHttpServerLocation
- Returns:
- Name of the host within the cluster.
-
getClusterHttpPort
public int getClusterHttpPort()
Description copied from interface:HttpServerLocation
The cluster may be behind a load balancer and the server may be running on a different port than expected by the client.- Specified by:
getClusterHttpPort
in interfaceHttpServerLocation
- Returns:
- Actual port on the cluster host the server is running on.
-
getClusterHttpsPort
public int getClusterHttpsPort()
Description copied from interface:HttpServerLocation
The cluster may be behind a load balancer and the server may be running on a different port than expected by the client.- Specified by:
getClusterHttpsPort
in interfaceHttpServerLocation
- Returns:
- Actual secure port on the cluster host the server is running on.
-
createClientUrl
public java.lang.String createClientUrl(boolean isSecure, java.lang.String path)
Description copied from interface:HttpServerLocation
Creates the client URL to call theHttpServer
.- Specified by:
createClientUrl
in interfaceHttpServerLocation
- Parameters:
isSecure
- If secure URL.path
- Path including query string and fragment.- Returns:
- URL for the client to call on the
HttpServer
.
-
configureHttpServer
public void configureHttpServer(HttpServerImplementationContext context)
Description copied from interface:HttpServerImplementation
Configures theHttpServer
.- Specified by:
configureHttpServer
in interfaceHttpServerImplementation
- Parameters:
context
-HttpServerImplementationContext
.
-
-