2929import org .apache .hc .core5 .http .HttpHeaders ;
3030import org .apache .hc .core5 .http .HttpResponse ;
3131import org .apache .hc .core5 .http .HttpStatus ;
32+ import org .apache .hc .core5 .http .HttpVersion ;
3233import org .apache .hc .core5 .http .ProtocolException ;
34+ import org .apache .hc .core5 .http .ProtocolVersion ;
3335import org .apache .hc .core5 .http .message .BasicHttpResponse ;
3436import org .apache .hc .core5 .http .ssl .TLS ;
3537import org .junit .jupiter .api .Assertions ;
@@ -95,4 +97,102 @@ void testSwitchInvalid() {
9597 Assertions .assertThrows (ProtocolException .class , () -> switchStrategy .switchProtocol (response3 ));
9698 }
9799
100+ @ Test
101+ void testSwitchToTlsValid_TLS_2_0 () throws ProtocolException {
102+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
103+ response .addHeader (HttpHeaders .UPGRADE , "HTTP/2.0" );
104+ final ProtocolVersion result = switchStrategy .switchProtocol (response );
105+ Assertions .assertEquals (HttpVersion .HTTP_2_0 , result );
106+ }
107+
108+ @ Test
109+ void testSwitchToHttpValid_HTTP_1_1 () throws Exception {
110+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
111+ response .addHeader (HttpHeaders .UPGRADE , "HTTP/1.1" );
112+ final ProtocolVersion result = switchStrategy .switchProtocol (response );
113+ Assertions .assertEquals (HttpVersion .HTTP_1_1 , result );
114+ }
115+
116+ @ Test
117+ void testUnsupportedHttpVersion () throws ProtocolException {
118+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
119+ response .addHeader (HttpHeaders .UPGRADE , "HTTP/11.22" );
120+ final ProtocolVersion result = switchStrategy .switchProtocol (response );
121+ Assertions .assertEquals (11 , result .getMajor ());
122+ Assertions .assertEquals (22 , result .getMinor ());
123+ }
124+
125+ @ Test
126+ void testSwitchToTlsValid_TLS_1_2 () throws Exception {
127+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
128+ response .addHeader (HttpHeaders .UPGRADE , "TLS/1.2" );
129+ final ProtocolVersion result = switchStrategy .switchProtocol (response );
130+ Assertions .assertEquals (TLS .V_1_2 .getVersion (), result );
131+ }
132+
133+ // New Tests for parseTlsToken Casuistics
134+ @ Test
135+ void testSwitchToTlsValid_TLS_1_0 () throws Exception {
136+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
137+ response .addHeader (HttpHeaders .UPGRADE , "TLS/1.0" );
138+ final ProtocolVersion result = switchStrategy .switchProtocol (response );
139+ Assertions .assertEquals (TLS .V_1_0 .getVersion (), result );
140+ }
141+
142+ @ Test
143+ void testSwitchToTlsValid_TLS_1_1 () throws Exception {
144+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
145+ response .addHeader (HttpHeaders .UPGRADE , "TLS/1.1" );
146+ final ProtocolVersion result = switchStrategy .switchProtocol (response );
147+ Assertions .assertEquals (TLS .V_1_1 .getVersion (), result );
148+ }
149+
150+ @ Test
151+ void testUnsupportedTlsVersion_TLS_1_4 () throws ProtocolException {
152+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
153+ response .addHeader (HttpHeaders .UPGRADE , "TLS/1.4" );
154+ final ProtocolVersion result = switchStrategy .switchProtocol (response );
155+ Assertions .assertEquals (1 , result .getMajor ());
156+ Assertions .assertEquals (4 , result .getMinor ());
157+ }
158+
159+ @ Test
160+ void testTlsVersion_TLS_2_0 () throws ProtocolException {
161+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
162+ response .addHeader (HttpHeaders .UPGRADE , "TLS/2.0" );
163+ final ProtocolVersion result = switchStrategy .switchProtocol (response );
164+ Assertions .assertEquals (2 , result .getMajor ());
165+ }
166+
167+ @ Test
168+ void testInvalidTlsFormat_NoSlash () {
169+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
170+ response .addHeader (HttpHeaders .UPGRADE , "TLSv1" );
171+ Assertions .assertThrows (ProtocolException .class , () -> switchStrategy .switchProtocol (response ),
172+ "Invalid TLS protocol format: TLSv1" );
173+ }
174+
175+ @ Test
176+ void testInvalidTlsFormat_NonNumeric () {
177+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
178+ response .addHeader (HttpHeaders .UPGRADE , "TLS/abc" );
179+ Assertions .assertThrows (ProtocolException .class , () -> switchStrategy .switchProtocol (response ),
180+ "Invalid TLS version: abc" );
181+ }
182+
183+ @ Test
184+ void testSwitchToTlsValid_TLS_1 () throws ProtocolException {
185+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
186+ response .addHeader (HttpHeaders .UPGRADE , "TLS/1" );
187+ final ProtocolVersion result = switchStrategy .switchProtocol (response );
188+ Assertions .assertEquals (TLS .V_1_0 .getVersion (), result );
189+ }
190+
191+ @ Test
192+ void testInvalidTlsFormat_MissingMajor () {
193+ final HttpResponse response = new BasicHttpResponse (HttpStatus .SC_SWITCHING_PROTOCOLS );
194+ response .addHeader (HttpHeaders .UPGRADE , "TLS/.1" );
195+ Assertions .assertThrows (ProtocolException .class , () -> switchStrategy .switchProtocol (response ),
196+ "Invalid TLS version: .1" );
197+ }
98198}
0 commit comments