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  package org.apache.hc.core5.http.protocol;
28  
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  import static org.junit.jupiter.api.Assertions.assertNotNull;
31  
32  import org.apache.hc.core5.http.HttpHeaders;
33  import org.apache.hc.core5.http.HttpRequestInterceptor;
34  import org.apache.hc.core5.http.HttpVersion;
35  import org.apache.hc.core5.http.Method;
36  import org.apache.hc.core5.http.ProtocolException;
37  import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
38  import org.apache.hc.core5.net.URIAuthority;
39  import org.junit.jupiter.api.Assertions;
40  import org.junit.jupiter.api.Test;
41  
42  
43  public class ViaRequestTest {
44  
45      @Test
46      public void testViaRequestGenerated() throws Exception {
47  
48          final HttpContext context = new BasicHttpContext(null);
49          final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
50          request.setAuthority(new URIAuthority("somehost", 8888));
51          context.setProtocolVersion(HttpVersion.HTTP_1_1);
52          final ViaRequest interceptor = new ViaRequest();
53          interceptor.process(request, request.getEntity(), context);
54  
55          assertEquals(request.getHeader(HttpHeaders.VIA).getName(), HttpHeaders.VIA);
56          assertNotNull(request.getHeader(HttpHeaders.VIA));
57          assertEquals(request.getHeader(HttpHeaders.VIA).getValue(), "HTTP 1.1 somehost:8888");
58  
59      }
60  
61      @Test
62      public void testViaRequestGeneratedWithOutPort() throws Exception {
63  
64          final HttpContext context = new BasicHttpContext(null);
65          final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
66          request.setAuthority(new URIAuthority("somehost"));
67          context.setProtocolVersion(HttpVersion.HTTP_1_1);
68          final ViaRequest interceptor = new ViaRequest();
69          interceptor.process(request, request.getEntity(), context);
70  
71          assertEquals(request.getHeader(HttpHeaders.VIA).getName(), HttpHeaders.VIA);
72          assertNotNull(request.getHeader(HttpHeaders.VIA));
73          assertEquals(request.getHeader(HttpHeaders.VIA).getValue(), "HTTP 1.1 somehost");
74      }
75  
76  
77          @Test
78      public void testViaRequestInvalidHttpVersion() {
79          final HttpContext context = new BasicHttpContext(null);
80          final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
81          context.setProtocolVersion(HttpVersion.HTTP_0_9);
82          request.setAuthority(new URIAuthority("somehost", 8888));
83  
84          final HttpRequestInterceptor interceptor = ViaRequest.INSTANCE;
85          Assertions.assertThrows(ProtocolException.class, () ->
86                  interceptor.process(request, request.getEntity(), context));
87      }
88  
89      @Test
90      public void testViaRequestInvalidAuthority() {
91          final HttpContext context = new BasicHttpContext(null);
92          final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
93          context.setProtocolVersion(HttpVersion.HTTP_1_1);
94  
95          final HttpRequestInterceptor interceptor = ViaRequest.INSTANCE;
96          Assertions.assertThrows(ProtocolException.class, () ->
97                  interceptor.process(request, request.getEntity(), context));
98      }
99  
100     @Test
101     public void testViaRequestNotCreatedAlreadyAdded() throws Exception {
102 
103         final HttpContext context = new BasicHttpContext(null);
104         final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
105         context.setProtocolVersion(HttpVersion.HTTP_1_1);
106         request.setAuthority(new URIAuthority("somehost", 8888));
107         final String viaValue = "HTTP 1.1 host:8888";
108         request.setHeader(HttpHeaders.VIA, viaValue);
109         final ViaRequest interceptor = new ViaRequest();
110         interceptor.process(request, request.getEntity(), context);
111 
112         assertEquals(request.getHeader(HttpHeaders.VIA).getName(), HttpHeaders.VIA);
113         assertNotNull(request.getHeader(HttpHeaders.VIA));
114         assertEquals(request.getHeader(HttpHeaders.VIA).getValue(), viaValue);
115 
116     }
117 }