@Test public void testResponseContentInvalidInput() throws Exception { final ResponseContent interceptor = new ResponseContent(); try { interceptor.process(null, null, null); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }
/** * Creates {@link HttpProcessorBuilder} initialized with default protocol interceptors * for server side HTTP/1.1 processing. * * @param serverInfo the server info text or {@code null} for default. * @return the processor builder. */ public static HttpProcessorBuilder customServer(final String serverInfo) { return HttpProcessorBuilder.create() .addAll( new ResponseDate(), new ResponseServer(!TextUtils.isBlank(serverInfo) ? serverInfo : VersionInfo.getSoftwareInfo(SOFTWARE, "org.apache.hc.core5", HttpProcessors.class)), new ResponseContent(), new ResponseConnControl()) .addAll( new RequestValidateHost()); }
@Override public void process( final HttpResponse response, final EntityDetails entity, final HttpContext context) throws HttpException, IOException { Args.notNull(context, "HTTP context"); final ProtocolVersion ver = context.getProtocolVersion(); if (ver.getMajor() < 2) { super.process(response, entity, context); } else if (entity != null) { MessageSupport.addContentTypeHeader(response, entity); MessageSupport.addContentEncodingHeader(response, entity); MessageSupport.addTrailerHeader(response, entity); } }
/** * Creates {@link HttpProcessorBuilder} initialized with default protocol interceptors * for server side HTTP/1.1 processing. * * @param serverInfo the server info text or {@code null} for default. * @return the processor builder. */ public static HttpProcessorBuilder customServer(final String serverInfo) { return HttpProcessorBuilder.create() .addAll( new ResponseDate(), new ResponseServer(!TextUtils.isBlank(serverInfo) ? serverInfo : VersionInfo.getSoftwareInfo(SOFTWARE, "org.apache.hc.core5", HttpProcessors.class)), new ResponseContent(), new ResponseConnControl()) .addAll( new RequestValidateHost()); }
@Test public void testResponseContentStatusNoContent() throws Exception { final HttpContext context = new BasicHttpContext(null); final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); response.setCode(HttpStatus.SC_NO_CONTENT); final ResponseContent interceptor = new ResponseContent(); interceptor.process(response, response.getEntity(), context); final Header header = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH); Assert.assertNull(header); }
@Test public void testResponseContentStatusNotModified() throws Exception { final HttpContext context = new BasicHttpContext(null); final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); response.setCode(HttpStatus.SC_NOT_MODIFIED); final ResponseContent interceptor = new ResponseContent(); interceptor.process(response, response.getEntity(), context); final Header header = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH); Assert.assertNull(header); }
@Test public void testResponseContentNoEntity() throws Exception { final HttpContext context = new BasicHttpContext(null); final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); final ResponseContent interceptor = new ResponseContent(); interceptor.process(response, response.getEntity(), context); final Header header = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH); Assert.assertNotNull(header); Assert.assertEquals("0", header.getValue()); }
@Test public void testResponseContentAddHeaders() throws Exception { final ResponseContent interceptor = new ResponseContent(true); final HttpContext context = new BasicHttpContext(null); final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); interceptor.process(response, response.getEntity(), context); Assert.assertEquals("0", response.getFirstHeader(HttpHeaders.CONTENT_LENGTH).getValue()); Assert.assertNull(response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING)); }
@Test public void testResponseContentEntityNoContentTypeAndEncoding() throws Exception { final HttpContext context = new BasicHttpContext(null); final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); final BasicHttpEntity entity = new BasicHttpEntity(); response.setEntity(entity); final ResponseContent interceptor = new ResponseContent(); interceptor.process(response, response.getEntity(), context); final Header h1 = response.getFirstHeader(HttpHeaders.CONTENT_TYPE); Assert.assertNull(h1); final Header h2 = response.getFirstHeader(HttpHeaders.CONTENT_ENCODING); Assert.assertNull(h2); }
@Test public void testResponseContentInvalidResponseState() throws Exception { final ResponseContent interceptor = new ResponseContent(); final HttpContext context = new BasicHttpContext(null); try { final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); response.addHeader(new BasicHeader(HttpHeaders.CONTENT_LENGTH, "10")); interceptor.process(response, response.getEntity(), context); Assert.fail("ProtocolException should have been thrown"); } catch (final ProtocolException ex) { // expected } try { final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); response.addHeader(new BasicHeader(HttpHeaders.TRANSFER_ENCODING, "stuff")); interceptor.process(response, response.getEntity(), context); Assert.fail("ProtocolException should have been thrown"); } catch (final ProtocolException ex) { // expected } }
@Test public void testResponseContentOverwriteHeaders() throws Exception { final ResponseContent interceptor = new ResponseContent(true); final HttpContext context = new BasicHttpContext(null); final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); response.addHeader(new BasicHeader(HttpHeaders.CONTENT_LENGTH, "10")); response.addHeader(new BasicHeader(HttpHeaders.TRANSFER_ENCODING, "whatever")); interceptor.process(response, response.getEntity(), context); Assert.assertEquals("0", response.getFirstHeader(HttpHeaders.CONTENT_LENGTH).getValue()); }
@Test public void testResponseContentEntityChunkedHTTP10() throws Exception { final HttpContext context = new BasicHttpContext(null); context.setProtocolVersion(HttpVersion.HTTP_1_0); final BasicClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); final BasicHttpEntity entity = new BasicHttpEntity(); entity.setChunked(true); response.setEntity(entity); final ResponseContent interceptor = new ResponseContent(); interceptor.process(response, response.getEntity(), context); final Header h1 = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING); Assert.assertNull(h1); final Header h2 = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH); Assert.assertNull(h2); }
@Test public void testResponseContentEntityUnknownContentLength() throws Exception { final HttpContext context = new BasicHttpContext(null); final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); final BasicHttpEntity entity = new BasicHttpEntity(); response.setEntity(entity); final ResponseContent interceptor = new ResponseContent(); interceptor.process(response, response.getEntity(), context); final Header h1 = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING); Assert.assertNotNull(h1); Assert.assertEquals("chunked", h1.getValue()); final Header h2 = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH); Assert.assertNull(h2); }
@Test public void testResponseContentEntityChunked() throws Exception { final HttpContext context = new BasicHttpContext(null); final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); final BasicHttpEntity entity = new BasicHttpEntity(); entity.setChunked(true); response.setEntity(entity); final ResponseContent interceptor = new ResponseContent(); interceptor.process(response, response.getEntity(), context); final Header h1 = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING); Assert.assertNotNull(h1); Assert.assertEquals("chunked", h1.getValue()); final Header h2 = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH); Assert.assertNull(h2); }
@Test public void testResponseContentEntityContentLenghtDelimited() throws Exception { final HttpContext context = new BasicHttpContext(null); final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); final BasicHttpEntity entity = new BasicHttpEntity(); entity.setContentLength (10); response.setEntity(entity); final ResponseContent interceptor = new ResponseContent(); interceptor.process(response, response.getEntity(), context); final Header h1 = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH); Assert.assertNotNull(h1); Assert.assertEquals("10", h1.getValue()); final Header h2 = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING); Assert.assertNull(h2); }
@Test public void testResponseContentEntityContentTypeAndEncoding() throws Exception { final HttpContext context = new BasicHttpContext(null); final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK"); final BasicHttpEntity entity = new BasicHttpEntity(); entity.setContentEncoding("whatever"); entity.setContentType("whatever"); response.setEntity(entity); final ResponseContent interceptor = new ResponseContent(); interceptor.process(response, response.getEntity(), context); final Header h1 = response.getFirstHeader(HttpHeaders.CONTENT_TYPE); Assert.assertNotNull(h1); Assert.assertEquals("whatever", h1.getValue()); final Header h2 = response.getFirstHeader(HttpHeaders.CONTENT_ENCODING); Assert.assertNotNull(h2); Assert.assertEquals("whatever", 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()); }