Skip to content

Commit 8c71362

Browse files
committed
Test other request types sent to Posts endpoint
1 parent db4ec52 commit 8c71362

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,21 @@ Feature: Posts Endpoint
4848
| 0 |
4949
| 101 |
5050
| -1 |
51+
52+
Scenario: Delete request to all posts endpoint raises error
53+
When I make a DELETE request to the Posts endpoint
54+
Then the response has a status code of 404
55+
And the response body is an empty JSON object
56+
57+
Scenario: Put request to all posts endpoint raises error
58+
When I make a PUT request to the Posts endpoint
59+
Then the response has a status code of 404
60+
And the response body is an empty JSON object
61+
62+
Scenario: Post request to all posts endpoint creates new post
63+
When I make a POST request to the Posts endpoint
64+
Then the response has a status code of 201
65+
And the response body matches the following
66+
| key | value |
67+
| id | 101 |
68+

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,42 @@ public static HttpResponse<String> sendGetRequestTo(String endpoint) {
2424
}
2525
}
2626

27+
public static HttpResponse<String> sendPutRequestTo(String endpoint, String body) {
28+
HttpRequest request = HttpRequest.newBuilder()
29+
.PUT(HttpRequest.BodyPublishers.ofString(body))
30+
.uri(URI.create(BASE_URL + endpoint))
31+
.build();
32+
try {
33+
return httpClient.send(request, HttpResponse.BodyHandlers.ofString());
34+
} catch (IOException | InterruptedException e) {
35+
e.printStackTrace();
36+
return null;
37+
}
38+
}
39+
40+
public static HttpResponse<String> sendPostRequestTo(String endpoint, String body) {
41+
HttpRequest request = HttpRequest.newBuilder()
42+
.POST(HttpRequest.BodyPublishers.ofString(body))
43+
.uri(URI.create(BASE_URL + endpoint))
44+
.build();
45+
try {
46+
return httpClient.send(request, HttpResponse.BodyHandlers.ofString());
47+
} catch (IOException | InterruptedException e) {
48+
e.printStackTrace();
49+
return null;
50+
}
51+
}
52+
53+
public static HttpResponse<String> sendDeleteRequestTo(String endpoint) {
54+
HttpRequest request = HttpRequest.newBuilder()
55+
.DELETE()
56+
.uri(URI.create(BASE_URL + endpoint))
57+
.build();
58+
try {
59+
return httpClient.send(request, HttpResponse.BodyHandlers.ofString());
60+
} catch (IOException | InterruptedException e) {
61+
e.printStackTrace();
62+
return null;
63+
}
64+
}
2765
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ public static void makeGetRequestWithPathParameter(String endpoint, int pathPara
6262
response = RequestHelpers.sendGetRequestTo(endpoints.get(endpoint) + "/" + pathParam);
6363
responses.add(response);
6464
}
65+
66+
@When("^I make a PUT request to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint$")
67+
public static void makePutRequestWithEmptyBody(String endpoint) {
68+
response = RequestHelpers.sendPutRequestTo(endpoints.get(endpoint), "{}");
69+
responses.add(response);
70+
}
71+
72+
@When("^I make a POST request to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint$")
73+
public static void makePostRequestWithEmptyBody(String endpoint) {
74+
response = RequestHelpers.sendPostRequestTo(endpoints.get(endpoint), "{}");
75+
responses.add(response);
76+
}
77+
78+
@When("^I make a DELETE request to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint$")
79+
public static void makeDeleteRequest(String endpoint) {
80+
response = RequestHelpers.sendDeleteRequestTo(endpoints.get(endpoint));
81+
responses.add(response);
82+
}
83+
6584
@Then("the response has a status code of {int}")
6685
public static void verifyResponseStatusCode(int code) {
6786
assertEquals(code, response.statusCode());

0 commit comments

Comments
 (0)