|
1 | 1 | package io.restassured.module.scala.extensions
|
2 | 2 |
|
3 |
| -import scala.util.chaining.* |
4 | 3 | import scala.reflect.ClassTag
|
| 4 | +import scala.util.chaining.* |
| 5 | + |
5 | 6 | import io.restassured.RestAssured.`given`
|
| 7 | +import io.restassured.internal.{ResponseSpecificationImpl, ValidatableResponseImpl} |
6 | 8 | import io.restassured.response.{ExtractableResponse, Response, ValidatableResponse}
|
7 | 9 | import io.restassured.specification.RequestSpecification
|
8 |
| -import io.restassured.internal.{ValidatableResponseImpl, ResponseSpecificationImpl} |
9 | 10 |
|
10 | 11 | // Main wrappers
|
11 | 12 |
|
@@ -54,92 +55,114 @@ def Given(block: RequestSpecification => RequestSpecification): RequestSpecifica
|
54 | 55 | def Given(): RequestSpecification =
|
55 | 56 | `given`()
|
56 | 57 |
|
57 |
| -/** |
58 |
| - * A wrapper around [io.restassured.RestAssured.when] to start building the DSL |
59 |
| - * expression by sending a request without any parameters or headers etc. E.g. |
60 |
| - * {{{ |
61 |
| - * Given() |
62 |
| - * .When(_.get("/x")) |
63 |
| - * .Then(_.body("x.y.z1", equalTo("Z1"))) |
64 |
| - * }}} |
65 |
| - * Note that if you need to add parameters, headers, cookies or other request |
66 |
| - * properties use the [[Given()]] method. |
67 |
| - * |
68 |
| - * @see |
69 |
| - * io.restassured.RestAssured.when |
70 |
| - * @return |
71 |
| - * A request sender interface that let's you call resources on the server |
72 |
| - */ |
73 | 58 | extension (spec: RequestSpecification)
|
| 59 | + /** |
| 60 | + * A wrapper around [io.restassured.RestAssured.when] to start building the |
| 61 | + * DSL expression by sending a request without any parameters or headers etc. |
| 62 | + * E.g. |
| 63 | + * {{{ |
| 64 | + * Given() |
| 65 | + * .When(_.get("/x")) |
| 66 | + * .Then(_.body("x.y.z1", equalTo("Z1"))) |
| 67 | + * }}} |
| 68 | + * Note that if you need to add parameters, headers, cookies or other request |
| 69 | + * properties use the [[Given()]] method. |
| 70 | + * |
| 71 | + * @see |
| 72 | + * io.restassured.RestAssured.when |
| 73 | + * @return |
| 74 | + * A request sender interface that let's you call resources on the server |
| 75 | + */ |
74 | 76 | infix def When(block: RequestSpecification => Response): Response =
|
75 | 77 | spec.`when`().pipe(block)
|
76 | 78 |
|
77 |
| -/** |
78 |
| - * A wrapper around [then] that lets you validate the response. Usage example: |
79 |
| - * {{{ |
80 |
| - * Given(_.params("firstName", "John") |
81 |
| - * .When(_.post("/greetXML")) |
82 |
| - * .Then(_.body("greeting.firstName", equalTo("John"))) |
83 |
| - * }}} |
84 |
| - * |
85 |
| - * @return |
86 |
| - * A validatable response |
87 |
| - */ |
88 | 79 | extension (resp: Response)
|
| 80 | + /** |
| 81 | + * A wrapper around [then] that lets you validate the response. This method |
| 82 | + * returns a [[ValidatableResponse]] that lets you chain validation methods or |
| 83 | + * extract values from the response with the [[Extract]] method. Usage |
| 84 | + * example: |
| 85 | + * {{{ |
| 86 | + * Given(_.params("firstName", "John") |
| 87 | + * .When(_.post("/greetXML")) |
| 88 | + * .Then(_.body("greeting.firstName", equalTo("John"))) |
| 89 | + * }}} |
| 90 | + * |
| 91 | + * @return |
| 92 | + * A validatable response |
| 93 | + */ |
89 | 94 | infix def Then(block: ValidatableResponse => ValidatableResponse): ValidatableResponse =
|
90 | 95 | resp
|
91 | 96 | .`then`()
|
92 | 97 | .tap(doIfValidatableResponseImpl(resp => resp.forceDisableEagerAssert()))
|
93 | 98 | .pipe(block)
|
94 | 99 | .tap(doIfValidatableResponseImpl(resp => resp.forceValidateResponse()))
|
95 | 100 |
|
96 |
| -/** |
97 |
| - * A wrapper around [ExtractableResponse] that lets you validate the response. |
98 |
| - * Usage example: |
99 |
| - * {{{ |
100 |
| - * val firstName: String = Given(_.params("firstName", "John") |
101 |
| - * .When(_.post("/greetXML")) |
102 |
| - * .Then(_.body("greeting.firstName", equalTo("John"))) |
103 |
| - * .Extract(_.path("greeting.firstName")) |
104 |
| - * }}} |
105 |
| - * The above code will send a POST request to "/greetXML" with request |
106 |
| - * parameters `firstName=John` and `lastName=Doe` and expect that the response |
107 |
| - * body containing JSON or XML firstName equal to John. The response is then |
108 |
| - * validated and the firstName is extracted from the response. The extracted |
109 |
| - * firstName is then returned. The type of the extracted value is needs to be |
110 |
| - * specified as a type parameter. |
111 |
| - * |
112 |
| - * @return |
113 |
| - * The extracted value |
114 |
| - */ |
| 101 | + /** |
| 102 | + * A wrapper around [then] that lets you validate the response. Usage example: |
| 103 | + * {{{ |
| 104 | + * Given(_.params("firstName", "John") |
| 105 | + * .When(_.post("/greetXML")) |
| 106 | + * .Then(_.body("greeting.firstName", equalTo("John"))) |
| 107 | + * }}} |
| 108 | + * This method is an overloaded version of [Then] that returns Unit so it's |
| 109 | + * automatically picked-up by test frameworks like JUnit which expect tests to |
| 110 | + * return Unit. |
| 111 | + * |
| 112 | + * @return |
| 113 | + * A validatable response |
| 114 | + */ |
| 115 | + infix def ThenAssert(block: ValidatableResponse => ValidatableResponse): Unit = |
| 116 | + resp.Then(block) |
| 117 | +end extension |
| 118 | + |
115 | 119 | extension [T](resp: Response)
|
| 120 | + /** |
| 121 | + * A wrapper around [ExtractableResponse] that lets you validate the response. |
| 122 | + * Usage example: |
| 123 | + * {{{ |
| 124 | + * val firstName: String = Given(_.params("firstName", "John") |
| 125 | + * .When(_.post("/greetXML")) |
| 126 | + * .Then(_.body("greeting.firstName", equalTo("John"))) |
| 127 | + * .Extract(_.path("greeting.firstName")) |
| 128 | + * }}} |
| 129 | + * The above code will send a POST request to "/greetXML" with request |
| 130 | + * parameters `firstName=John` and `lastName=Doe` and expect that the response |
| 131 | + * body containing JSON or XML firstName equal to John. The response is then |
| 132 | + * validated and the firstName is extracted from the response. The extracted |
| 133 | + * firstName is then returned. The type of the extracted value is needs to be |
| 134 | + * specified as a type parameter. |
| 135 | + * |
| 136 | + * @return |
| 137 | + * The extracted value |
| 138 | + */ |
116 | 139 | infix def Extract(
|
117 | 140 | block: ExtractableResponse[Response] => T
|
118 | 141 | )(using
|
119 | 142 | ClassTag[T]
|
120 | 143 | ): T =
|
121 | 144 | resp.`then`().extract().pipe(block)
|
122 | 145 |
|
123 |
| -/** |
124 |
| - * A wrapper around [ValidatableResponse] that lets you validate the response. |
125 |
| - * Usage example: |
126 |
| - * {{{ |
127 |
| - * val firstName: String = Given(_.params("firstName", "John") |
128 |
| - * .When(_.post("/greetXML")) |
129 |
| - * .Then(_.body("greeting.firstName", equalTo("John"))) |
130 |
| - * .Extract(_.path("greeting.firstName")) |
131 |
| - * }}} |
132 |
| - * The above code will send a POST request to "/greetXML" with request |
133 |
| - * parameters `firstName=John` and `lastName=Doe` and expect that the response |
134 |
| - * body containing JSON or XML firstName equal to John. The response is then |
135 |
| - * validated and the firstName is extracted from the response. The extracted |
136 |
| - * firstName is then returned. The type of the extracted value is needs to be |
137 |
| - * specified as a type parameter. |
138 |
| - * |
139 |
| - * @return |
140 |
| - * The extracted value |
141 |
| - */ |
142 | 146 | extension [T](resp: ValidatableResponse)
|
| 147 | + /** |
| 148 | + * A wrapper around [ValidatableResponse] that lets you validate the response. |
| 149 | + * Usage example: |
| 150 | + * {{{ |
| 151 | + * val firstName: String = Given(_.params("firstName", "John") |
| 152 | + * .When(_.post("/greetXML")) |
| 153 | + * .Then(_.body("greeting.firstName", equalTo("John"))) |
| 154 | + * .Extract(_.path("greeting.firstName")) |
| 155 | + * }}} |
| 156 | + * The above code will send a POST request to "/greetXML" with request |
| 157 | + * parameters `firstName=John` and `lastName=Doe` and expect that the response |
| 158 | + * body containing JSON or XML firstName equal to John. The response is then |
| 159 | + * validated and the firstName is extracted from the response. The extracted |
| 160 | + * firstName is then returned. The type of the extracted value is needs to be |
| 161 | + * specified as a type parameter. |
| 162 | + * |
| 163 | + * @return |
| 164 | + * The extracted value |
| 165 | + */ |
143 | 166 | infix def Extract(
|
144 | 167 | block: ExtractableResponse[Response] => T
|
145 | 168 | )(using
|
|
0 commit comments