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.client5.http.impl;
28  
29  import org.apache.hc.core5.http.HttpHeaders;
30  import org.apache.hc.core5.http.HttpResponse;
31  import org.apache.hc.core5.http.HttpStatus;
32  import org.apache.hc.core5.http.ProtocolException;
33  import org.apache.hc.core5.http.message.BasicHttpResponse;
34  import org.apache.hc.core5.http.ssl.TLS;
35  import org.junit.jupiter.api.Assertions;
36  import org.junit.jupiter.api.BeforeEach;
37  import org.junit.jupiter.api.Test;
38  
39  /**
40   * Simple tests for {@link DefaultAuthenticationStrategy}.
41   */
42  public class TestProtocolSwitchStrategy {
43  
44      ProtocolSwitchStrategy switchStrategy;
45  
46      @BeforeEach
47      void setUp() {
48          switchStrategy = new ProtocolSwitchStrategy();
49      }
50  
51      @Test
52      public void testSwitchToTLS() throws Exception {
53          final HttpResponse response1 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
54          response1.addHeader(HttpHeaders.UPGRADE, "TLS");
55          Assertions.assertEquals(TLS.V_1_2.getVersion(), switchStrategy.switchProtocol(response1));
56  
57          final HttpResponse response2 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
58          response2.addHeader(HttpHeaders.UPGRADE, "TLS/1.3");
59          Assertions.assertEquals(TLS.V_1_3.getVersion(), switchStrategy.switchProtocol(response2));
60      }
61  
62      @Test
63      public void testSwitchToHTTP11AndTLS() throws Exception {
64          final HttpResponse response1 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
65          response1.addHeader(HttpHeaders.UPGRADE, "TLS, HTTP/1.1");
66          Assertions.assertEquals(TLS.V_1_2.getVersion(), switchStrategy.switchProtocol(response1));
67  
68          final HttpResponse response2 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
69          response2.addHeader(HttpHeaders.UPGRADE, ",, HTTP/1.1, TLS, ");
70          Assertions.assertEquals(TLS.V_1_2.getVersion(), switchStrategy.switchProtocol(response2));
71  
72          final HttpResponse response3 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
73          response3.addHeader(HttpHeaders.UPGRADE, "HTTP/1.1");
74          response3.addHeader(HttpHeaders.UPGRADE, "TLS/1.2");
75          Assertions.assertEquals(TLS.V_1_2.getVersion(), switchStrategy.switchProtocol(response3));
76  
77          final HttpResponse response4 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
78          response4.addHeader(HttpHeaders.UPGRADE, "HTTP/1.1");
79          response4.addHeader(HttpHeaders.UPGRADE, "TLS/1.2, TLS/1.3");
80          Assertions.assertEquals(TLS.V_1_3.getVersion(), switchStrategy.switchProtocol(response4));
81      }
82  
83      @Test
84      public void testSwitchInvalid() throws Exception {
85          final HttpResponse response1 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
86          response1.addHeader(HttpHeaders.UPGRADE, "Crap");
87          Assertions.assertThrows(ProtocolException.class, () -> switchStrategy.switchProtocol(response1));
88  
89          final HttpResponse response2 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
90          response2.addHeader(HttpHeaders.UPGRADE, "TLS, huh?");
91          Assertions.assertThrows(ProtocolException.class, () -> switchStrategy.switchProtocol(response2));
92  
93          final HttpResponse response3 = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
94          response3.addHeader(HttpHeaders.UPGRADE, ",,,");
95          Assertions.assertThrows(ProtocolException.class, () -> switchStrategy.switchProtocol(response3));
96      }
97  
98  }