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.client5.http.protocol;
29  
30  import javax.net.ssl.SSLSession;
31  
32  import org.apache.hc.client5.http.HeadersMatcher;
33  import org.apache.hc.client5.http.config.RequestConfig;
34  import org.apache.hc.core5.http.HttpHeaders;
35  import org.apache.hc.core5.http.HttpRequest;
36  import org.apache.hc.core5.http.HttpVersion;
37  import org.apache.hc.core5.http.message.BasicHeader;
38  import org.apache.hc.core5.http.message.BasicHttpRequest;
39  import org.hamcrest.MatcherAssert;
40  import org.junit.jupiter.api.Assertions;
41  import org.junit.jupiter.api.BeforeEach;
42  import org.junit.jupiter.api.Test;
43  import org.mockito.Mockito;
44  
45  class TestRequestUpgrade {
46  
47      private RequestUpgrade interceptor;
48      private HttpClientContext context;
49  
50      @BeforeEach
51      void setUp() {
52          interceptor = new RequestUpgrade();
53          context = HttpClientContext.create();
54      }
55  
56      @Test
57      void testUpgrade() throws Exception {
58          final HttpRequest get = new BasicHttpRequest("GET", "/");
59          interceptor.process(get, null, context);
60          MatcherAssert.assertThat(get.getHeaders(),
61                  HeadersMatcher.same(
62                          new BasicHeader(HttpHeaders.UPGRADE, "TLS/1.2"),
63                          new BasicHeader(HttpHeaders.CONNECTION, HttpHeaders.UPGRADE)));
64          final HttpRequest options = new BasicHttpRequest("OPTIONS", "/");
65          interceptor.process(options, null, context);
66          MatcherAssert.assertThat(options.getHeaders(),
67                  HeadersMatcher.same(
68                          new BasicHeader(HttpHeaders.UPGRADE, "TLS/1.2"),
69                          new BasicHeader(HttpHeaders.CONNECTION, HttpHeaders.UPGRADE)));
70          final HttpRequest head = new BasicHttpRequest("HEAD", "/");
71          interceptor.process(head, null, context);
72          MatcherAssert.assertThat(head.getHeaders(),
73                  HeadersMatcher.same(
74                          new BasicHeader(HttpHeaders.UPGRADE, "TLS/1.2"),
75                          new BasicHeader(HttpHeaders.CONNECTION, HttpHeaders.UPGRADE)));
76      }
77  
78      @Test
79      void testUpgradeDisabled() throws Exception {
80          context.setRequestConfig(RequestConfig.custom()
81                  .setProtocolUpgradeEnabled(false)
82                  .build());
83          final HttpRequest get = new BasicHttpRequest("GET", "/");
84          interceptor.process(get, null, context);
85          Assertions.assertFalse(get.containsHeader(HttpHeaders.UPGRADE));
86      }
87  
88      @Test
89      void testDoNotUpgradeHTTP2() throws Exception {
90          context.setProtocolVersion(HttpVersion.HTTP_2);
91          final HttpRequest get = new BasicHttpRequest("GET", "/");
92          interceptor.process(get, null, context);
93          Assertions.assertFalse(get.containsHeader(HttpHeaders.UPGRADE));
94      }
95  
96      @Test
97      void testDoNotUpgradeHTTP10() throws Exception {
98          context.setProtocolVersion(HttpVersion.HTTP_1_0);
99          final HttpRequest get = new BasicHttpRequest("GET", "/");
100         interceptor.process(get, null, context);
101         Assertions.assertFalse(get.containsHeader(HttpHeaders.UPGRADE));
102     }
103 
104     @Test
105     void testDoUpgradeIfAlreadyTLS() throws Exception {
106         context.setSSLSession(Mockito.mock(SSLSession.class));
107         final HttpRequest get = new BasicHttpRequest("GET", "/");
108         interceptor.process(get, null, context);
109         Assertions.assertFalse(get.containsHeader(HttpHeaders.UPGRADE));
110     }
111 
112     @Test
113     void testDoUpgradeNonSafeMethodsOrTrace() throws Exception {
114         final HttpRequest post = new BasicHttpRequest("POST", "/");
115         interceptor.process(post, null, context);
116         Assertions.assertFalse(post.containsHeader(HttpHeaders.UPGRADE));
117 
118         final HttpRequest put = new BasicHttpRequest("PUT", "/");
119         interceptor.process(put, null, context);
120         Assertions.assertFalse(put.containsHeader(HttpHeaders.UPGRADE));
121 
122         final HttpRequest patch = new BasicHttpRequest("PATCH", "/");
123         interceptor.process(patch, null, context);
124         Assertions.assertFalse(patch.containsHeader(HttpHeaders.UPGRADE));
125 
126         final HttpRequest trace = new BasicHttpRequest("TRACE", "/");
127         interceptor.process(trace, null, context);
128         Assertions.assertFalse(trace.containsHeader(HttpHeaders.UPGRADE));
129     }
130 
131 }