Skip to content

Commit 48e94f9

Browse files
committed
Refactor steps that make requests
1 parent 774bb7f commit 48e94f9

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ Feature: Posts Endpoint
5555
And the response body is an empty JSON object
5656

5757
Scenario: Put request to all posts endpoint raises error
58-
When I make a PUT request to the Posts endpoint
58+
When I make a PUT request with an empty body to the Posts endpoint
5959
Then the response has a status code of 404
6060
And the response body is an empty JSON object
6161

6262
Scenario: Post request to all posts endpoint creates new post
63-
When I make a POST request to the Posts endpoint
63+
When I make a POST request with an empty body to the Posts endpoint
6464
Then the response has a status code of 201
6565
And the response body matches the following
6666
| key | value |

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

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,41 +50,38 @@ public static void setup() {
5050
responses = new ArrayList<>();
5151
}
5252

53-
@When("^I make a GET request to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint$")
54-
public static void makeGetRequest(String endpoint) {
55-
response = RequestHelpers.sendGetRequestTo(endpoints.get(endpoint));
53+
@When("^I make a (GET|DELETE) request to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint$")
54+
public static void makeRequest(String requestType, String endpoint) {
55+
response = requestType.equals("GET") ?
56+
RequestHelpers.sendGetRequestTo(endpoints.get(endpoint)) :
57+
RequestHelpers.sendDeleteRequestTo((endpoints.get(endpoint)));
5658
responses.add(response);
5759
}
5860

59-
@When("^I make a GET request to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint with a path parameter of (-?\\d+)$")
60-
public static void makeGetRequestWithPathParameter(String endpoint, int pathParam) {
61-
response = RequestHelpers.sendGetRequestTo(endpoints.get(endpoint) + "/" + pathParam);
61+
@When("^I make a (GET|DELETE) request to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint with a path parameter of (-?\\d+)$")
62+
public static void makeRequestWithPathParameter(String requestType, String endpoint, int pathParam) {
63+
response = requestType.equals("GET") ?
64+
RequestHelpers.sendGetRequestTo(endpoints.get(endpoint) + "/" + pathParam) :
65+
RequestHelpers.sendDeleteRequestTo(endpoints.get(endpoint) + "/" + pathParam);
6266
responses.add(response);
6367
}
6468

65-
@When("^I make a PUT request to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint$")
66-
public static void makePutRequestWithEmptyBody(String endpoint) {
67-
response = RequestHelpers.sendPutRequestTo(endpoints.get(endpoint), "{}");
69+
@When("^I make a (POST|PUT) request with an empty body to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint$")
70+
public static void makeRequestWithEmptyBody(String requestType, String endpoint) {
71+
response = requestType.equals("POST") ?
72+
RequestHelpers.sendPostRequestTo(endpoints.get(endpoint), "{}") :
73+
RequestHelpers.sendPutRequestTo(endpoints.get(endpoint), "{}");
6874
responses.add(response);
6975
}
7076

71-
@When("^I make a POST request to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint$")
72-
public static void makePostRequestWithEmptyBody(String endpoint) {
73-
response = RequestHelpers.sendPostRequestTo(endpoints.get(endpoint), "{}");
77+
@When("^I make a (POST|PUT) request with an empty body to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint with a path parameter of (-?\\d+)$")
78+
public static void makeRequestWithEmptyBody(String requestType, String endpoint, int pathParam) {
79+
response = requestType.equals("POST") ?
80+
RequestHelpers.sendPostRequestTo(endpoints.get(endpoint) + "/" + pathParam, "{}") :
81+
RequestHelpers.sendPutRequestTo(endpoints.get(endpoint) + "/" + pathParam, "{}");
7482
responses.add(response);
7583
}
7684

77-
@When("^I make a DELETE request to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint$")
78-
public static void makeDeleteRequest(String endpoint) {
79-
response = RequestHelpers.sendDeleteRequestTo(endpoints.get(endpoint));
80-
responses.add(response);
81-
}
82-
83-
@When("^I make a DELETE request to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint with a path parameter of (-?\\d+)$")
84-
public static void makeDeleteRequestWithPathParameter(String endpoint, int pathParam) {
85-
response = RequestHelpers.sendDeleteRequestTo(endpoints.get(endpoint) + "/" + pathParam);
86-
responses.add(response);
87-
}
8885

8986

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

0 commit comments

Comments
 (0)