/** * Ensures that, when setting content bytes, the bytes' encoding is reflected * in the current Content-Type header. * Note, this method does NOT override existing Content-Type values if newEncoding is null. * This allows charset to be set by header only, along with a byte array -- a very typical, * and important, pattern when creating an HttpResponse in an HttpFetcher. */ @Override protected void setContentBytesState(byte[] newBytes, Charset newEncoding) { super.setContentBytesState(newBytes, newEncoding); // Set the new encoding of the raw bytes, in order to ensure that // Content-Type headers are in sync w/ the content's encoding. if (newEncoding != null) setEncoding(newEncoding); }
/** * Ensures that, when setting content bytes, the bytes' encoding is reflected * in the current Content-Type header. * Note, this method does NOT override existing Content-Type values if newEncoding is null. * This allows charset to be set by header only, along with a byte array -- a very typical, * and important, pattern when creating an HttpResponse in an HttpFetcher. */ @Override protected void setContentBytesState(byte[] newBytes, Charset newEncoding) { super.setContentBytesState(newBytes, newEncoding); // Set the new encoding of the raw bytes, in order to ensure that // Content-Type headers are in sync w/ the content's encoding. if (newEncoding != null) setEncoding(newEncoding); }
/** * Ensures that, when setting content bytes, the bytes' encoding is reflected * in the current Content-Type header. * Note, this method does NOT override existing Content-Type values if newEncoding is null. * This allows charset to be set by header only, along with a byte array -- a very typical, * and important, pattern when creating an HttpResponse in an HttpFetcher. */ @Override protected void setContentBytesState(byte[] newBytes, Charset newEncoding) { super.setContentBytesState(newBytes, newEncoding); // Set the new encoding of the raw bytes, in order to ensure that // Content-Type headers are in sync w/ the content's encoding. if (newEncoding != null) setEncoding(newEncoding); }
@Test public void setEncoding() { HttpResponseBuilder builder = new HttpResponseBuilder() .addHeader("Content-Type", "text/html; charset=Big5") .setEncoding(Charsets.UTF_8); Multimap<String, String> headers = builder.getHeaders(); assertEquals("text/html; charset=UTF-8", headers.get("Content-Type").iterator().next()); }
@Test public void setEncoding() { HttpResponseBuilder builder = new HttpResponseBuilder() .addHeader("Content-Type", "text/html; charset=Big5") .setEncoding(Charsets.UTF_8); Multimap<String, String> headers = builder.getHeaders(); assertEquals("text/html; charset=UTF-8", headers.get("Content-Type").iterator().next()); }
@Test public void setEncodingEmpty() { HttpResponseBuilder builder = new HttpResponseBuilder() .addHeader("Content-Type", "text/html") .setEncoding(Charsets.UTF_8); Multimap<String, String> headers = builder.getHeaders(); assertEquals("text/html; charset=UTF-8", headers.get("Content-Type").iterator().next()); }
@Test public void setEncodingEmpty() { HttpResponseBuilder builder = new HttpResponseBuilder() .addHeader("Content-Type", "text/html") .setEncoding(Charsets.UTF_8); Multimap<String, String> headers = builder.getHeaders(); assertEquals("text/html; charset=UTF-8", headers.get("Content-Type").iterator().next()); }
@Test public void setEncodingEmpty() { HttpResponseBuilder builder = new HttpResponseBuilder() .addHeader("Content-Type", "text/html") .setEncoding(Charsets.UTF_8); Multimap<String, String> headers = builder.getHeaders(); assertEquals("text/html; charset=UTF-8", headers.get("Content-Type").iterator().next()); }
@Test public void setEncoding() { HttpResponseBuilder builder = new HttpResponseBuilder() .addHeader("Content-Type", "text/html; charset=Big5") .setEncoding(Charsets.UTF_8); Multimap<String, String> headers = builder.getHeaders(); assertEquals("text/html; charset=UTF-8", headers.get("Content-Type").iterator().next()); }
@Test public void testCreateProxyResponse() throws Exception { HttpResponseBuilder httpResponse = new HttpResponseBuilder(); httpResponse.setContent("Content"); httpResponse.addHeader("header", "hval"); httpResponse.setEncoding(Charset.forName("UTF8")); httpResponse.setHttpStatusCode(404); GadgetsHandlerApi.ProxyResponse response = gadgetHandler.createProxyResponse(RESOURCE, httpResponse.create(), ImmutableSet.<String> of("*"), 1000001L); BeanDelegator.validateDelegator(response); assertEquals("Content", new String(Base64.decodeBase64(response.getProxyContent().getContentBase64()))); assertEquals(404, response.getProxyContent().getCode()); assertEquals(2, response.getProxyContent().getHeaders().size()); assertEquals("Date", response.getProxyContent().getHeaders().get(0).getName()); assertEquals("header", response.getProxyContent().getHeaders().get(1).getName()); assertEquals("hval", response.getProxyContent().getHeaders().get(1).getValue()); assertEquals(1000001L, response.getExpireTimeMs().longValue()); }
@Test public void testCreateProxyResponse() throws Exception { HttpResponseBuilder httpResponse = new HttpResponseBuilder(); httpResponse.setContent("Content"); httpResponse.addHeader("header", "hval"); httpResponse.setEncoding(Charset.forName("UTF8")); httpResponse.setHttpStatusCode(404); GadgetsHandlerApi.ProxyResponse response = gadgetHandler.createProxyResponse(RESOURCE, httpResponse.create(), ImmutableSet.<String> of("*"), 1000001L); BeanDelegator.validateDelegator(response); assertEquals("Content", new String(Base64.decodeBase64(response.getProxyContent().getContentBase64()))); assertEquals(404, response.getProxyContent().getCode()); assertEquals(2, response.getProxyContent().getHeaders().size()); assertEquals("Date", response.getProxyContent().getHeaders().get(0).getName()); assertEquals("header", response.getProxyContent().getHeaders().get(1).getName()); assertEquals("hval", response.getProxyContent().getHeaders().get(1).getValue()); assertEquals(1000001L, response.getExpireTimeMs().longValue()); }