Skip to content

Commit c3ced7d

Browse files
committed
Query param & path param equivalence
1 parent acfbf10 commit c3ced7d

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/test/java/com/typicode/jsonplaceholder/features/Posts.feature

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,9 @@ Feature: Posts Endpoint
184184
| 0 |
185185
| 101 |
186186
| -1 |
187+
188+
Scenario: Path parameter equivalent to id query parameter
189+
When I make a GET request to the Posts endpoint with a path parameter of 1
190+
And I make a GET request to the Posts endpoint with an "id" query parameter of 1
191+
Then the two response bodies are identical
192+

src/test/java/com/typicode/jsonplaceholder/helpers/RequestHelpers.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import java.net.http.HttpClient;
66
import java.net.http.HttpRequest;
77
import java.net.http.HttpResponse;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.Map;
811

912
public class RequestHelpers {
1013

@@ -64,4 +67,17 @@ public static HttpResponse<String> sendDeleteRequestTo(String endpoint) {
6467
return null;
6568
}
6669
}
70+
71+
public static String buildQueryParamsString(Map<String, String> params) {
72+
StringBuilder paramString = new StringBuilder();
73+
if (params.size() > 0) {
74+
List<String> keys = new ArrayList<>(params.keySet());
75+
for (String key : keys) {
76+
paramString.append("&").append(key).append("=").append(params.get(key));
77+
}
78+
//Strip off initial &=
79+
paramString.substring(2);
80+
}
81+
return "?" + paramString;
82+
}
6783
}

src/test/java/com/typicode/jsonplaceholder/steps/CommonSteps.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.net.http.HttpResponse;
1616
import java.nio.file.Files;
1717
import java.util.ArrayList;
18+
import java.util.HashMap;
1819
import java.util.List;
1920
import java.util.Map;
2021

@@ -102,6 +103,13 @@ public static void makeRequestWithBody(String requestType, String endpoint, int
102103
responses.add(response);
103104
}
104105

106+
@When("^I make a GET request to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint with an? \"(.*)\" query parameter of (.*)$")
107+
public static void makeGetRequestWithQueryParameter(String endpoint, String key, String value) {
108+
Map<String, String> params = new HashMap<>();
109+
params.put(key, value);
110+
response = RequestHelpers.sendGetRequestTo(endpoints.get(endpoint) + RequestHelpers.buildQueryParamsString(params));
111+
responses.add(response);
112+
}
105113

106114
@Then("the response has a status code of {int}")
107115
public static void verifyResponseStatusCode(int code) {
@@ -156,5 +164,16 @@ public static void verifyResponseBodyIsEmptyJSONObject() {
156164
assertEquals(new JSONObject().toString(), response.body());
157165
}
158166

159-
167+
@Then("the two response bodies are identical")
168+
public static void verifyResponseBodiesMatch() {
169+
String[] responseBodies = {responses.get(responses.size() - 2).body(), responses.get(responses.size() - 1).body()};
170+
for (int i = 0; i < responseBodies.length; i++) {
171+
if (responseBodies[i].startsWith("[")) {
172+
responseBodies[i] = new JSONArray(responseBodies[i]).get(0).toString();
173+
} else {
174+
responseBodies[i] = new JSONObject((responseBodies[i])).toString();
175+
}
176+
}
177+
assertEquals(responseBodies[0], responseBodies[1]);
178+
}
160179
}

0 commit comments

Comments
 (0)