@Test public void testFromWithoutBody() throws Exception { Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("key1", "value1"); headers.put("key2", "value2,value3,value4"); int status = 200; RecordedHttpResponse recordedHttpResponse = new RecordedHttpResponse(status, headers, null); FullHttpResponse fullHttpResponse = NettyHttpResponseMapper.from(recordedHttpResponse); Assert.assertEquals(fullHttpResponse.getStatus().code(), status); Assert.assertEquals(fullHttpResponse.headers().get("key1"), "value1"); List<String> headrValues = fullHttpResponse.headers().getAll("key2"); Assert.assertEquals(headrValues.size(), 1); Assert.assertTrue(headrValues.contains("value2,value3,value4")); }
/** * If found matched request, then return response accordingly. * Otherwise, return bad request. * * @return bad request if not matched request/response found in the scene. * */ private FullHttpResponse playBack() throws IOException { RecordedHttpRequest recordedHttpRequest = _clientRequestBuilder.build(); boolean found = _sceneAccessLayer.hasMatchRequest(recordedHttpRequest); if (!found) { if (LOG.isDebugEnabled()) { LOG.debug(_sceneAccessLayer.getMatchFailureDescription(recordedHttpRequest)); } return NoMatchResponseGenerator.generateNoMatchResponse(recordedHttpRequest); } RecordedHttpResponse recordedHttpResponse = _sceneAccessLayer.playback(recordedHttpRequest); return NettyHttpResponseMapper.from(recordedHttpResponse); } }
@Test public void testDuplicateHeader() throws IOException { Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("key1", "value1"); headers.put("key1", "value2"); int status = 200; RecordedHttpResponse recordedHttpResponse = new RecordedHttpResponse(status, headers, null); FullHttpResponse fullHttpResponse = NettyHttpResponseMapper.from(recordedHttpResponse); Assert.assertEquals(fullHttpResponse.getStatus().code(), status); List<String> headrValues = fullHttpResponse.headers().getAll("key1"); Assert.assertEquals(headrValues.size(), 2); Assert.assertTrue(headrValues.contains("value1")); Assert.assertTrue(headrValues.contains("value2")); } }
@Test public void testNonCookieHeader() throws URISyntaxException, IOException { Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("key1", "value1"); headers.put("Not-Set-Cookie", "YSxiLGM=, ZCxlLGY="); int status = 200; String str = "Hello world"; RecordedStringHttpBody recordedStringHttpBody = new RecordedStringHttpBody(str); RecordedHttpResponse recordedHttpResponse = new RecordedHttpResponse(status, headers, recordedStringHttpBody); FullHttpResponse fullHttpResponse = NettyHttpResponseMapper.from(recordedHttpResponse); Assert.assertEquals(fullHttpResponse.getStatus().code(), status); Assert.assertEquals(fullHttpResponse.headers().get("key1"), "value1"); List<String> headrValues = fullHttpResponse.headers().getAll("Not-Set-Cookie"); Assert.assertEquals(headrValues.size(), 1); Assert.assertTrue(headrValues.contains("YSxiLGM=, ZCxlLGY=")); }
@Test public void testFromWithBody() throws Exception { Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("key1", "value1"); headers.put("key2", "value2,value3,value4"); int status = 200; String str = "Hello world"; RecordedStringHttpBody recordedStringHttpBody = new RecordedStringHttpBody(str); RecordedHttpResponse recordedHttpResponse = new RecordedHttpResponse(status, headers, recordedStringHttpBody); FullHttpResponse fullHttpResponse = NettyHttpResponseMapper.from(recordedHttpResponse); Assert.assertEquals(fullHttpResponse.getStatus().code(), status); Assert.assertEquals(fullHttpResponse.headers().get("key1"), "value1"); List<String> headrValues = fullHttpResponse.headers().getAll("key2"); Assert.assertEquals(headrValues.size(), 1); Assert.assertTrue(headrValues.contains("value2,value3,value4")); Assert.assertEquals(fullHttpResponse.content().array(), str.getBytes()); }
@Test public void testCookieHeader() throws URISyntaxException, IOException { Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("key1", "value1"); headers.put("Set-Cookie", "YSxiLGM=, ZCxlLGY="); int status = 200; String str = "Hello world"; RecordedStringHttpBody recordedStringHttpBody = new RecordedStringHttpBody(str); RecordedHttpResponse recordedHttpResponse = new RecordedHttpResponse(status, headers, recordedStringHttpBody); FullHttpResponse fullHttpResponse = NettyHttpResponseMapper.from(recordedHttpResponse); Assert.assertEquals(fullHttpResponse.getStatus().code(), status); Assert.assertEquals(fullHttpResponse.headers().get("key1"), "value1"); List<String> headrValues = fullHttpResponse.headers().getAll("Set-Cookie"); Assert.assertEquals(headrValues.size(), 1); Assert.assertTrue(headrValues.contains("YSxiLGM=, ZCxlLGY=")); }