Skip to content

Commit 4940017

Browse files
committed
Fix tests
1 parent 98aa755 commit 4940017

File tree

4 files changed

+605
-0
lines changed

4 files changed

+605
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) 2024-2025 Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
// If you have any questions regarding licensing, please contact us at
17+
// info@rabbitmq.com.
18+
package com.rabbitmq.client.amqp.oauth2;
19+
20+
import static com.rabbitmq.client.amqp.oauth2.OAuth2TestUtils.sampleJsonToken;
21+
import static java.time.Duration.ofSeconds;
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
import static org.assertj.core.api.Assertions.within;
24+
25+
import java.time.Duration;
26+
import java.time.Instant;
27+
import java.time.temporal.ChronoUnit;
28+
import java.util.UUID;
29+
import org.junit.jupiter.api.Test;
30+
31+
public class GsonTokenParserTest {
32+
33+
TokenParser parser = new GsonTokenParser();
34+
35+
@Test
36+
void parse() {
37+
String accessToken = UUID.randomUUID().toString();
38+
Duration expireIn = ofSeconds(60);
39+
String jsonToken = sampleJsonToken(accessToken, expireIn);
40+
Token token = parser.parse(jsonToken);
41+
assertThat(token.value()).isEqualTo(accessToken);
42+
assertThat(token.expirationTime())
43+
.isCloseTo(Instant.now().plus(expireIn), within(1, ChronoUnit.SECONDS));
44+
}
45+
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// Copyright (c) 2024-2025 Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
// If you have any questions regarding licensing, please contact us at
17+
// info@rabbitmq.com.
18+
package com.rabbitmq.client.amqp.oauth2;
19+
20+
import static java.nio.charset.StandardCharsets.UTF_8;
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
import com.google.gson.Gson;
24+
import com.google.gson.reflect.TypeToken;
25+
import com.sun.net.httpserver.Headers;
26+
import com.sun.net.httpserver.HttpServer;
27+
import java.io.IOException;
28+
import java.io.OutputStream;
29+
import java.net.http.HttpClient;
30+
import java.security.KeyStore;
31+
import java.time.Duration;
32+
import java.time.Instant;
33+
import java.util.Arrays;
34+
import java.util.Map;
35+
import java.util.UUID;
36+
import java.util.concurrent.atomic.AtomicReference;
37+
import java.util.function.Consumer;
38+
import java.util.stream.Collectors;
39+
import javax.net.ssl.SSLContext;
40+
import javax.net.ssl.TrustManagerFactory;
41+
import org.junit.jupiter.api.AfterEach;
42+
import org.junit.jupiter.api.BeforeEach;
43+
import org.junit.jupiter.params.ParameterizedTest;
44+
import org.junit.jupiter.params.provider.ValueSource;
45+
46+
public class HttpTokenRequesterTest {
47+
48+
HttpServer server;
49+
int port;
50+
String contextPath = "/uaa/oauth/token";
51+
52+
@BeforeEach
53+
void init() throws IOException {
54+
this.port = OAuth2TestUtils.randomNetworkPort();
55+
}
56+
57+
@ParameterizedTest
58+
@ValueSource(booleans = {true, false})
59+
void requestToken(boolean tls) throws Exception {
60+
String protocol;
61+
KeyStore keyStore;
62+
Consumer<HttpClient.Builder> clientBuilderConsumer;
63+
if (tls) {
64+
protocol = "https";
65+
keyStore = OAuth2TestUtils.generateKeyPair();
66+
SSLContext sslContext = SSLContext.getInstance("TLS");
67+
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
68+
tmf.init(keyStore);
69+
sslContext.init(null, tmf.getTrustManagers(), null);
70+
clientBuilderConsumer = b -> b.sslContext(sslContext);
71+
} else {
72+
protocol = "http";
73+
keyStore = null;
74+
clientBuilderConsumer = b -> {};
75+
}
76+
String uri = String.format("%s://localhost:%d%s", protocol, port, contextPath);
77+
AtomicReference<String> httpMethod = new AtomicReference<>();
78+
AtomicReference<String> contentType = new AtomicReference<>();
79+
AtomicReference<String> authorization = new AtomicReference<>();
80+
AtomicReference<String> accept = new AtomicReference<>();
81+
AtomicReference<Map<String, String>> httpParameters = new AtomicReference<>();
82+
83+
String accessToken = UUID.randomUUID().toString();
84+
85+
Duration expiresIn = Duration.ofSeconds(60);
86+
server =
87+
OAuth2TestUtils.startServer(
88+
port,
89+
contextPath,
90+
keyStore,
91+
exchange -> {
92+
Headers headers = exchange.getRequestHeaders();
93+
httpMethod.set(exchange.getRequestMethod());
94+
contentType.set(headers.getFirst("content-type"));
95+
authorization.set(headers.getFirst("authorization"));
96+
accept.set(headers.getFirst("accept"));
97+
98+
String requestBody = new String(exchange.getRequestBody().readAllBytes(), UTF_8);
99+
Map<String, String> parameters =
100+
Arrays.stream(requestBody.split("&"))
101+
.map(p -> p.split("="))
102+
.collect(Collectors.toMap(p -> p[0], p -> p[1]));
103+
httpParameters.set(parameters);
104+
105+
byte[] data = OAuth2TestUtils.sampleJsonToken(accessToken, expiresIn).getBytes(UTF_8);
106+
107+
Headers responseHeaders = exchange.getResponseHeaders();
108+
responseHeaders.set("content-type", "application/json");
109+
exchange.sendResponseHeaders(200, data.length);
110+
OutputStream responseBody = exchange.getResponseBody();
111+
responseBody.write(data);
112+
responseBody.close();
113+
});
114+
115+
TokenRequester requester =
116+
new HttpTokenRequester(
117+
uri,
118+
"rabbit_client",
119+
"rabbit_secret",
120+
"password",
121+
Map.of("username", "rabbit_username", "password", "rabbit_password"),
122+
clientBuilderConsumer,
123+
null,
124+
StringToken::new);
125+
126+
String token = requester.request().value();
127+
assertThat(token).contains(accessToken);
128+
Gson gson = new Gson();
129+
TypeToken<Map<String, Object>> mapType = new TypeToken<>() {};
130+
Map<String, Object> tokenMap = gson.fromJson(token, mapType);
131+
assertThat(tokenMap)
132+
.containsEntry("access_token", accessToken)
133+
.containsEntry("expires_in", (double) expiresIn.toSeconds());
134+
135+
assertThat(httpMethod).hasValue("POST");
136+
assertThat(contentType).hasValue("application/x-www-form-urlencoded");
137+
assertThat(authorization).hasValue("Basic cmFiYml0X2NsaWVudDpyYWJiaXRfc2VjcmV0");
138+
assertThat(accept).hasValue("application/json");
139+
Map<String, String> parameters = httpParameters.get();
140+
assertThat(parameters)
141+
.isNotNull()
142+
.hasSize(3)
143+
.containsEntry("grant_type", "password")
144+
.containsEntry("username", "rabbit_username")
145+
.containsEntry("password", "rabbit_password");
146+
}
147+
148+
@AfterEach
149+
public void tearDown() {
150+
if (server != null) {
151+
server.stop(0);
152+
}
153+
}
154+
155+
private static class StringToken implements Token {
156+
157+
private final String value;
158+
159+
private StringToken(String value) {
160+
this.value = value;
161+
}
162+
163+
@Override
164+
public String value() {
165+
return this.value;
166+
}
167+
168+
@Override
169+
public Instant expirationTime() {
170+
return Instant.EPOCH;
171+
}
172+
}
173+
}

0 commit comments

Comments
 (0)