Skip to content

Commit 42060a8

Browse files
committed
Add example, exercise and answer for stub priority
1 parent b97d2e1 commit 42060a8

3 files changed

Lines changed: 242 additions & 0 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package answers;
2+
3+
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
4+
import io.restassured.builder.RequestSpecBuilder;
5+
import io.restassured.http.ContentType;
6+
import io.restassured.specification.RequestSpecification;
7+
import models.LoanDetails;
8+
import models.LoanRequest;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.params.ParameterizedTest;
12+
import org.junit.jupiter.params.provider.CsvSource;
13+
14+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
15+
import static io.restassured.RestAssured.given;
16+
17+
@WireMockTest(httpPort = 9876)
18+
public class WireMockAnswers6Test {
19+
20+
private RequestSpecification requestSpec;
21+
22+
@BeforeEach
23+
public void createRequestSpec() {
24+
25+
requestSpec = new RequestSpecBuilder().
26+
setBaseUri("http://localhost").
27+
setPort(9876).
28+
build();
29+
}
30+
31+
public void setupStubExercise601() {
32+
33+
/**
34+
* Add two stub definitions:
35+
* - One that responds to an HTTP POST to /requestLoan with an HTTP status code 201,
36+
* but only if the value of the 'Authorization' header is equal to 'Bearer validToken',
37+
* i.e., the authorization token used is equal to 'validToken'
38+
* - Another that responds to an HTTP POST to /requestLoan with an HTTP status code 401,
39+
* but only if any other authorization token value is used (hint: use a regular expression),
40+
* or if there is no 'Authorization' header at all present in the request
41+
*
42+
* Use stub priority to have WireMock check that the token is equal to 'validToken' first,
43+
* before rejecting the request because the token has any other value
44+
* or because the Authorization header is absent
45+
*/
46+
47+
stubFor(post(urlMatching("/requestLoan")).atPriority(1)
48+
.withHeader("Authorization", equalTo("Bearer validToken"))
49+
.willReturn(aResponse()
50+
.withStatus(201)
51+
));
52+
53+
stubFor(post(urlMatching("/requestLoan")).atPriority(2)
54+
.withHeader("Authorization", matching("Bearer (.*)").or(absent()))
55+
.willReturn(aResponse()
56+
.withStatus(401)
57+
));
58+
}
59+
60+
@Test
61+
public void testExercise601() {
62+
63+
setupStubExercise601();
64+
65+
given().
66+
spec(requestSpec).
67+
auth().
68+
oauth2("validToken").
69+
when().
70+
post("/requestLoan").
71+
then().
72+
assertThat().
73+
statusCode(201);
74+
75+
given().
76+
spec(requestSpec).
77+
auth().
78+
oauth2("invalidToken").
79+
when().
80+
post("/requestLoan").
81+
then().
82+
assertThat().
83+
statusCode(401);
84+
85+
given().
86+
spec(requestSpec).
87+
when().
88+
post("/requestLoan").
89+
then().
90+
assertThat().
91+
statusCode(401);
92+
}
93+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package examples;
2+
3+
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
4+
import io.restassured.builder.RequestSpecBuilder;
5+
import io.restassured.specification.RequestSpecification;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
10+
import static io.restassured.RestAssured.given;
11+
12+
@WireMockTest(httpPort = 9876)
13+
public class WireMockExamples6Test {
14+
15+
private RequestSpecification requestSpec;
16+
17+
@BeforeEach
18+
public void createRequestSpec() {
19+
20+
requestSpec = new RequestSpecBuilder().
21+
setBaseUri("http://localhost").
22+
setPort(9876).
23+
build();
24+
}
25+
26+
public void setupPizzaIngredientsStub() {
27+
28+
stubFor(get(urlEqualTo("/pizza/ingredients/pineapple"))
29+
.atPriority(1)
30+
.willReturn(
31+
aResponse()
32+
.withHeader("Content-Type", "text/plain")
33+
.withStatus(400)
34+
.withBody("No. Just no."))
35+
);
36+
37+
stubFor(get(urlMatching("/pizza/ingredients/(.*)"))
38+
.atPriority(2)
39+
.willReturn(
40+
aResponse()
41+
.withHeader("Content-Type", "text/plain")
42+
.withStatus(200)
43+
.withBody("That's fine."))
44+
);
45+
}
46+
47+
@Test
48+
public void pizzaIngredientsVerificationTest() {
49+
50+
setupPizzaIngredientsStub();
51+
52+
given().
53+
spec(requestSpec).
54+
when().
55+
get("/pizza/ingredients/pineapple").
56+
then().
57+
assertThat().
58+
statusCode(400).
59+
and().
60+
body(org.hamcrest.Matchers.equalTo("No. Just no."));
61+
62+
given().
63+
spec(requestSpec).
64+
when().
65+
get("/pizza/ingredients/mozzarella").
66+
then().
67+
assertThat().
68+
statusCode(200).
69+
and().
70+
body(org.hamcrest.Matchers.equalTo("That's fine."));
71+
}
72+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package exercises;
2+
3+
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
4+
import io.restassured.builder.RequestSpecBuilder;
5+
import io.restassured.specification.RequestSpecification;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
10+
import static io.restassured.RestAssured.given;
11+
12+
@WireMockTest(httpPort = 9876)
13+
public class WireMockExercises6Test {
14+
15+
private RequestSpecification requestSpec;
16+
17+
@BeforeEach
18+
public void createRequestSpec() {
19+
20+
requestSpec = new RequestSpecBuilder().
21+
setBaseUri("http://localhost").
22+
setPort(9876).
23+
build();
24+
}
25+
26+
public void setupStubExercise601() {
27+
28+
/**
29+
* Add two stub definitions:
30+
* - One that responds to an HTTP POST to /requestLoan with an HTTP status code 201,
31+
* but only if the value of the 'Authorization' header is equal to 'Bearer validToken',
32+
* i.e., the authorization token used is equal to 'validToken'
33+
* - Another that responds to an HTTP POST to /requestLoan with an HTTP status code 401,
34+
* but only if any other authorization token value is used (hint: use a regular expression),
35+
* or if there is no 'Authorization' header at all present in the request
36+
*
37+
* Use stub priority to have WireMock check that the token is equal to 'validToken' first,
38+
* before rejecting the request because the token has any other value
39+
* or because the Authorization header is absent
40+
*/
41+
42+
}
43+
44+
@Test
45+
public void testExercise601() {
46+
47+
setupStubExercise601();
48+
49+
given().
50+
spec(requestSpec).
51+
auth().
52+
oauth2("validToken").
53+
when().
54+
post("/requestLoan").
55+
then().
56+
assertThat().
57+
statusCode(201);
58+
59+
given().
60+
spec(requestSpec).
61+
auth().
62+
oauth2("invalidToken").
63+
when().
64+
post("/requestLoan").
65+
then().
66+
assertThat().
67+
statusCode(401);
68+
69+
given().
70+
spec(requestSpec).
71+
when().
72+
post("/requestLoan").
73+
then().
74+
assertThat().
75+
statusCode(401);
76+
}
77+
}

0 commit comments

Comments
 (0)