Skip to content

Commit 8f23285

Browse files
committed
refactor: split parseBody into parseJson and parseQuery in test utilities
1 parent 212cb89 commit 8f23285

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

oauth2_http/javatests/com/google/auth/TestUtils.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,14 @@ public static Map<String, String> parseQuery(String query) throws IOException {
116116
return map;
117117
}
118118

119-
/**
120-
* Parses the request body as either JSON or a query string.
121-
*
122-
* @param content The request body content.
123-
* @return A map of the parsed parameters.
124-
* @throws IOException If the content cannot be parsed.
125-
*/
126-
public static Map<String, String> parseBody(String content) throws IOException {
127-
if (content != null && content.trim().startsWith("{")) {
128-
GenericJson json = JSON_FACTORY.fromString(content, GenericJson.class);
129-
Map<String, String> map = new HashMap<>();
130-
for (Map.Entry<String, Object> entry : json.entrySet()) {
131-
Object value = entry.getValue();
132-
map.put(entry.getKey(), value == null ? null : value.toString());
133-
}
134-
return map;
119+
public static Map<String, String> parseJson(String content) throws IOException {
120+
GenericJson json = JSON_FACTORY.fromString(content, GenericJson.class);
121+
Map<String, String> map = new HashMap<>();
122+
for (Map.Entry<String, Object> entry : json.entrySet()) {
123+
Object value = entry.getValue();
124+
map.put(entry.getKey(), value == null ? null : value.toString());
135125
}
136-
return parseQuery(content);
126+
return map;
137127
}
138128

139129
public static String errorJson(String message) throws IOException {

oauth2_http/javatests/com/google/auth/oauth2/MockTokenServerTransport.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public LowLevelHttpResponse execute() throws IOException {
213213
}
214214

215215
String content = this.getContentAsString();
216-
Map<String, String> query = TestUtils.parseBody(content);
216+
Map<String, String> query = parseRequestContent(content);
217217
String accessToken = null;
218218
String refreshToken = null;
219219
String grantedScopesString = null;
@@ -332,7 +332,7 @@ public LowLevelHttpResponse execute() throws IOException {
332332
new MockLowLevelHttpRequest(url) {
333333
@Override
334334
public LowLevelHttpResponse execute() throws IOException {
335-
Map<String, String> parameters = TestUtils.parseBody(this.getContentAsString());
335+
Map<String, String> parameters = parseRequestContent(this.getContentAsString());
336336
String token = parameters.get("token");
337337
if (token == null) {
338338
throw new IOException("Token to revoke not found.");
@@ -364,7 +364,7 @@ public LowLevelHttpResponse execute() throws IOException {
364364
}
365365

366366
String content = this.getContentAsString();
367-
Map<String, String> query = TestUtils.parseBody(content);
367+
Map<String, String> query = parseRequestContent(content);
368368

369369
// Validate required fields.
370370
if (!query.containsKey("code")
@@ -420,6 +420,12 @@ public LowLevelHttpResponse execute() throws IOException {
420420
return super.buildRequest(method, url);
421421
}
422422

423+
private Map<String, String> parseRequestContent(String content) throws IOException {
424+
if (content != null && content.trim().startsWith("{")) {
425+
return TestUtils.parseJson(content);
426+
}
427+
return TestUtils.parseQuery(content);
428+
}
423429
private void validateAdditionalParameters(Map<String, String> query) {
424430
if (additionalParameters.containsKey(query.get("code"))) {
425431
Map<String, String> additionalParametersMap = additionalParameters.get(query.get("code"));

0 commit comments

Comments
 (0)