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 org.apache.hc.core5.http.HttpResponse;
31  import org.apache.hc.core5.http.message.BasicHeader;
32  import org.apache.hc.core5.http.message.BasicHttpResponse;
33  import org.junit.jupiter.api.Assertions;
34  import org.junit.jupiter.api.BeforeEach;
35  import org.junit.jupiter.api.Test;
36  
37  class TestNextNonceInterceptor {
38  
39      private static final String AUTHENTICATION_INFO_HEADER = "Authentication-Info";
40  
41      private NextNonceInterceptor interceptor;
42      private HttpClientContext context;
43  
44      @BeforeEach
45      void setUp() {
46          interceptor = new NextNonceInterceptor();
47          context = HttpClientContext.create();
48      }
49  
50      @Test
51      void testNoAuthenticationInfoHeader() {
52          final HttpResponse response = new BasicHttpResponse(200);
53  
54          interceptor.process(response, null, context);
55  
56          Assertions.assertNull(context.getNextNonce(),
57                  "Context should not contain nextnonce when the header is missing");
58      }
59  
60      @Test
61      void testAuthenticationInfoHeaderWithoutNextNonce() {
62          final HttpResponse response = new BasicHttpResponse(200);
63          response.addHeader(new BasicHeader(AUTHENTICATION_INFO_HEADER, "auth-param=value"));
64  
65          interceptor.process(response, null, context);
66  
67          Assertions.assertNull(context.getNextNonce(),
68                  "Context should not contain nextnonce when it is missing in the header value");
69      }
70  
71      @Test
72      void testAuthenticationInfoHeaderWithNextNonce() {
73          final HttpResponse response = new BasicHttpResponse(200);
74          response.addHeader(new BasicHeader(AUTHENTICATION_INFO_HEADER, "nextnonce=\"10024b2308596a55d02699c0a0400fb4\",qop=auth,rspauth=\"0386df3cb9effdf08c9e00ab955827f3\",cnonce=\"21558090\",nc=00000001"));
75  
76          interceptor.process(response, null, context);
77  
78          Assertions.assertEquals("10024b2308596a55d02699c0a0400fb4", context.getNextNonce(),
79                  "Context should contain the correct nextnonce value when it is present in the header");
80      }
81  
82      @Test
83      void testMultipleAuthenticationInfoHeaders() {
84          final HttpResponse response = new BasicHttpResponse(200);
85          response.addHeader(new BasicHeader(AUTHENTICATION_INFO_HEADER, "auth-param=value"));  // First header without nextnonce
86          response.addHeader(new BasicHeader(AUTHENTICATION_INFO_HEADER, "nextnonce=\"10024b2308596a55d02699c0a0400fb4\",qop=auth,rspauth=\"0386df3cb9effdf08c9e00ab955827f3\",cnonce=\"21558090\",nc=00000001"));  // Second header with nextnonce
87  
88          interceptor.process(response, null, context);
89  
90          // Since only the first header is processed, `auth-nextnonce` should not be set in the context
91          Assertions.assertNull(context.getNextNonce(),
92                  "Context should not contain nextnonce if it's not in the first Authentication-Info header");
93      }
94  
95      @Test
96      void testAuthenticationInfoHeaderWithEmptyNextNonce() {
97          final HttpResponse response = new BasicHttpResponse(200);
98          response.addHeader(new BasicHeader(AUTHENTICATION_INFO_HEADER, "nextnonce=\"\",qop=auth,rspauth=\"0386df3cb9effdf08c9e00ab955827f3\",cnonce=\"21558090\",nc=00000001"));
99  
100         interceptor.process(response, null, context);
101 
102         Assertions.assertNull(context.getNextNonce(),
103                 "Context should not contain nextnonce if it is empty in the Authentication-Info header");
104     }
105 
106 }