1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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"));
86 response.addHeader(new BasicHeader(AUTHENTICATION_INFO_HEADER, "nextnonce=\"10024b2308596a55d02699c0a0400fb4\",qop=auth,rspauth=\"0386df3cb9effdf08c9e00ab955827f3\",cnonce=\"21558090\",nc=00000001"));
87
88 interceptor.process(response, null, context);
89
90
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 }