@Test public void testRequestContentEntityWithTrailers() throws Exception { final HttpContext context = new BasicHttpContext(null); final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/"); final String s = "whatever"; final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII); request.setEntity(new HttpEntityWithTrailers(entity, new BasicHeader("h1", "this"), new BasicHeader("h1", "that"), new BasicHeader("h2", "this and that"))); final RequestContent interceptor = new RequestContent(); interceptor.process(request, request.getEntity(), context); final Header header1 = request.getFirstHeader(HttpHeaders.TRANSFER_ENCODING); Assert.assertNotNull(header1); final Header header2 = request.getFirstHeader(HttpHeaders.TRAILER); Assert.assertNotNull(header2); Assert.assertEquals("h1, h2", header2.getValue()); }
@Test public void testAddContentHeaders() throws Exception { final HttpEntityWithTrailers entity = new HttpEntityWithTrailers( new StringEntity("some stuff with trailers", StandardCharsets.US_ASCII), new BasicHeader("z", "this"), new BasicHeader("b", "that"), new BasicHeader("a", "this and that")); final HttpMessage message = new BasicHttpResponse(200); MessageSupport.addTrailerHeader(message, entity); MessageSupport.addContentTypeHeader(message, entity); final Header h1 = message.getFirstHeader(HttpHeaders.TRAILER); final Header h2 = message.getFirstHeader(HttpHeaders.CONTENT_TYPE); Assert.assertNotNull(h1); Assert.assertEquals("a, b, z", h1.getValue()); Assert.assertNotNull(h2); Assert.assertEquals("text/plain; charset=US-ASCII", h2.getValue()); }
@Test public void testResponseContentEntityWithTrailers() throws Exception { final HttpContext context = new BasicHttpContext(null); final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); final String s = "whatever"; final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII); response.setEntity(new HttpEntityWithTrailers(entity, new BasicHeader("h1", "this"), new BasicHeader("h1", "that"), new BasicHeader("h2", "this and that"))); final ResponseContent interceptor = new ResponseContent(); interceptor.process(response, response.getEntity(), context); final Header header1 = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING); Assert.assertNotNull(header1); final Header header2 = response.getFirstHeader(HttpHeaders.TRAILER); Assert.assertNotNull(header2); Assert.assertEquals("h1, h2", header2.getValue()); }
@Test public void testContentHeadersAlreadyPresent() throws Exception { final HttpEntityWithTrailers entity = new HttpEntityWithTrailers( new StringEntity("some stuff with trailers", StandardCharsets.US_ASCII), new BasicHeader("z", "this"), new BasicHeader("b", "that"), new BasicHeader("a", "this and that")); final HttpMessage message = new BasicHttpResponse(200); message.addHeader(HttpHeaders.TRAILER, "a, a, a"); message.addHeader(HttpHeaders.CONTENT_TYPE, "text/plain; charset=ascii"); MessageSupport.addTrailerHeader(message, entity); MessageSupport.addContentTypeHeader(message, entity); final Header h1 = message.getFirstHeader(HttpHeaders.TRAILER); final Header h2 = message.getFirstHeader(HttpHeaders.CONTENT_TYPE); Assert.assertNotNull(h1); Assert.assertEquals("a, a, a", h1.getValue()); Assert.assertNotNull(h2); Assert.assertEquals("text/plain; charset=ascii", h2.getValue()); }