@@ -48,6 +48,17 @@ class ParamsPayloadTestAction extends Action with SkipCsrfCheck {
4848 }
4949}
5050
51+ @ GET (" test/params/validation" )
52+ class ParamsValidationTestAction extends Action with SkipCsrfCheck {
53+ def execute () {
54+ param[Char ](" keyChar" )
55+ param[Int ](" keyInt" )
56+ param[Boolean ](" keyBoolean" )
57+
58+ respondText(" Ok" )
59+ }
60+ }
61+
5162class ParamsTest extends FlatSpec with Matchers with BeforeAndAfter with Log {
5263 private implicit val formats = Serialization .formats(NoTypeHints )
5364 private val base = " http://127.0.0.1:8000/my_site"
@@ -107,6 +118,50 @@ class ParamsTest extends FlatSpec with Matchers with BeforeAndAfter with Log {
107118 shouldEqual(response, ParamsPayloadResponse (1 , Seq (2 , 3 ), " ru-RU" , Some (" value" )))
108119 }
109120
121+ " [GET] param" should " response validation error char" in {
122+ val response = client.prepareGet(s " $base/test/params/validation " )
123+ .addQueryParam(" keyChar" , " " )
124+ .addQueryParam(" keyInt" , " 1" )
125+ .addQueryParam(" keyBoolean" , " true" )
126+ .execute().get()
127+ (response.getStatusCode, response.getResponseBody) shouldEqual
128+ (400 , """ Validation error: Cannot convert "" of param "keyChar" to Char""" )
129+ }
130+
131+ " [GET] param" should " response validation error overflow int" in {
132+ val keyValue = Int .MaxValue + 1L
133+ val response = client.prepareGet(s " $base/test/params/validation " )
134+ .addQueryParam(" keyChar" , " a" )
135+ .addQueryParam(" keyInt" , keyValue.toString)
136+ .addQueryParam(" keyBoolean" , " true" )
137+ .execute().get()
138+ (response.getStatusCode, response.getResponseBody) shouldEqual
139+ (400 , s """ Validation error: Cannot convert " $keyValue" of param "keyInt" to Int """ )
140+ }
141+
142+
143+ " [GET] param" should " response validation error parse int" in {
144+ val keyValue = " 1a"
145+ val response = client.prepareGet(s " $base/test/params/validation " )
146+ .addQueryParam(" keyChar" , " a" )
147+ .addQueryParam(" keyInt" , keyValue)
148+ .addQueryParam(" keyBoolean" , " true" )
149+ .execute().get()
150+ (response.getStatusCode, response.getResponseBody) shouldEqual
151+ (400 , s """ Validation error: Cannot convert " $keyValue" of param "keyInt" to Int """ )
152+ }
153+
154+ " [GET] param" should " response validation error parse boolean" in {
155+ val value = " tru"
156+ val response = client.prepareGet(s " $base/test/params/validation " )
157+ .addQueryParam(" keyChar" , " c" )
158+ .addQueryParam(" keyInt" , " 1" )
159+ .addQueryParam(" keyBoolean" , value)
160+ .execute().get()
161+ (response.getStatusCode, response.getResponseBody) shouldEqual
162+ (400 , s """ Validation error: Cannot convert " $value" of param "keyBoolean" to Boolean """ )
163+ }
164+
110165 private def shouldEqual [T ](response : Response , expected : T )(implicit formats : Formats , mf : Manifest [T ]) = {
111166 if (response.getStatusCode != 200 ) {
112167 log.warn(s " Response: \n $response" )
0 commit comments