View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http2.impl;
29  
30  import java.util.Arrays;
31  import java.util.List;
32  
33  import org.apache.hc.core5.http.Header;
34  import org.apache.hc.core5.http.HttpException;
35  import org.apache.hc.core5.http.HttpResponse;
36  import org.apache.hc.core5.http.message.BasicHeader;
37  import org.apache.hc.core5.http.message.BasicHttpResponse;
38  import org.junit.Assert;
39  import org.junit.Rule;
40  import org.junit.Test;
41  import org.junit.rules.ExpectedException;
42  
43  public class TestDefaultH2ResponseConverter {
44  
45      @Rule
46      public ExpectedException thrown = ExpectedException.none();
47  
48      @Test
49      public void testConvertFromFieldsBasic() throws Exception {
50  
51          final List<Header> headers = Arrays.<Header>asList(
52                  new BasicHeader(":status", "200"),
53                  new BasicHeader("location", "http://www.example.com/"),
54                  new BasicHeader("custom123", "value"));
55  
56          final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter();
57          final HttpResponse response = converter.convert(headers);
58          Assert.assertNotNull(response );
59          Assert.assertEquals(200, response .getCode());
60          final Header[] allHeaders = response.getHeaders();
61          Assert.assertEquals(2, allHeaders.length);
62          Assert.assertEquals("location", allHeaders[0].getName());
63          Assert.assertEquals("http://www.example.com/", allHeaders[0].getValue());
64          Assert.assertEquals("custom123", allHeaders[1].getName());
65          Assert.assertEquals("value", allHeaders[1].getValue());
66      }
67  
68      @Test
69      public void testConvertFromFieldsUpperCaseHeaderName() throws Exception {
70  
71          thrown.expect(HttpException.class);
72          thrown.expectMessage("Header name ':Status' is invalid (header name contains uppercase characters)");
73  
74          final List<Header> headers = Arrays.<Header>asList(
75                  new BasicHeader(":Status", "200"),
76                  new BasicHeader("location", "http://www.example.com/"),
77                  new BasicHeader("custom123", "value"));
78  
79          final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter();
80          converter.convert(headers);
81      }
82  
83      @Test
84      public void testConvertFromFieldsInvalidStatusCode() throws Exception {
85  
86          thrown.expect(HttpException.class);
87          thrown.expectMessage("Invalid response status: boom");
88  
89          final List<Header> headers = Arrays.<Header>asList(
90                  new BasicHeader(":status", "boom"),
91                  new BasicHeader("location", "http://www.example.com/"),
92                  new BasicHeader("custom123", "value"));
93  
94          final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter();
95          converter.convert(headers);
96      }
97  
98      @Test
99      public void testConvertFromFieldsConnectionHeader() throws Exception {
100 
101         thrown.expect(HttpException.class);
102         thrown.expectMessage("Header 'connection: keep-alive' is illegal for HTTP/2 messages");
103 
104         final List<Header> headers = Arrays.<Header>asList(
105                 new BasicHeader(":status", "200"),
106                 new BasicHeader("location", "http://www.example.com/"),
107                 new BasicHeader("connection", "keep-alive"));
108 
109         final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter();
110         converter.convert(headers);
111     }
112 
113     @Test
114     public void testConvertFromFieldsMissingStatus() throws Exception {
115 
116         thrown.expect(HttpException.class);
117         thrown.expectMessage("Mandatory response header ':status' not found");
118 
119         final List<Header> headers = Arrays.<Header>asList(
120                 new BasicHeader("location", "http://www.example.com/"),
121                 new BasicHeader("custom", "value"));
122 
123         final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter();
124         converter.convert(headers);
125     }
126 
127     @Test
128     public void testConvertFromFieldsUnknownPseudoHeader() throws Exception {
129 
130         thrown.expect(HttpException.class);
131         thrown.expectMessage("Unsupported response header ':custom'");
132 
133         final List<Header> headers = Arrays.<Header>asList(
134                 new BasicHeader(":status", "200"),
135                 new BasicHeader(":custom", "200"),
136                 new BasicHeader("location", "http://www.example.com/"),
137                 new BasicHeader("custom1", "value"));
138 
139         final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter();
140         converter.convert(headers);
141     }
142 
143     @Test
144     public void testConvertFromFieldsMultipleStatus() throws Exception {
145 
146         thrown.expect(HttpException.class);
147         thrown.expectMessage("Multiple ':status' response headers are illegal");
148 
149         final List<Header> headers = Arrays.<Header>asList(
150                 new BasicHeader(":status", "200"),
151                 new BasicHeader(":status", "200"),
152                 new BasicHeader("location", "http://www.example.com/"),
153                 new BasicHeader("custom1", "value"));
154 
155         final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter();
156         converter.convert(headers);
157     }
158 
159     @Test
160     public void testConvertFromMessageBasic() throws Exception {
161 
162         final HttpResponse response = new BasicHttpResponse(200);
163         response.addHeader("custom123", "Value");
164 
165         final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter();
166         final List<Header> headers = converter.convert(response);
167 
168         Assert.assertNotNull(headers);
169         Assert.assertEquals(2, headers.size());
170         final Header header1 = headers.get(0);
171         Assert.assertEquals(":status", header1.getName());
172         Assert.assertEquals("200", header1.getValue());
173         final Header header2 = headers.get(1);
174         Assert.assertEquals("custom123", header2.getName());
175         Assert.assertEquals("Value", header2.getValue());
176     }
177 
178     @Test
179     public void testConvertFromMessageInvalidStatus() throws Exception {
180 
181         thrown.expect(HttpException.class);
182         thrown.expectMessage("Response status 99 is invalid");
183 
184         final HttpResponse response = new BasicHttpResponse(99);
185         response.addHeader("Custom123", "Value");
186 
187         final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter();
188         converter.convert(response);
189     }
190 
191     @Test
192     public void testConvertFromMessageConnectionHeader() throws Exception {
193 
194         thrown.expect(HttpException.class);
195         thrown.expectMessage("Header 'Connection: Keep-Alive' is illegal for HTTP/2 messages");
196 
197         final HttpResponse response = new BasicHttpResponse(200);
198         response.addHeader("Connection", "Keep-Alive");
199 
200         final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter();
201         converter.convert(response);
202     }
203 
204     @Test
205     public void testConvertFromMessageInvalidHeader() throws Exception {
206 
207         thrown.expect(HttpException.class);
208         thrown.expectMessage("Header name ':custom' is invalid");
209 
210         final HttpResponse response = new BasicHttpResponse(200);
211         response.addHeader(":custom", "stuff");
212 
213         final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter();
214         converter.convert(response);
215     }
216 
217 }
218