Skip to content

Commit ece42ca

Browse files
committed
Add tests for POST requests
1 parent d447c0b commit ece42ca

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,47 @@ Feature: Posts Endpoint
122122
| key_1 | New value |
123123
| key_2 | New value |
124124

125+
Scenario: Create new post with partial request body
126+
# New posts will always be created with the same ID (101) as there is no database behind this API
127+
When I make a POST request with the following body to the Posts endpoint
128+
| key | value |
129+
| title | New Post Title |
130+
| body | New post body text |
131+
Then the response has a status code of 201
132+
And the response body matches the following
133+
| key | value |
134+
| id | 101 |
135+
| title | New Post Title |
136+
| body | New post body text |
137+
138+
Scenario: Create new post with full request body
139+
When I make a POST request with the following body to the Posts endpoint
140+
| key | value |
141+
| title | New Post Title |
142+
| body | New post body text |
143+
| userId | 3 |
144+
Then the response has a status code of 201
145+
And the response body matches the following
146+
| key | value |
147+
| id | 101 |
148+
| title | New Post Title |
149+
| body | New post body text |
150+
| userId | 3 |
151+
152+
Scenario: Create new post with invalid fields in request body
153+
# As there is no data source behind this API, POST requests are not validated against the data schema so invalid
154+
# fields can be passed in the request body. For a real API this would throw an error
155+
When I make a POST request with the following body to the Posts endpoint
156+
| key | value |
157+
| key_1 | New value |
158+
| key_2 | New value |
159+
Then the response has a status code of 201
160+
And the response body matches the following
161+
| key | value |
162+
| id | 101 |
163+
| key_1 | New value |
164+
| key_2 | New value |
165+
125166
Scenario Outline: Delete post with invalid ID - post #<ID>
126167
# As this is a fake API without an underlying data source that updates based on the API requests, a delete request
127168
# 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/steps/CommonSteps.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ 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$")
86+
public static void makeRequestWithBody(String requestType, String endpoint, 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), requestBody.toString()) :
91+
RequestHelpers.sendPutRequestTo(endpoints.get(endpoint), requestBody.toString());
92+
responses.add(response);
93+
}
94+
8595
@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+)$")
8696
public static void makeRequestWithEmptyBody(String requestType, String endpoint, int pathParam, DataTable dataTable) {
8797
Map<String, String> requestBodyMap = dataTable.subTable(1, 0).asMap(String.class, String.class);

0 commit comments

Comments
 (0)