public class TestHttpServer extends Object
In the following example, if the test passes, you know that myXmlClient
made a GET request to the correct URI and sent the appropriate Accept header;
if the request did not have the expected URI or header, the test server would return
a 500 error instead of the expected content.
server.respondIf(
allOf(method().is("GET"),
uri().is(URI.create("/resource")),
header("Accept").is(contains("application/xml"))),
status(200).
body(MY_XML_CONTENT).
header("Content-Type", "application/xml"));
assertThat(myXmlClient.makeGetRequestTo(server.getBaseUri().resolve("/resource")),
returnsExpectedXmlContent());
| Modifier and Type | Class and Description |
|---|---|
static class |
TestHttpServer.RequestProperties
Simple abstraction of an HTTP request, used for matching requests with
respondIf(org.hamcrest.Matcher<com.atlassian.utt.http.TestHttpServer.RequestProperties>, com.atlassian.utt.http.TestHttpServer.ResponseProperties). |
static class |
TestHttpServer.ResponseProperties
A simple abstraction of an HTTP response, used for configuring the test server.
|
| Modifier and Type | Field and Description |
|---|---|
static String |
BASIC_AUTH_CHALLENGE
Use this constant with
authChallenge(java.lang.String) or proxyAuthChallenge(java.lang.String) in a test
response to simulate a basicauth challenge. |
static String |
DIGEST_AUTH_CHALLENGE
Use this constant with
authChallenge(java.lang.String) or proxyAuthChallenge(java.lang.String) in a test
response to simulate a digest authentication challenge. |
| Modifier and Type | Method and Description |
|---|---|
static TestHttpServer.ResponseProperties |
authChallenge(String challengeHeaderValue)
Constructs a
TestHttpServer.ResponseProperties instance with a 401 (unauthorized)
status and a WWW-Authenticate header. |
static TestHttpServer |
create(int port)
Creates a new test server instance.
|
URI |
getBaseUri() |
boolean |
isPrintUnexpectedRequests()
Returns true if unmatched HTTP requests will be logged to the console.
|
static TestHttpServer.ResponseProperties |
proxyAuthChallenge(String challengeHeaderValue)
Constructs a
TestHttpServer.ResponseProperties instance with a 407 (proxy authorization)
status and a Proxy-Authenticate header. |
static NamedFunction<TestHttpServer,Iterable<? extends TestHttpServer.RequestProperties>> |
requests()
Use this to construct a Hamcrest matcher for the list of all requests that the
test server has received.
|
void |
reset()
Clears the lists of expected requests and received requests.
|
TestHttpServer |
respondIf(org.hamcrest.Matcher<TestHttpServer.RequestProperties> pattern,
TestHttpServer.ResponseProperties r)
Configures the embedded server to send a specific response whenever it receives a
request that meets certain conditions.
|
void |
setPrintUnexpectedRequests(boolean flag)
Specifies whether unmatched HTTP requests should be logged to the console, in addition
to the server's usual behavior of returning a 500 error.
|
void |
start()
Starts the embedded server.
|
static TestHttpServer.ResponseProperties |
status(int status)
Constructs a
TestHttpServer.ResponseProperties instance with a specific HTTP status code. |
void |
stop()
Stops the embedded server immediately, releasing all HTTP worker threads.
|
public static final String BASIC_AUTH_CHALLENGE
authChallenge(java.lang.String) or proxyAuthChallenge(java.lang.String) in a test
response to simulate a basicauth challenge.public static final String DIGEST_AUTH_CHALLENGE
authChallenge(java.lang.String) or proxyAuthChallenge(java.lang.String) in a test
response to simulate a digest authentication challenge.public static TestHttpServer create(int port) throws IOException
port - the port to listen onIOExceptionpublic URI getBaseUri()
public void start()
public void stop()
public boolean isPrintUnexpectedRequests()
public void setPrintUnexpectedRequests(boolean flag)
public void reset()
public TestHttpServer respondIf(org.hamcrest.Matcher<TestHttpServer.RequestProperties> pattern, TestHttpServer.ResponseProperties r)
If the server receives a request that does not match any of the matchers previously
specified with respondIf, it will return a 500 error; the response body
will include a description of the incorrect request.
pattern - a Hamcrest matcher that will match the desired requestr - properties of the response to sendpublic static TestHttpServer.ResponseProperties status(int status)
TestHttpServer.ResponseProperties instance with a specific HTTP status code.
To specify additional properties of the response, call TestHttpServer.ResponseProperties.body
and/or TestHttpServer.ResponseProperties.header(java.lang.String, java.lang.String) on the returned response object.public static TestHttpServer.ResponseProperties authChallenge(String challengeHeaderValue)
TestHttpServer.ResponseProperties instance with a 401 (unauthorized)
status and a WWW-Authenticate header. Use this to test authentication behavior
of your client code.challengeHeaderValue - the value of the Authenticate headerpublic static TestHttpServer.ResponseProperties proxyAuthChallenge(String challengeHeaderValue)
TestHttpServer.ResponseProperties instance with a 407 (proxy authorization)
status and a Proxy-Authenticate header. Use this to test HTTP proxy behavior
of your client code.challengeHeaderValue - the value of the Proxy-Authenticate headerpublic static NamedFunction<TestHttpServer,Iterable<? extends TestHttpServer.RequestProperties>> requests()
doSomethingThatShouldMakeAPostRequest();
assertThat(server, requests().is(contains(
allOf(method().is("POST"),
uri().is(URI.create("/resource")),
requestBody().is("I should have posted this")
)));
NamedFunctionCopyright © 2017 Atlassian. All rights reserved.