Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions src/main/java/com/harlap/test/http/MockHttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.simpleframework.http.Request;
import org.simpleframework.http.Response;
Expand All @@ -29,6 +31,12 @@
import org.simpleframework.transport.connect.SocketConnection;

public class MockHttpServer {

/**
* specific header type for content-type
*/
public static final String CONTENT_TYPE = "Content-Type";

public enum Method {
GET, POST, PUT, DELETE;
}
Expand Down Expand Up @@ -90,22 +98,28 @@ public int hashCode() {

private class ExpectedResponse {
private int statusCode;
private String contentType;
private final Map<String, String> headers = new HashMap<String, String>();
private String body;

public ExpectedResponse(int statusCode, String contentType, String body) {
this.statusCode = statusCode;
this.contentType = contentType;
this.headers.put(MockHttpServer.CONTENT_TYPE, contentType);
this.body = body;
}

public ExpectedResponse(final int statusCode, final Map<String, String> headers, final String body) {
this.statusCode = statusCode;
this.headers.putAll(headers);
this.body = body;
}

public int getStatusCode() {
return statusCode;
}

public String getContentType() {
return contentType;
}
public Map<String, String> getHeaders() {
return Collections.unmodifiableMap(this.headers);
}

public String getBody() {
return body;
Expand Down Expand Up @@ -139,7 +153,7 @@ public void handle(Request req, Response response) {
ExpectedResponse expectedResponse = responsesForRequests
.get(expectedRequest);
response.setCode(expectedResponse.getStatusCode());
response.set("Content-Type", expectedResponse.getContentType());
setHttpHeadersToResponse(response, expectedResponse);
PrintStream body = null;
try {
body = response.getPrintStream();
Expand All @@ -152,7 +166,7 @@ public void handle(Request req, Response response) {
body.close();
} else {
response.setCode(500);
response.set("Content-Type", "text/plain;charset=utf-8");
response.set(MockHttpServer.CONTENT_TYPE, "text/plain;charset=utf-8");
PrintStream body;
try {
body = response.getPrintStream();
Expand All @@ -164,6 +178,16 @@ public void handle(Request req, Response response) {
}
}
}

/**
* @param response
* @param expectedResponse
*/
private void setHttpHeadersToResponse(final Response response, final ExpectedResponse expectedResponse) {
for (final Entry<String, String> headerEntry : expectedResponse.getHeaders().entrySet()) {
response.set(headerEntry.getKey(), headerEntry.getValue());
}
}

public void addExpectedRequest(ExpectedRequest request) {
lastAddedExpectation = request;
Expand Down Expand Up @@ -222,6 +246,11 @@ public MockHttpServer respondWith(int statusCode, String contentType,
contentType, body));
return this;
}

public MockHttpServer respondWith(final int statusCode, final Map<String, String> headers, final String body) {
this.handler.addExpectedResponse(new ExpectedResponse(statusCode, headers, body));
return this;
}

public MockHttpServer expect(Method method, String path, String data) {
handler.addExpectedRequest(new ExpectedRequest(method, path, data));
Expand All @@ -232,4 +261,4 @@ public void verify() {
handler.verify();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static org.junit.Assert.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
Expand All @@ -35,7 +37,7 @@
import org.junit.Before;
import org.junit.Test;

public class MockHttpServerBehaviour {
public class MockHttpServerTest {

private static final int PORT = 51234;
private static final String baseUrl = "http://localhost:" + PORT;
Expand All @@ -58,9 +60,10 @@ public void tearDown() throws Exception {
@Test
public void testShouldHandleGetRequests() throws ClientProtocolException,
IOException {
final String contentType = "text/plain";
// Given a mock server configured to respond to a GET / with "OK"
server.expect(MockHttpServer.Method.GET, "/").respondWith(200,
"text/plain", "OK");
contentType, "OK");

// When a request for GET / arrives
HttpGet req = new HttpGet(baseUrl + "/");
Expand All @@ -73,6 +76,9 @@ public void testShouldHandleGetRequests() throws ClientProtocolException,

// And the status code is 200
assertEquals(200, statusCode);

// And the Content-Type header is there with the correct value
assertEquals(contentType, response.getFirstHeader(MockHttpServer.CONTENT_TYPE).getValue());
}

@Test
Expand Down Expand Up @@ -229,5 +235,30 @@ public void testShouldRespondWith500OWhenNotMatchingAnyRequestExpectation() thro
public void testVerifyDoNothingWhenNoExceptations() {
server.verify();
}

@Test
public void testShouldReplyWithCorrectHeaders() throws IllegalStateException, IOException {
// Given a mock server configured to respond to a GET / with "OK" with specific headers
final Map<String, String> httpHeaders = new HashMap<String, String>();
httpHeaders.put(MockHttpServer.CONTENT_TYPE, "application/xml");
httpHeaders.put("fakeHttpHeader", "fakeHttpValue");
this.server.expect(MockHttpServer.Method.GET, "/").respondWith(200, httpHeaders, "OK");

// When a request for GET / arrives
final HttpGet req = new HttpGet(MockHttpServerTest.baseUrl + "/");
final HttpResponse response = this.client.execute(req);
final String responseBody = IOUtils.toString(response.getEntity().getContent());
final int statusCode = response.getStatusLine().getStatusCode();

// Then the response is "OK"
assertEquals("OK", responseBody);

// And the status code is 200
assertEquals(200, statusCode);

// And our http headers are there
assertEquals("fakeHttpValue", response.getFirstHeader("fakeHttpHeader").getValue());
assertEquals("application/xml", response.getFirstHeader(MockHttpServer.CONTENT_TYPE).getValue());
}

}
}