DefaultH2ResponseConverter implements H2MessageConverter<HttpResponse> { @Override public HttpResponse convert(final List<Header> headers) throws HttpException { String statusText = null; final List<Header> messageHeaders = new ArrayList<>(); for (int i = 0; i < headers.size(); i++) { final Header header = headers.get(i); final String name = header.getName(); final String value = header.getValue(); for (int n = 0; n < name.length(); n++) { final char ch = name.charAt(n); if (Character.isAlphabetic(ch) && !Character.isLowerCase(ch)) { throw new ProtocolException("Header name '%s' is invalid (header name contains uppercase characters)", name); } } if (name.startsWith(":")) { if (!messageHeaders.isEmpty()) { throw new ProtocolException("Invalid sequence of headers (pseudo-headers must precede message headers)"); } if (name.equals(H2PseudoResponseHeaders.STATUS)) { if (statusText != null) { throw new ProtocolException("Multiple '%s' response headers are illegal", name); } statusText = value; } else { throw new ProtocolException("Unsupported response header '%s'", name); } } else { if (name.equalsIgnoreCase(HttpHeaders.CONNECTION)) { throw new ProtocolException("Header '%s: %s' is illegal for HTTP/2 messages", header.getName(), header.getValue()); } messageHeaders.add(header); } } if (statusText == null) { throw new ProtocolException("Mandatory response header '%s' not found", H2PseudoResponseHeaders.STATUS); } final int statusCode; try { statusCode = Integer.parseInt(statusText); } catch (final NumberFormatException ex) { throw new ProtocolException("Invalid response status: " + statusText); } final HttpResponse response = new BasicHttpResponse(statusCode, null); response.setVersion(HttpVersion.HTTP_2); for (int i = 0; i < messageHeaders.size(); i++) { response.addHeader(messageHeaders.get(i)); } return response; }  @Override HttpResponse convert(final List<Header> headers); @Override List<Header> convert(final HttpResponse message); final static DefaultH2ResponseConverter INSTANCE; }
@Test public void testConvertFromMessageBasic() throws Exception { final HttpResponse response = new BasicHttpResponse(200); response.addHeader("custom123", "Value"); final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter(); final List<Header> headers = converter.convert(response); Assert.assertNotNull(headers); Assert.assertEquals(2, headers.size()); final Header header1 = headers.get(0); Assert.assertEquals(":status", header1.getName()); Assert.assertEquals("200", header1.getValue()); final Header header2 = headers.get(1); Assert.assertEquals("custom123", header2.getName()); Assert.assertEquals("Value", header2.getValue()); }
@Test public void testConvertFromMessageInvalidStatus() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Response status 99 is invalid"); final HttpResponse response = new BasicHttpResponse(99); response.addHeader("Custom123", "Value"); final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter(); converter.convert(response); }
@Test public void testConvertFromMessageConnectionHeader() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Header 'Connection: Keep-Alive' is illegal for HTTP/2 messages"); final HttpResponse response = new BasicHttpResponse(200); response.addHeader("Connection", "Keep-Alive"); final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter(); converter.convert(response); }
@Test public void testConvertFromMessageInvalidHeader() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Header name ':custom' is invalid"); final HttpResponse response = new BasicHttpResponse(200); response.addHeader(":custom", "stuff"); final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter(); converter.convert(response); }
@Test public void testConvertFromFieldsBasic() throws Exception { final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":status", "200"), new BasicHeader("location", "http: new BasicHeader("custom123", "value")); final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter(); final HttpResponse response = converter.convert(headers); Assert.assertNotNull(response ); Assert.assertEquals(200, response .getCode()); final Header[] allHeaders = response.getHeaders(); Assert.assertEquals(2, allHeaders.length); Assert.assertEquals("location", allHeaders[0].getName()); Assert.assertEquals("http: Assert.assertEquals("custom123", allHeaders[1].getName()); Assert.assertEquals("value", allHeaders[1].getValue()); }
@Test public void testConvertFromFieldsUpperCaseHeaderName() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Header name ':Status' is invalid (header name contains uppercase characters)"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":Status", "200"), new BasicHeader("location", "http: new BasicHeader("custom123", "value")); final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter(); converter.convert(headers); }
@Test public void testConvertFromFieldsInvalidStatusCode() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Invalid response status: boom"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":status", "boom"), new BasicHeader("location", "http: new BasicHeader("custom123", "value")); final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter(); converter.convert(headers); }
@Test public void testConvertFromFieldsConnectionHeader() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Header 'connection: keep-alive' is illegal for HTTP/2 messages"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":status", "200"), new BasicHeader("location", "http: new BasicHeader("connection", "keep-alive")); final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter(); converter.convert(headers); }
@Test public void testConvertFromFieldsMissingStatus() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Mandatory response header ':status' not found"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader("location", "http: new BasicHeader("custom", "value")); final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter(); converter.convert(headers); }
@Test public void testConvertFromFieldsUnknownPseudoHeader() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Unsupported response header ':custom'"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":status", "200"), new BasicHeader(":custom", "200"), new BasicHeader("location", "http: new BasicHeader("custom1", "value")); final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter(); converter.convert(headers); }
@Test public void testConvertFromFieldsMultipleStatus() throws Exception { thrown.expect(HttpException.class); thrown.expectMessage("Multiple ':status' response headers are illegal"); final List<Header> headers = Arrays.<Header>asList( new BasicHeader(":status", "200"), new BasicHeader(":status", "200"), new BasicHeader("location", "http: new BasicHeader("custom1", "value")); final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter(); converter.convert(headers); }