Skip to content

Commit af8036d

Browse files
committed
Add test to parse OAuth2 token with GSON
Instead of Jackson. The requirement is very small, and GSON is a much smaller dependency than Jackson.
1 parent 1611364 commit af8036d

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

Diff for: pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<jetty.version>9.4.50.v20221201</jetty.version>
6767
<bouncycastle.version>1.70</bouncycastle.version>
6868
<netcrusher.version>0.10</netcrusher.version>
69+
<gson.version>2.10.1</gson.version>
6970

7071
<maven.javadoc.plugin.version>3.5.0</maven.javadoc.plugin.version>
7172
<maven.release.plugin.version>2.5.3</maven.release.plugin.version>
@@ -784,6 +785,12 @@
784785
<version>${opentelemetry.version}</version>
785786
<scope>test</scope>
786787
</dependency>
788+
<dependency>
789+
<groupId>com.google.code.gson</groupId>
790+
<artifactId>gson</artifactId>
791+
<version>${gson.version}</version>
792+
<scope>test</scope>
793+
</dependency>
787794

788795
</dependencies>
789796

Diff for: src/test/java/com/rabbitmq/client/impl/OAuth2ClientCredentialsGrantCredentialsProviderTest.java

+32-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package com.rabbitmq.client.impl;
1717

18+
import com.google.gson.Gson;
1819
import com.rabbitmq.client.test.TestUtils;
1920
import org.bouncycastle.asn1.x500.X500NameBuilder;
2021
import org.bouncycastle.asn1.x500.style.BCStyle;
@@ -195,7 +196,7 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques
195196
}
196197

197198
@Test
198-
public void parseToken() {
199+
public void parseTokenDefault() {
199200
OAuth2ClientCredentialsGrantCredentialsProvider provider = new OAuth2ClientCredentialsGrantCredentialsProvider(
200201
"http://localhost:8080/uaa/oauth/token/",
201202
"rabbit_client", "rabbit_secret",
@@ -211,6 +212,36 @@ public void parseToken() {
211212
assertThat(token.getTimeBeforeExpiration()).isBetween(Duration.ofSeconds(expiresIn - 10), Duration.ofSeconds(expiresIn + 1));
212213
}
213214

215+
@Test
216+
public void parseTokenGson() {
217+
Gson gson = new Gson();
218+
OAuth2ClientCredentialsGrantCredentialsProvider provider = new OAuth2ClientCredentialsGrantCredentialsProvider(
219+
"http://localhost:8080/uaa/oauth/token/",
220+
"rabbit_client", "rabbit_secret",
221+
"client_credentials"
222+
) {
223+
@Override
224+
protected Token parseToken(String response) {
225+
try {
226+
Map<?, ?> map = gson.fromJson(response, Map.class);
227+
int expiresIn = ((Number) map.get("expires_in")).intValue();
228+
Instant receivedAt = Instant.now();
229+
return new Token(map.get("access_token").toString(), expiresIn, receivedAt);
230+
} catch (Exception e) {
231+
throw new OAuthTokenManagementException("Error while parsing OAuth 2 token", e);
232+
}
233+
}
234+
};
235+
236+
String accessToken = "18c1b1dfdda04382a8bcc14d077b71dd";
237+
int expiresIn = 43199;
238+
String response = sampleJsonToken(accessToken, expiresIn);
239+
240+
OAuth2ClientCredentialsGrantCredentialsProvider.Token token = provider.parseToken(response);
241+
assertThat(token.getAccess()).isEqualTo("18c1b1dfdda04382a8bcc14d077b71dd");
242+
assertThat(token.getTimeBeforeExpiration()).isBetween(Duration.ofSeconds(expiresIn - 10), Duration.ofSeconds(expiresIn + 1));
243+
}
244+
214245
String sampleJsonToken(String accessToken, int expiresIn) {
215246
String json = "{\n" +
216247
" \"access_token\" : \"{accessToken}\",\n" +

0 commit comments

Comments
 (0)