Skip to content

Commit

Permalink
HttpParserTest: verify using Http200ResponseBean
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan Langendorf committed Jun 18, 2014
1 parent 510d1b1 commit 9a84ed8
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/test/java/retromock/parser/HttpParserTest.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
package retromock.parser;

import com.google.gson.Gson;
import org.junit.Test;
import retrofit.client.Header;
import retrofit.client.Response;
import retrofit.mime.TypedByteArray;
import retromock.test.FileLocator;
import retromock.test.Http200ResponseBean;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import static org.junit.Assert.*;

public class HttpParserTest {

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*\\}");
static final Gson GSON = new Gson();

@Test
public void testParse200ResponseFromFile() throws Exception {
Expand All @@ -37,8 +35,9 @@ public void testParse200ResponseFromFile() throws Exception {
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());
Http200ResponseBean bodyAsBean = GSON.fromJson(new String(body.getBytes()), Http200ResponseBean.class);
assertEquals("test", bodyAsBean.getTitle());
assertEquals("qwerty", bodyAsBean.getFoot());
}

@Test
Expand Down

0 comments on commit 9a84ed8

Please sign in to comment.