Skip to content

Commit

Permalink
HttpParserTest: added UnitTest with 404 response file
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan Langendorf committed Jun 18, 2014
1 parent a051c5c commit e4925ca
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/test/java/retromock/parser/HttpParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import retromock.test.FileLocator;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -16,16 +17,16 @@

public class HttpParserTest {

static final Path HTTP_200_FILE = FileLocator.findFirstInClasspath("http-200-response.txt");
static final List<Path> HTTP_FILES = FileLocator.findAllInClasspath("http-*-response.txt");
static final String LOCALHOST = "http://localhost";
static final String NAME_VALUE = "\"\\w+?\"\\s*:\\s*\".+?\",?\\s*";
static final String LIST = "\\[\\s*(" + NAME_VALUE + ")+\\],?\\s*";
static final String MULTI_LIST = "\\[\\s*(" + NAME_VALUE + "|" + LIST + ")+\\],?\\s*";
static final Pattern JSON_PATTERN = Pattern.compile("\\{\\s*(" + NAME_VALUE + "|" + MULTI_LIST + ")+\\s*\\}");

@Test
public void testParseResponseFromFile() throws Exception {
Response response = HttpParser.parse(LOCALHOST, HTTP_200_FILE);
public void testParse200ResponseFromFile() throws Exception {
Response response = HttpParser.parse(LOCALHOST, getFile("http-200-response.txt"));
assertNotNull(response);
assertEquals(LOCALHOST, response.getUrl());
assertEquals(200, response.getStatus());
Expand All @@ -35,10 +36,23 @@ public void testParseResponseFromFile() throws Exception {
assertTrue(response.getBody() instanceof TypedByteArray);
TypedByteArray body = (TypedByteArray) response.getBody();
assertEquals(headers.get("Content-Type"), body.mimeType());
assertEquals(headers.get("Content-Length"), String.valueOf(body.length()));
String bodyAsStr = new String(body.getBytes());
assertTrue(JSON_PATTERN.matcher(bodyAsStr.replaceAll("\n", "")).matches());
}

@Test
public void testParse404ResponseFromFile() throws Exception {
Response response = HttpParser.parse(LOCALHOST, getFile("http-404-response.txt"));
assertNotNull(response);
assertEquals(LOCALHOST, response.getUrl());
assertEquals(404, response.getStatus());
assertEquals("Not Found", response.getReason());
Map<String, String> headers = headerMap(response.getHeaders());
assertEquals("Flat-File", headers.get("X-Powered-By"));
assertEquals(headers.get("Content-Length"), "0");
}

private Map<String, String> headerMap(List<Header> headers) {
Map<String, String> headerMap = new HashMap<>();
for (Header header : headers) {
Expand All @@ -47,4 +61,13 @@ private Map<String, String> headerMap(List<Header> headers) {
return headerMap;
}

private static Path getFile(String name) {
Path fileName = Paths.get(name);
for (Path path : HTTP_FILES) {
if (path.endsWith(fileName)) {
return path;
}
}
return null;
}
}

0 comments on commit e4925ca

Please sign in to comment.