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
29
30 package com.jcabi.http.wire;
31
32 import com.jcabi.http.mock.MkAnswer;
33 import com.jcabi.http.mock.MkContainer;
34 import com.jcabi.http.mock.MkGrizzlyContainer;
35 import com.jcabi.http.mock.MkQueryMatchers;
36 import com.jcabi.http.request.JdkRequest;
37 import com.jcabi.http.response.RestResponse;
38 import jakarta.ws.rs.core.HttpHeaders;
39 import jakarta.ws.rs.core.UriBuilder;
40 import jakarta.xml.bind.DatatypeConverter;
41 import java.net.HttpURLConnection;
42 import java.net.URI;
43 import java.net.URLEncoder;
44 import java.nio.charset.StandardCharsets;
45 import javax.net.ssl.HttpsURLConnection;
46 import org.hamcrest.MatcherAssert;
47 import org.hamcrest.Matchers;
48 import org.junit.jupiter.api.Assertions;
49 import org.junit.jupiter.api.Test;
50 import org.junit.jupiter.api.function.Executable;
51 import org.junit.jupiter.params.ParameterizedTest;
52 import org.junit.jupiter.params.provider.CsvSource;
53
54
55
56
57
58
59 final class BasicAuthWireTest {
60
61
62
63
64 private static final String CRED_FORMAT = "%s:%s";
65
66
67
68
69
70
71
72
73 @ParameterizedTest
74 @CsvSource({
75 "Alice, secret",
76 "Bob, s&e+c`ret",
77 "user, \u20ac\u20ac"
78 })
79 void testHeader(
80 final String username, final String password
81 ) throws Exception {
82 final MkContainer container = new MkGrizzlyContainer().next(
83 new MkAnswer.Simple("")
84 ).start();
85 final URI uri = UriBuilder.fromUri(container.home()).userInfo(
86 String.format(
87 BasicAuthWireTest.CRED_FORMAT,
88 URLEncoder.encode(username, StandardCharsets.UTF_8.displayName()),
89 URLEncoder.encode(password, StandardCharsets.UTF_8.displayName())
90 )
91 ).build();
92 final String expected = BasicAuthWireTest.expectHeader(
93 username,
94 password
95 );
96 new JdkRequest(uri)
97 .through(BasicAuthWire.class)
98 .fetch()
99 .as(RestResponse.class)
100 .assertStatus(HttpURLConnection.HTTP_OK);
101 container.stop();
102 MatcherAssert.assertThat(
103 container.take().headers().get(HttpHeaders.AUTHORIZATION).get(0),
104 Matchers.equalTo(expected)
105 );
106 }
107
108
109
110
111
112
113 @Test
114 void shouldStripUserInfo() throws Exception {
115 final MkContainer container = new MkGrizzlyContainer().next(
116 new MkAnswer.Simple(HttpsURLConnection.HTTP_NOT_FOUND),
117 MkQueryMatchers.hasHeader(
118 "Authorization", Matchers.contains(
119 BasicAuthWireTest.expectHeader("foo", "bar")
120 )
121 )
122 ).start();
123 final String userinfo = "foo:bar";
124 final URI uri = UriBuilder.fromUri(container.home()).userInfo(
125 userinfo
126 ).build();
127 MatcherAssert.assertThat(
128 Assertions.assertThrows(
129 AssertionError.class,
130 new Executable() {
131 @Override
132 public void execute() throws Throwable {
133 new JdkRequest(uri)
134 .through(BasicAuthWire.class)
135 .fetch()
136 .as(RestResponse.class)
137 .assertStatus(HttpURLConnection.HTTP_OK);
138 }
139 }
140 ),
141 Matchers.<AssertionError>hasToString(
142 Matchers.not(
143 Matchers.containsString(userinfo)
144 )
145 )
146 );
147 container.stop();
148 }
149
150
151
152
153
154
155
156
157
158
159 private static String expectHeader(
160 final String username,
161 final String password
162 ) {
163 final String credentials = DatatypeConverter.printBase64Binary(
164 String.format(
165 BasicAuthWireTest.CRED_FORMAT,
166 username,
167 password
168 ).getBytes(StandardCharsets.UTF_8)
169 );
170 return String.format("Basic %s", credentials);
171 }
172 }