Skip to content

Commit ddc3dee

Browse files
committed
Added ThenAssert convenience method returning Unit. Fixed method docs.
1 parent efab826 commit ddc3dee

File tree

2 files changed

+106
-67
lines changed

2 files changed

+106
-67
lines changed

modules/scala-extensions/src/main/scala/io/restassured/module/scala/extensions/RestAssuredScalaExtensions.scala

Lines changed: 90 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package io.restassured.module.scala.extensions
22

3-
import scala.util.chaining.*
43
import scala.reflect.ClassTag
4+
import scala.util.chaining.*
5+
56
import io.restassured.RestAssured.`given`
7+
import io.restassured.internal.{ResponseSpecificationImpl, ValidatableResponseImpl}
68
import io.restassured.response.{ExtractableResponse, Response, ValidatableResponse}
79
import io.restassured.specification.RequestSpecification
8-
import io.restassured.internal.{ValidatableResponseImpl, ResponseSpecificationImpl}
910

1011
// Main wrappers
1112

@@ -54,92 +55,114 @@ def Given(block: RequestSpecification => RequestSpecification): RequestSpecifica
5455
def Given(): RequestSpecification =
5556
`given`()
5657

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-
*/
7358
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+
*/
7476
infix def When(block: RequestSpecification => Response): Response =
7577
spec.`when`().pipe(block)
7678

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-
*/
8879
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+
*/
8994
infix def Then(block: ValidatableResponse => ValidatableResponse): ValidatableResponse =
9095
resp
9196
.`then`()
9297
.tap(doIfValidatableResponseImpl(resp => resp.forceDisableEagerAssert()))
9398
.pipe(block)
9499
.tap(doIfValidatableResponseImpl(resp => resp.forceValidateResponse()))
95100

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+
115119
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+
*/
116139
infix def Extract(
117140
block: ExtractableResponse[Response] => T
118141
)(using
119142
ClassTag[T]
120143
): T =
121144
resp.`then`().extract().pipe(block)
122145

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-
*/
142146
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+
*/
143166
infix def Extract(
144167
block: ExtractableResponse[Response] => T
145168
)(using

modules/scala-extensions/src/test/scala/io/restassured/module/scala/extensions/RestAssuredScalaExtensionsTest.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ class RestAssuredScalaExtensionsTest:
7171
)
7272
assertThat(message).isEqualTo("Hello World")
7373

74+
@Test
75+
def `validation with rest assured scala extensions using ThenAssert returning Unit`: Unit =
76+
val result = Given(req =>
77+
req.port(7000)
78+
req.header("Header", "Header")
79+
req.body("hello")
80+
)
81+
.When(
82+
_.put("/the/path")
83+
)
84+
.ThenAssert(res =>
85+
res.statusCode(200)
86+
res.body("message", equalTo("Hello World"))
87+
)
88+
assertThat(result).isEqualTo(())
89+
7490
@Test
7591
def `extraction after 'then', when path is not used in 'Then', with rest assured scala extensions`: Unit =
7692
val message: String = Given(req =>

0 commit comments

Comments
 (0)