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.http.protocol;
29  
30  import java.nio.charset.StandardCharsets;
31  
32  import org.apache.hc.core5.http.ClassicHttpResponse;
33  import org.apache.hc.core5.http.Header;
34  import org.apache.hc.core5.http.HeaderElements;
35  import org.apache.hc.core5.http.HttpHeaders;
36  import org.apache.hc.core5.http.HttpStatus;
37  import org.apache.hc.core5.http.HttpVersion;
38  import org.apache.hc.core5.http.ProtocolException;
39  import org.apache.hc.core5.http.io.entity.BasicHttpEntity;
40  import org.apache.hc.core5.http.io.entity.HttpEntityWithTrailers;
41  import org.apache.hc.core5.http.io.entity.StringEntity;
42  import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
43  import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
44  import org.apache.hc.core5.http.message.BasicHeader;
45  import org.apache.hc.core5.net.URIAuthority;
46  import org.junit.Assert;
47  import org.junit.Test;
48  
49  public class TestStandardInterceptors {
50  
51      @Test
52      public void testRequestConnControlGenerated() throws Exception {
53          final HttpContext context = new BasicHttpContext(null);
54          final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
55          final RequestConnControl interceptor = new RequestConnControl();
56          interceptor.process(request, request.getEntity(), context);
57          final Header header = request.getFirstHeader(HttpHeaders.CONNECTION);
58          Assert.assertNotNull(header);
59          Assert.assertEquals("keep-alive", header.getValue());
60      }
61  
62      @Test
63      public void testRequestConnControlConnectMethod() throws Exception {
64          final HttpContext context = new BasicHttpContext(null);
65          final BasicClassicHttpRequest request = new BasicClassicHttpRequest("CONNECT", "/");
66          final RequestConnControl interceptor = new RequestConnControl();
67          interceptor.process(request, request.getEntity(), context);
68          final Header header = request.getFirstHeader(HttpHeaders.CONNECTION);
69          Assert.assertNull(header);
70      }
71  
72      @Test
73      public void testRequestConnControlCustom() throws Exception {
74          final HttpContext context = new BasicHttpContext(null);
75          final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
76          final Header myheader = new BasicHeader(HttpHeaders.CONNECTION, "close");
77          request.addHeader(myheader);
78          final RequestConnControl interceptor = new RequestConnControl();
79          interceptor.process(request, request.getEntity(), context);
80          final Header header = request.getFirstHeader(HttpHeaders.CONNECTION);
81          Assert.assertNotNull(header);
82          Assert.assertEquals("close", header.getValue());
83          Assert.assertTrue(header == myheader);
84      }
85  
86      @Test
87      public void testRequestConnControlUpgrade() throws Exception {
88          final HttpContext context = new BasicHttpContext(null);
89          final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
90          request.addHeader(HttpHeaders.UPGRADE, "HTTP/2");
91          final RequestConnControl interceptor = new RequestConnControl();
92          interceptor.process(request, request.getEntity(), context);
93          final Header header = request.getFirstHeader(HttpHeaders.CONNECTION);
94          Assert.assertNotNull(header);
95          Assert.assertEquals("upgrade", header.getValue());
96      }
97  
98      @Test
99      public void testRequestConnControlInvalidInput() throws Exception {
100         final RequestConnControl interceptor = new RequestConnControl();
101         try {
102             interceptor.process(null, null, null);
103             Assert.fail("IllegalArgumentException should have been thrown");
104         } catch (final IllegalArgumentException ex) {
105             // expected
106         }
107     }
108 
109     @Test
110     public void testRequestContentProtocolException() throws Exception {
111         final HttpContext context = new BasicHttpContext(null);
112         final BasicClassicHttpRequest request1 = new BasicClassicHttpRequest("POST", "/");
113         request1.addHeader(new BasicHeader(HttpHeaders.TRANSFER_ENCODING, "chunked"));
114         final BasicClassicHttpRequest request2 = new BasicClassicHttpRequest("POST", "/");
115         request2.addHeader(new BasicHeader(HttpHeaders.CONTENT_LENGTH, "12"));
116 
117         final RequestContent interceptor = new RequestContent();
118         try {
119             interceptor.process(request1, request1.getEntity(), context);
120             Assert.fail("ProtocolException should have been thrown");
121         } catch (final ProtocolException ex) {
122             // expected
123         }
124         try {
125             interceptor.process(request2, request2.getEntity(), context);
126             Assert.fail("ProtocolException should have been thrown");
127         } catch (final ProtocolException ex) {
128             // expected
129         }
130    }
131 
132     @Test
133     public void testRequestContentNullEntity() throws Exception {
134         final HttpContext context = new BasicHttpContext(null);
135         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
136 
137         final RequestContent interceptor = new RequestContent();
138         interceptor.process(request, request.getEntity(), context);
139         final Header header = request.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
140         Assert.assertNull(header);
141         Assert.assertNull(request.getFirstHeader(HttpHeaders.TRANSFER_ENCODING));
142    }
143 
144     @Test
145     public void testRequestContentEntityContentLengthDelimitedHTTP11() throws Exception {
146         final HttpContext context = new BasicHttpContext(null);
147         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
148         final String s = "whatever";
149         final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII);
150         request.setEntity(entity);
151 
152         final RequestContent interceptor = new RequestContent();
153         interceptor.process(request, request.getEntity(), context);
154         final Header header = request.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
155         Assert.assertNotNull(header);
156         Assert.assertEquals(s.length(), Integer.parseInt(header.getValue()));
157         Assert.assertNull(request.getFirstHeader(HttpHeaders.TRANSFER_ENCODING));
158    }
159 
160     @Test
161     public void testRequestContentEntityChunkedHTTP11() throws Exception {
162         final HttpContext context = new BasicHttpContext(null);
163         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
164         final String s = "whatever";
165         final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII);
166         entity.setChunked(true);
167         request.setEntity(entity);
168 
169         final RequestContent interceptor = new RequestContent();
170         interceptor.process(request, request.getEntity(), context);
171         final Header header = request.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
172         Assert.assertNotNull(header);
173         Assert.assertEquals("chunked", header.getValue());
174         Assert.assertNull(request.getFirstHeader(HttpHeaders.CONTENT_LENGTH));
175    }
176 
177     @Test
178     public void testRequestContentEntityUnknownLengthHTTP11() throws Exception {
179         final HttpContext context = new BasicHttpContext(null);
180         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
181         final BasicHttpEntity entity = new BasicHttpEntity();
182         entity.setContentLength(-1);
183         entity.setChunked(false);
184         request.setEntity(entity);
185 
186         final RequestContent interceptor = new RequestContent();
187         interceptor.process(request, request.getEntity(), context);
188         final Header header = request.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
189         Assert.assertNotNull(header);
190         Assert.assertEquals("chunked", header.getValue());
191         Assert.assertNull(request.getFirstHeader(HttpHeaders.CONTENT_LENGTH));
192     }
193 
194     @Test
195     public void testRequestContentEntityChunkedHTTP10() throws Exception {
196         final HttpContext context = new BasicHttpContext(null);
197         context.setProtocolVersion(HttpVersion.HTTP_1_0);
198         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
199         final String s = "whatever";
200         final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII);
201         entity.setChunked(true);
202         request.setEntity(entity);
203 
204         final RequestContent interceptor = new RequestContent();
205         try {
206             interceptor.process(request, request.getEntity(), context);
207             Assert.fail("ProtocolException should have been thrown");
208         } catch (final ProtocolException ex) {
209             // expected
210         }
211     }
212 
213     @Test
214     public void testRequestContentTypeAndEncoding() throws Exception {
215         final HttpContext context = new BasicHttpContext(null);
216         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
217         final BasicHttpEntity entity = new BasicHttpEntity();
218         entity.setContentType("whatever");
219         entity.setContentEncoding("whatever");
220         request.setEntity(entity);
221 
222         final RequestContent interceptor = new RequestContent();
223         interceptor.process(request, request.getEntity(), context);
224         final Header h1 = request.getFirstHeader(HttpHeaders.CONTENT_TYPE);
225         Assert.assertNotNull(h1);
226         Assert.assertEquals("whatever", h1.getValue());
227         final Header h2 = request.getFirstHeader(HttpHeaders.CONTENT_ENCODING);
228         Assert.assertNotNull(h2);
229         Assert.assertEquals("whatever", h2.getValue());
230     }
231 
232     @Test
233     public void testRequestContentNullTypeAndEncoding() throws Exception {
234         final HttpContext context = new BasicHttpContext(null);
235         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
236         final BasicHttpEntity entity = new BasicHttpEntity();
237         request.setEntity(entity);
238 
239         final RequestContent interceptor = new RequestContent();
240         interceptor.process(request, request.getEntity(), context);
241         Assert.assertNull(request.getFirstHeader(HttpHeaders.CONTENT_TYPE));
242         Assert.assertNull(request.getFirstHeader(HttpHeaders.CONTENT_ENCODING));
243     }
244 
245     @Test
246     public void testRequestContentEntityUnknownLengthHTTP10() throws Exception {
247         final HttpContext context = new BasicHttpContext(null);
248         context.setProtocolVersion(HttpVersion.HTTP_1_0);
249         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
250         final BasicHttpEntity entity = new BasicHttpEntity();
251         entity.setContentLength(-1);
252         entity.setChunked(false);
253         request.setEntity(entity);
254 
255         final RequestContent interceptor = new RequestContent();
256         try {
257             interceptor.process(request, request.getEntity(), context);
258             Assert.fail("ProtocolException should have been thrown");
259         } catch (final ProtocolException ex) {
260             // expected
261         }
262    }
263 
264     @Test
265     public void testRequestContentInvalidInput() throws Exception {
266         final RequestContent interceptor = new RequestContent();
267         try {
268             interceptor.process(null, null, null);
269             Assert.fail("IllegalArgumentException should have been thrown");
270         } catch (final IllegalArgumentException ex) {
271             // expected
272         }
273     }
274 
275     @Test
276     public void testRequestContentIgnoreNonenclosingRequests() throws Exception {
277         final HttpContext context = new BasicHttpContext(null);
278         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
279         final RequestContent interceptor = new RequestContent();
280         interceptor.process(request, request.getEntity(), context);
281         Assert.assertEquals(0, request.getAllHeaders().length);
282     }
283 
284     @Test
285     public void testRequestContentOverwriteHeaders() throws Exception {
286         final RequestContent interceptor = new RequestContent(true);
287         final HttpContext context = new BasicHttpContext(null);
288         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
289         request.addHeader(new BasicHeader(HttpHeaders.CONTENT_LENGTH, "10"));
290         request.addHeader(new BasicHeader(HttpHeaders.TRANSFER_ENCODING, "whatever"));
291         request.setEntity(new StringEntity(""));
292         interceptor.process(request, request.getEntity(), context);
293         final Header h1 = request.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
294         Assert.assertNotNull(h1);
295         Assert.assertEquals("0", h1.getValue());
296     }
297 
298     @Test
299     public void testRequestContentAddHeaders() throws Exception {
300         final RequestContent interceptor = new RequestContent(true);
301         final HttpContext context = new BasicHttpContext(null);
302         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
303         request.setEntity(new StringEntity(""));
304         interceptor.process(request, request.getEntity(), context);
305         final Header h1 = request.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
306         Assert.assertNotNull(h1);
307         Assert.assertEquals("0", h1.getValue());
308         final Header h2 = request.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
309         Assert.assertNull(h2);
310     }
311 
312     @Test
313     public void testRequestContentEntityWithTrailers() throws Exception {
314         final HttpContext context = new BasicHttpContext(null);
315         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
316         final String s = "whatever";
317         final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII);
318         request.setEntity(new HttpEntityWithTrailers(entity,
319                 new BasicHeader("h1", "this"), new BasicHeader("h1", "that"), new BasicHeader("h2", "this and that")));
320 
321         final RequestContent interceptor = new RequestContent();
322         interceptor.process(request, request.getEntity(), context);
323         final Header header1 = request.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
324         Assert.assertNotNull(header1);
325         final Header header2 = request.getFirstHeader(HttpHeaders.TRAILER);
326         Assert.assertNotNull(header2);
327         Assert.assertEquals("h1, h2", header2.getValue());
328     }
329 
330     @Test
331     public void testRequestExpectContinueGenerated() throws Exception {
332         final HttpCoreContext context = HttpCoreContext.create();
333         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
334         final String s = "whatever";
335         final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII);
336         request.setEntity(entity);
337         final RequestExpectContinue interceptor = new RequestExpectContinue();
338         interceptor.process(request, request.getEntity(), context);
339         final Header header = request.getFirstHeader(HttpHeaders.EXPECT);
340         Assert.assertNotNull(header);
341         Assert.assertEquals(HeaderElements.CONTINUE, header.getValue());
342     }
343 
344     @Test
345     public void testRequestExpectContinueHTTP10() throws Exception {
346         final HttpCoreContext context = HttpCoreContext.create();
347         context.setProtocolVersion(HttpVersion.HTTP_1_0);
348         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
349         final String s = "whatever";
350         final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII);
351         request.setEntity(entity);
352         final RequestExpectContinue interceptor = new RequestExpectContinue();
353         interceptor.process(request, request.getEntity(), context);
354         final Header header = request.getFirstHeader(HttpHeaders.EXPECT);
355         Assert.assertNull(header);
356     }
357 
358     @Test
359     public void testRequestExpectContinueZeroContent() throws Exception {
360         final HttpCoreContext context = HttpCoreContext.create();
361         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
362         final String s = "";
363         final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII);
364         request.setEntity(entity);
365         final RequestExpectContinue interceptor = new RequestExpectContinue();
366         interceptor.process(request, request.getEntity(), context);
367         final Header header = request.getFirstHeader(HttpHeaders.EXPECT);
368         Assert.assertNull(header);
369     }
370 
371     @Test
372     public void testRequestExpectContinueInvalidInput() throws Exception {
373         final RequestExpectContinue interceptor = new RequestExpectContinue();
374         try {
375             interceptor.process(null, null, null);
376             Assert.fail("IllegalArgumentException should have been thrown");
377         } catch (final IllegalArgumentException ex) {
378             // expected
379         }
380     }
381 
382     @Test
383     public void testRequestExpectContinueIgnoreNonenclosingRequests() throws Exception {
384         final HttpContext context = new BasicHttpContext(null);
385         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
386         final RequestExpectContinue interceptor = new RequestExpectContinue();
387         interceptor.process(request, request.getEntity(), context);
388         Assert.assertEquals(0, request.getAllHeaders().length);
389     }
390 
391     @Test
392     public void testRequestTargetHostGenerated() throws Exception {
393         final HttpContext context = new BasicHttpContext(null);
394         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
395         request.setAuthority(new URIAuthority("somehost", 8080));
396         final RequestTargetHost interceptor = new RequestTargetHost();
397         interceptor.process(request, request.getEntity(), context);
398         final Header header = request.getFirstHeader(HttpHeaders.HOST);
399         Assert.assertNotNull(header);
400         Assert.assertEquals("somehost:8080", header.getValue());
401     }
402 
403     @Test
404     public void testRequestTargetHostNotGenerated() throws Exception {
405         final HttpContext context = new BasicHttpContext(null);
406         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
407         request.setAuthority(new URIAuthority("somehost", 8080));
408         request.addHeader(new BasicHeader(HttpHeaders.HOST, "whatever"));
409         final RequestTargetHost interceptor = new RequestTargetHost();
410         interceptor.process(request, request.getEntity(), context);
411         final Header header = request.getFirstHeader(HttpHeaders.HOST);
412         Assert.assertNotNull(header);
413         Assert.assertEquals("whatever", header.getValue());
414     }
415 
416     @Test
417     public void testRequestTargetHostMissingHostHTTP10() throws Exception {
418         final HttpContext context = new BasicHttpContext(null);
419         context.setProtocolVersion(HttpVersion.HTTP_1_0);
420         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
421         final RequestTargetHost interceptor = new RequestTargetHost();
422         interceptor.process(request, request.getEntity(), context);
423         final Header header = request.getFirstHeader(HttpHeaders.HOST);
424         Assert.assertNull(header);
425     }
426 
427     @Test
428     public void testRequestTargetHostMissingHostHTTP11() throws Exception {
429         final HttpContext context = new BasicHttpContext(null);
430         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
431         final RequestTargetHost interceptor = new RequestTargetHost();
432         try {
433             interceptor.process(request, request.getEntity(), context);
434             Assert.fail("ProtocolException should have been thrown");
435         } catch (final ProtocolException ex) {
436             // expected
437         }
438     }
439 
440     @Test
441     public void testRequestTargetHostInvalidInput() throws Exception {
442         final RequestTargetHost interceptor = new RequestTargetHost();
443         try {
444             interceptor.process(null, null, null);
445             Assert.fail("IllegalArgumentException should have been thrown");
446         } catch (final IllegalArgumentException ex) {
447             // expected
448         }
449         try {
450             interceptor.process(new BasicClassicHttpRequest("GET", "/"), null, null);
451             Assert.fail("IllegalArgumentException should have been thrown");
452         } catch (final IllegalArgumentException ex) {
453             // expected
454         }
455     }
456 
457     @Test
458     public void testRequestTargetHostConnectHttp11() throws Exception {
459         final HttpContext context = new BasicHttpContext(null);
460         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("CONNECT", "/");
461         request.setAuthority(new URIAuthority("somehost", 8080));
462         final RequestTargetHost interceptor = new RequestTargetHost();
463         interceptor.process(request, request.getEntity(), context);
464         final Header header = request.getFirstHeader(HttpHeaders.HOST);
465         Assert.assertNotNull(header);
466         Assert.assertEquals("somehost:8080", header.getValue());
467     }
468 
469     @Test
470     public void testRequestTargetHostConnectHttp10() throws Exception {
471         final HttpContext context = new BasicHttpContext(null);
472         context.setProtocolVersion(HttpVersion.HTTP_1_0);
473         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("CONNECT", "/");
474         request.setAuthority(new URIAuthority("somehost", 8080));
475         final RequestTargetHost interceptor = new RequestTargetHost();
476         interceptor.process(request, request.getEntity(), context);
477         final Header header = request.getFirstHeader(HttpHeaders.HOST);
478         Assert.assertNull(header);
479     }
480 
481     @Test
482     public void testRequestUserAgentGenerated() throws Exception {
483         final HttpContext context = new BasicHttpContext(null);
484         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
485         final RequestUserAgent interceptor = new RequestUserAgent("some agent");
486         interceptor.process(request, request.getEntity(), context);
487         final Header header = request.getFirstHeader(HttpHeaders.USER_AGENT);
488         Assert.assertNotNull(header);
489         Assert.assertEquals("some agent", header.getValue());
490     }
491 
492     @Test
493     public void testRequestUserAgentNotGenerated() throws Exception {
494         final HttpContext context = new BasicHttpContext(null);
495         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
496         request.addHeader(new BasicHeader(HttpHeaders.USER_AGENT, "whatever"));
497         final RequestUserAgent interceptor = new RequestUserAgent("some agent");
498         interceptor.process(request, request.getEntity(), context);
499         final Header header = request.getFirstHeader(HttpHeaders.USER_AGENT);
500         Assert.assertNotNull(header);
501         Assert.assertEquals("whatever", header.getValue());
502     }
503 
504     @Test
505     public void testRequestUserAgentMissingUserAgent() throws Exception {
506         final HttpContext context = new BasicHttpContext(null);
507         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
508         final RequestUserAgent interceptor = new RequestUserAgent();
509         interceptor.process(request, request.getEntity(), context);
510         final Header header = request.getFirstHeader(HttpHeaders.USER_AGENT);
511         Assert.assertNull(header);
512     }
513 
514     @Test
515     public void testRequestUserAgentInvalidInput() throws Exception {
516         final RequestUserAgent interceptor = new RequestUserAgent();
517         try {
518             interceptor.process(null, null, null);
519             Assert.fail("IllegalArgumentException should have been thrown");
520         } catch (final IllegalArgumentException ex) {
521             // expected
522         }
523     }
524 
525     @Test
526     public void testResponseConnControlNoEntity() throws Exception {
527         final HttpContext context = new BasicHttpContext(null);
528         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
529         final ResponseConnControl interceptor = new ResponseConnControl();
530         interceptor.process(response, response.getEntity(), context);
531         final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
532         Assert.assertNull(header);
533     }
534 
535     @Test
536     public void testResponseConnControlEntityContentLength() throws Exception {
537         final HttpContext context = new BasicHttpContext(null);
538         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
539         final StringEntity entity = new StringEntity("whatever");
540         response.setEntity(entity);
541         final ResponseConnControl interceptor = new ResponseConnControl();
542         interceptor.process(response, response.getEntity(), context);
543         final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
544         Assert.assertNull(header);
545     }
546 
547     @Test
548     public void testResponseConnControlEntityUnknownContentLengthExplicitKeepAlive() throws Exception {
549         final HttpContext context = new BasicHttpContext(null);
550         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
551         request.addHeader(new BasicHeader(HttpHeaders.CONNECTION, "keep-alive"));
552         context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
553         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
554         final BasicHttpEntity entity = new BasicHttpEntity();
555         response.setEntity(entity);
556         final ResponseConnControl interceptor = new ResponseConnControl();
557         interceptor.process(response, response.getEntity(), context);
558         final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
559         Assert.assertNotNull(header);
560         Assert.assertEquals("keep-alive", header.getValue());
561     }
562 
563     @Test
564     public void testResponseConnControlEntityChunked() throws Exception {
565         final HttpContext context = new BasicHttpContext(null);
566         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
567         final BasicHttpEntity entity = new BasicHttpEntity();
568         entity.setChunked(true);
569         response.setEntity(entity);
570         final ResponseConnControl interceptor = new ResponseConnControl();
571         interceptor.process(response, response.getEntity(), context);
572         final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
573         Assert.assertNull(header);
574     }
575 
576     @Test
577     public void testResponseConnControlEntityUnknownContentLengthHTTP10() throws Exception {
578         final HttpContext context = new BasicHttpContext(null);
579         context.setProtocolVersion(HttpVersion.HTTP_1_0);
580         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
581         request.addHeader(new BasicHeader(HttpHeaders.CONNECTION, "keep-alive"));
582         context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
583 
584         final BasicClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
585         final BasicHttpEntity entity = new BasicHttpEntity();
586         response.setEntity(entity);
587         final ResponseConnControl interceptor = new ResponseConnControl();
588         interceptor.process(response, response.getEntity(), context);
589         final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
590         Assert.assertNotNull(header);
591         Assert.assertEquals("close", header.getValue());
592     }
593 
594     @Test
595     public void testResponseConnControlClientRequest() throws Exception {
596         final HttpContext context = new BasicHttpContext(null);
597         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
598         request.addHeader(new BasicHeader(HttpHeaders.CONNECTION, "keep-alive"));
599         context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
600 
601         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
602         final StringEntity entity = new StringEntity("whatever");
603         response.setEntity(entity);
604         final ResponseConnControl interceptor = new ResponseConnControl();
605         interceptor.process(response, response.getEntity(), context);
606         final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
607         Assert.assertNotNull(header);
608         Assert.assertEquals("keep-alive", header.getValue());
609     }
610 
611     @Test
612     public void testResponseConnControlClientRequest2() throws Exception {
613         final HttpContext context = new BasicHttpContext(null);
614         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
615         context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
616 
617         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
618         final StringEntity entity = new StringEntity("whatever");
619         response.setEntity(entity);
620         final ResponseConnControl interceptor = new ResponseConnControl();
621         interceptor.process(response, response.getEntity(), context);
622         final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
623         Assert.assertNull(header);
624     }
625 
626     @Test
627     public void testResponseConnControl10Client11Response() throws Exception {
628         final HttpContext context = new BasicHttpContext(null);
629         context.setProtocolVersion(HttpVersion.HTTP_1_0);
630         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
631         context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
632 
633         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
634         final StringEntity entity = new StringEntity("whatever");
635         response.setEntity(entity);
636         final ResponseConnControl interceptor = new ResponseConnControl();
637         interceptor.process(response, response.getEntity(), context);
638         final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
639         Assert.assertNotNull(header);
640         Assert.assertEquals("keep-alive", header.getValue());
641     }
642 
643     @Test
644     public void testResponseConnControlStatusCode() throws Exception {
645         final HttpContext context = new BasicHttpContext(null);
646         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
647         request.addHeader(new BasicHeader(HttpHeaders.CONNECTION, "keep-alive"));
648         context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
649 
650         final ResponseConnControl interceptor = new ResponseConnControl();
651 
652         final int [] statusCodes = new int[] {
653                 HttpStatus.SC_BAD_REQUEST,
654                 HttpStatus.SC_REQUEST_TIMEOUT,
655                 HttpStatus.SC_LENGTH_REQUIRED,
656                 HttpStatus.SC_REQUEST_TOO_LONG,
657                 HttpStatus.SC_REQUEST_URI_TOO_LONG,
658                 HttpStatus.SC_SERVICE_UNAVAILABLE,
659                 HttpStatus.SC_NOT_IMPLEMENTED };
660 
661         for (final int statusCode : statusCodes) {
662             final ClassicHttpResponse response = new BasicClassicHttpResponse(statusCode, "Unreasonable");
663             interceptor.process(response, response.getEntity(), context);
664             final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
665             Assert.assertNotNull(header);
666             Assert.assertEquals("close", header.getValue());
667         }
668 
669     }
670 
671     @Test
672     public void testResponseConnControlExplicitClose() throws Exception {
673         final HttpContext context = new BasicHttpContext(null);
674         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
675         request.addHeader(new BasicHeader(HttpHeaders.CONNECTION, "keep-alive"));
676         context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
677 
678         final ResponseConnControl interceptor = new ResponseConnControl();
679 
680         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
681         response.setHeader(HttpHeaders.CONNECTION, "close");
682         interceptor.process(response, response.getEntity(), context);
683         final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
684         Assert.assertNotNull(header);
685         Assert.assertEquals("close", header.getValue());
686     }
687 
688     @Test
689     public void testResponseConnControlClientRequestMixUp() throws Exception {
690         final HttpContext context = new BasicHttpContext(null);
691         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
692         request.addHeader(new BasicHeader(HttpHeaders.CONNECTION, "blah, keep-alive, close"));
693         context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
694 
695         final ResponseConnControl interceptor = new ResponseConnControl();
696 
697         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
698         interceptor.process(response, response.getEntity(), context);
699         final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
700         Assert.assertNotNull(header);
701         Assert.assertEquals("close", header.getValue());
702     }
703 
704     @Test
705     public void testResponseConnControlUpgrade() throws Exception {
706         final HttpContext context = new BasicHttpContext(null);
707 
708         final ResponseConnControl interceptor = new ResponseConnControl();
709 
710         final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
711         response.addHeader(HttpHeaders.UPGRADE, "HTTP/2");
712         interceptor.process(response, response.getEntity(), context);
713         final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
714         Assert.assertNotNull(header);
715         Assert.assertEquals("upgrade", header.getValue());
716     }
717 
718     @Test
719     public void testResponseConnControlHostInvalidInput() throws Exception {
720         final ResponseConnControl interceptor = new ResponseConnControl();
721         try {
722             interceptor.process(null, null, null);
723             Assert.fail("IllegalArgumentException should have been thrown");
724         } catch (final IllegalArgumentException ex) {
725             // expected
726         }
727         try {
728             final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
729             interceptor.process(response, null, null);
730             Assert.fail("IllegalArgumentException should have been thrown");
731         } catch (final IllegalArgumentException ex) {
732             // expected
733         }
734     }
735 
736     @Test
737     public void testResponseContentNoEntity() throws Exception {
738         final HttpContext context = new BasicHttpContext(null);
739         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
740         final ResponseContent interceptor = new ResponseContent();
741         interceptor.process(response, response.getEntity(), context);
742         final Header header = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
743         Assert.assertNotNull(header);
744         Assert.assertEquals("0", header.getValue());
745     }
746 
747     @Test
748     public void testResponseContentStatusNoContent() throws Exception {
749         final HttpContext context = new BasicHttpContext(null);
750         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
751         response.setCode(HttpStatus.SC_NO_CONTENT);
752         final ResponseContent interceptor = new ResponseContent();
753         interceptor.process(response, response.getEntity(), context);
754         final Header header = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
755         Assert.assertNull(header);
756     }
757 
758     @Test
759     public void testResponseContentStatusNotModified() throws Exception {
760         final HttpContext context = new BasicHttpContext(null);
761         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
762         response.setCode(HttpStatus.SC_NOT_MODIFIED);
763         final ResponseContent interceptor = new ResponseContent();
764         interceptor.process(response, response.getEntity(), context);
765         final Header header = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
766         Assert.assertNull(header);
767     }
768 
769     @Test
770     public void testResponseContentEntityChunked() throws Exception {
771         final HttpContext context = new BasicHttpContext(null);
772         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
773         final BasicHttpEntity entity = new BasicHttpEntity();
774         entity.setChunked(true);
775         response.setEntity(entity);
776         final ResponseContent interceptor = new ResponseContent();
777         interceptor.process(response, response.getEntity(), context);
778         final Header h1 = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
779         Assert.assertNotNull(h1);
780         Assert.assertEquals("chunked", h1.getValue());
781         final Header h2 = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
782         Assert.assertNull(h2);
783     }
784 
785     @Test
786     public void testResponseContentEntityContentLenghtDelimited() throws Exception {
787         final HttpContext context = new BasicHttpContext(null);
788         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
789         final BasicHttpEntity entity = new BasicHttpEntity();
790         entity.setContentLength (10);
791         response.setEntity(entity);
792         final ResponseContent interceptor = new ResponseContent();
793         interceptor.process(response, response.getEntity(), context);
794         final Header h1 = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
795         Assert.assertNotNull(h1);
796         Assert.assertEquals("10", h1.getValue());
797         final Header h2 = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
798         Assert.assertNull(h2);
799     }
800 
801     @Test
802     public void testResponseContentEntityUnknownContentLength() throws Exception {
803         final HttpContext context = new BasicHttpContext(null);
804         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
805         final BasicHttpEntity entity = new BasicHttpEntity();
806         response.setEntity(entity);
807         final ResponseContent interceptor = new ResponseContent();
808         interceptor.process(response, response.getEntity(), context);
809         final Header h1 = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
810         Assert.assertNotNull(h1);
811         Assert.assertEquals("chunked", h1.getValue());
812         final Header h2 = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
813         Assert.assertNull(h2);
814     }
815 
816     @Test
817     public void testResponseContentEntityChunkedHTTP10() throws Exception {
818         final HttpContext context = new BasicHttpContext(null);
819         context.setProtocolVersion(HttpVersion.HTTP_1_0);
820         final BasicClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
821         final BasicHttpEntity entity = new BasicHttpEntity();
822         entity.setChunked(true);
823         response.setEntity(entity);
824         final ResponseContent interceptor = new ResponseContent();
825         interceptor.process(response, response.getEntity(), context);
826         final Header h1 = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
827         Assert.assertNull(h1);
828         final Header h2 = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
829         Assert.assertNull(h2);
830     }
831 
832     @Test
833     public void testResponseContentEntityNoContentTypeAndEncoding() throws Exception {
834         final HttpContext context = new BasicHttpContext(null);
835         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
836         final BasicHttpEntity entity = new BasicHttpEntity();
837         response.setEntity(entity);
838         final ResponseContent interceptor = new ResponseContent();
839         interceptor.process(response, response.getEntity(), context);
840         final Header h1 = response.getFirstHeader(HttpHeaders.CONTENT_TYPE);
841         Assert.assertNull(h1);
842         final Header h2 = response.getFirstHeader(HttpHeaders.CONTENT_ENCODING);
843         Assert.assertNull(h2);
844     }
845 
846     @Test
847     public void testResponseContentEntityContentTypeAndEncoding() throws Exception {
848         final HttpContext context = new BasicHttpContext(null);
849         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
850         final BasicHttpEntity entity = new BasicHttpEntity();
851         entity.setContentEncoding("whatever");
852         entity.setContentType("whatever");
853         response.setEntity(entity);
854         final ResponseContent interceptor = new ResponseContent();
855         interceptor.process(response, response.getEntity(), context);
856         final Header h1 = response.getFirstHeader(HttpHeaders.CONTENT_TYPE);
857         Assert.assertNotNull(h1);
858         Assert.assertEquals("whatever", h1.getValue());
859         final Header h2 = response.getFirstHeader(HttpHeaders.CONTENT_ENCODING);
860         Assert.assertNotNull(h2);
861         Assert.assertEquals("whatever", h2.getValue());
862     }
863 
864     @Test
865     public void testResponseContentInvalidInput() throws Exception {
866         final ResponseContent interceptor = new ResponseContent();
867         try {
868             interceptor.process(null, null, null);
869             Assert.fail("IllegalArgumentException should have been thrown");
870         } catch (final IllegalArgumentException ex) {
871             // expected
872         }
873     }
874 
875     @Test
876     public void testResponseContentInvalidResponseState() throws Exception {
877         final ResponseContent interceptor = new ResponseContent();
878         final HttpContext context = new BasicHttpContext(null);
879         try {
880             final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
881             response.addHeader(new BasicHeader(HttpHeaders.CONTENT_LENGTH, "10"));
882             interceptor.process(response, response.getEntity(), context);
883             Assert.fail("ProtocolException should have been thrown");
884         } catch (final ProtocolException ex) {
885             // expected
886         }
887         try {
888             final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
889             response.addHeader(new BasicHeader(HttpHeaders.TRANSFER_ENCODING, "stuff"));
890             interceptor.process(response, response.getEntity(), context);
891             Assert.fail("ProtocolException should have been thrown");
892         } catch (final ProtocolException ex) {
893             // expected
894         }
895     }
896 
897     @Test
898     public void testResponseContentOverwriteHeaders() throws Exception {
899         final ResponseContent interceptor = new ResponseContent(true);
900         final HttpContext context = new BasicHttpContext(null);
901         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
902         response.addHeader(new BasicHeader(HttpHeaders.CONTENT_LENGTH, "10"));
903         response.addHeader(new BasicHeader(HttpHeaders.TRANSFER_ENCODING, "whatever"));
904         interceptor.process(response, response.getEntity(), context);
905         Assert.assertEquals("0", response.getFirstHeader(HttpHeaders.CONTENT_LENGTH).getValue());
906     }
907 
908     @Test
909     public void testResponseContentAddHeaders() throws Exception {
910         final ResponseContent interceptor = new ResponseContent(true);
911         final HttpContext context = new BasicHttpContext(null);
912         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
913         interceptor.process(response, response.getEntity(), context);
914         Assert.assertEquals("0", response.getFirstHeader(HttpHeaders.CONTENT_LENGTH).getValue());
915         Assert.assertNull(response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING));
916     }
917 
918     @Test
919     public void testResponseContentEntityWithTrailers() throws Exception {
920         final HttpContext context = new BasicHttpContext(null);
921         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
922         final String s = "whatever";
923         final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII);
924         response.setEntity(new HttpEntityWithTrailers(entity,
925                 new BasicHeader("h1", "this"), new BasicHeader("h1", "that"), new BasicHeader("h2", "this and that")));
926 
927         final ResponseContent interceptor = new ResponseContent();
928         interceptor.process(response, response.getEntity(), context);
929         final Header header1 = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
930         Assert.assertNotNull(header1);
931         final Header header2 = response.getFirstHeader(HttpHeaders.TRAILER);
932         Assert.assertNotNull(header2);
933         Assert.assertEquals("h1, h2", header2.getValue());
934     }
935 
936     @Test
937     public void testResponseDateGenerated() throws Exception {
938         final HttpContext context = new BasicHttpContext(null);
939         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
940         final ResponseDate interceptor = new ResponseDate();
941         interceptor.process(response, response.getEntity(), context);
942         final Header h1 = response.getFirstHeader(HttpHeaders.DATE);
943         Assert.assertNotNull(h1);
944         interceptor.process(response, response.getEntity(), context);
945         final Header h2 = response.getFirstHeader(HttpHeaders.DATE);
946         Assert.assertNotNull(h2);
947     }
948 
949     @Test
950     public void testResponseDateNotGenerated() throws Exception {
951         final HttpContext context = new BasicHttpContext(null);
952         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
953         response.setCode(199);
954         final ResponseDate interceptor = new ResponseDate();
955         interceptor.process(response, response.getEntity(), context);
956         final Header h1 = response.getFirstHeader(HttpHeaders.DATE);
957         Assert.assertNull(h1);
958     }
959 
960     @Test
961     public void testResponseDateInvalidInput() throws Exception {
962         final ResponseDate interceptor = new ResponseDate();
963         try {
964             interceptor.process(null, null, null);
965             Assert.fail("IllegalArgumentException should have been thrown");
966         } catch (final IllegalArgumentException ex) {
967             // expected
968         }
969     }
970 
971     @Test
972     public void testRequestDateGenerated() throws Exception {
973         final HttpContext context = new BasicHttpContext(null);
974         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
975         request.setEntity(new StringEntity("stuff"));
976 
977         final RequestDate interceptor = new RequestDate();
978         interceptor.process(request, request.getEntity(), context);
979         final Header h1 = request.getFirstHeader(HttpHeaders.DATE);
980         Assert.assertNotNull(h1);
981         interceptor.process(request, request.getEntity(), context);
982         final Header h2 = request.getFirstHeader(HttpHeaders.DATE);
983         Assert.assertNotNull(h2);
984     }
985 
986     @Test
987     public void testRequestDateNotGenerated() throws Exception {
988         final HttpContext context = new BasicHttpContext(null);
989         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
990 
991         final RequestDate interceptor = new RequestDate();
992         interceptor.process(request, request.getEntity(), context);
993         final Header h1 = request.getFirstHeader(HttpHeaders.DATE);
994         Assert.assertNull(h1);
995     }
996 
997     @Test
998     public void testRequestDateInvalidInput() throws Exception {
999         final RequestDate interceptor = new RequestDate();
1000         try {
1001             interceptor.process(null, null, null);
1002             Assert.fail("IllegalArgumentException should have been thrown");
1003         } catch (final IllegalArgumentException ex) {
1004             // expected
1005         }
1006     }
1007 
1008     @Test
1009     public void testResponseServerGenerated() throws Exception {
1010         final HttpContext context = new BasicHttpContext(null);
1011         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
1012         final ResponseServer interceptor = new ResponseServer("some server");
1013         interceptor.process(response, response.getEntity(), context);
1014         final Header h1 = response.getFirstHeader(HttpHeaders.SERVER);
1015         Assert.assertNotNull(h1);
1016         Assert.assertEquals("some server", h1.getValue());
1017     }
1018 
1019     @Test
1020     public void testResponseServerNotGenerated() throws Exception {
1021         final HttpContext context = new BasicHttpContext(null);
1022         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
1023         response.addHeader(new BasicHeader(HttpHeaders.SERVER, "whatever"));
1024         final ResponseServer interceptor = new ResponseServer("some server");
1025         interceptor.process(response, response.getEntity(), context);
1026         final Header h1 = response.getFirstHeader(HttpHeaders.SERVER);
1027         Assert.assertNotNull(h1);
1028         Assert.assertEquals("whatever", h1.getValue());
1029     }
1030 
1031     @Test
1032     public void testResponseServerMissing() throws Exception {
1033         final HttpContext context = new BasicHttpContext(null);
1034         final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
1035         final ResponseServer interceptor = new ResponseServer();
1036         interceptor.process(response, response.getEntity(), context);
1037         final Header h1 = response.getFirstHeader(HttpHeaders.SERVER);
1038         Assert.assertNull(h1);
1039     }
1040 
1041     @Test
1042     public void testResponseServerInvalidInput() throws Exception {
1043         final ResponseServer interceptor = new ResponseServer();
1044         try {
1045             interceptor.process(null, null, null);
1046             Assert.fail("IllegalArgumentException should have been thrown");
1047         } catch (final IllegalArgumentException ex) {
1048             // expected
1049         }
1050     }
1051 
1052     @Test
1053     public void testRequestHttp10HostHeaderAbsent() throws Exception {
1054         final HttpContext context = new BasicHttpContext(null);
1055         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
1056         request.setVersion(HttpVersion.HTTP_1_0);
1057         final RequestValidateHost interceptor = new RequestValidateHost();
1058         interceptor.process(request, request.getEntity(), context);
1059     }
1060 
1061     @Test
1062     public void testRequestHttpHostHeader() throws Exception {
1063         final HttpContext context = new BasicHttpContext(null);
1064         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
1065         request.setVersion(HttpVersion.HTTP_1_1);
1066         request.setHeader(HttpHeaders.HOST, "host:8888");
1067         final RequestValidateHost interceptor = new RequestValidateHost();
1068         interceptor.process(request, request.getEntity(), context);
1069         Assert.assertEquals(new URIAuthority("host", 8888), request.getAuthority());
1070     }
1071 
1072     @Test
1073     public void testRequestHttpHostHeaderNoPort() throws Exception {
1074         final HttpContext context = new BasicHttpContext(null);
1075         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
1076         request.setVersion(HttpVersion.HTTP_1_1);
1077         request.setHeader(HttpHeaders.HOST, "host");
1078         final RequestValidateHost interceptor = new RequestValidateHost();
1079         interceptor.process(request, request.getEntity(), context);
1080         Assert.assertEquals(new URIAuthority("host"), request.getAuthority());
1081     }
1082 
1083     @Test
1084     public void testRequestHttp11HostHeaderPresent() throws Exception {
1085         final HttpContext context = new BasicHttpContext(null);
1086         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
1087         request.setHeader(HttpHeaders.HOST, "blah");
1088         final RequestValidateHost interceptor = new RequestValidateHost();
1089         interceptor.process(request, request.getEntity(), context);
1090     }
1091 
1092     @Test(expected = ProtocolException.class)
1093     public void testRequestHttp11HostHeaderAbsent() throws Exception {
1094         final HttpContext context = new BasicHttpContext(null);
1095         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
1096         final RequestValidateHost interceptor = new RequestValidateHost();
1097         interceptor.process(request, request.getEntity(), context);
1098     }
1099 
1100     @Test(expected = ProtocolException.class)
1101     public void testRequestHttp11MultipleHostHeaders() throws Exception {
1102         final HttpContext context = new BasicHttpContext(null);
1103         final BasicClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
1104         request.addHeader(HttpHeaders.HOST, "blah");
1105         request.addHeader(HttpHeaders.HOST, "blah");
1106         final RequestValidateHost interceptor = new RequestValidateHost();
1107         interceptor.process(request, request.getEntity(), context);
1108     }
1109 
1110 }