Skip to content

Commit d447c0b

Browse files
committed
Add PUT request tests
1 parent 48e94f9 commit d447c0b

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,55 @@ Feature: Posts Endpoint
7373
Then the response has a status code of 200
7474
And the response body is an empty JSON object
7575

76+
Scenario: Update post with empty request body
77+
# This API isn't backed by a working data source so there is no follow up GET request to confirm that the
78+
# post has been properly updated in the back end
79+
When I make a PUT request with an empty body to the Posts endpoint with a path parameter of 1
80+
Then the response has a status code of 200
81+
And the response body matches the following
82+
| key | value |
83+
| id | 1 |
84+
85+
Scenario: Update post with partial request body
86+
When I make a PUT request with the following body to the Posts endpoint with a path parameter of 1
87+
| key | value |
88+
| title | Updated Post Title |
89+
| body | Updated post body text |
90+
Then the response has a status code of 200
91+
And the response body matches the following
92+
| key | value |
93+
| id | 1 |
94+
| title | Updated Post Title |
95+
| body | Updated post body text |
96+
97+
Scenario: Update post with full request body
98+
When I make a PUT request with the following body to the Posts endpoint with a path parameter of 1
99+
| key | value |
100+
| title | Updated Post Title |
101+
| body | Updated post body text |
102+
| userId | 5 |
103+
Then the response has a status code of 200
104+
And the response body matches the following
105+
| key | value |
106+
| id | 1 |
107+
| title | Updated Post Title |
108+
| body | Updated post body text |
109+
| userId | 5 |
110+
111+
Scenario: Update post with invalid fields in request body
112+
# As this API isn't linked to a working data source there is no validation of the field names passed in via the
113+
# request body. If this were a fully working API I would expect this to raise an error
114+
When I make a PUT request with the following body to the Posts endpoint with a path parameter of 1
115+
| key | value |
116+
| key_1 | New value |
117+
| key_2 | New value |
118+
Then the response has a status code of 200
119+
And the response body matches the following
120+
| key | value |
121+
| id | 1 |
122+
| key_1 | New value |
123+
| key_2 | New value |
124+
76125
Scenario Outline: Delete post with invalid ID - post #<ID>
77126
# As this is a fake API without an underlying data source that updates based on the API requests, a delete request
78127
# returns a success response regardless of the post ID passed in via the path parameters. This should raise an error

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static HttpResponse<String> sendGetRequestTo(String endpoint) {
2727
public static HttpResponse<String> sendPutRequestTo(String endpoint, String body) {
2828
HttpRequest request = HttpRequest.newBuilder()
2929
.PUT(HttpRequest.BodyPublishers.ofString(body))
30+
.header("Content-Type", "application/json")
3031
.uri(URI.create(BASE_URL + endpoint))
3132
.build();
3233
try {
@@ -40,6 +41,7 @@ public static HttpResponse<String> sendPutRequestTo(String endpoint, String body
4041
public static HttpResponse<String> sendPostRequestTo(String endpoint, String body) {
4142
HttpRequest request = HttpRequest.newBuilder()
4243
.POST(HttpRequest.BodyPublishers.ofString(body))
44+
.header("Content-Type", "application/json")
4345
.uri(URI.create(BASE_URL + endpoint))
4446
.build();
4547
try {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ public static void makeRequestWithEmptyBody(String requestType, String endpoint,
8282
responses.add(response);
8383
}
8484

85+
@When("^I make a (POST|PUT) request with the following body to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint with a path parameter of (-?\\d+)$")
86+
public static void makeRequestWithEmptyBody(String requestType, String endpoint, int pathParam, DataTable dataTable) {
87+
Map<String, String> requestBodyMap = dataTable.subTable(1, 0).asMap(String.class, String.class);
88+
JSONObject requestBody = new JSONObject(requestBodyMap);
89+
response = requestType.equals("POST") ?
90+
RequestHelpers.sendPostRequestTo(endpoints.get(endpoint) + "/" + pathParam, requestBody.toString()) :
91+
RequestHelpers.sendPutRequestTo(endpoints.get(endpoint) + "/" + pathParam, requestBody.toString());
92+
responses.add(response);
93+
}
8594

8695

8796
@Then("the response has a status code of {int}")

0 commit comments

Comments
 (0)