1
1
package scommons .api .http
2
2
3
3
import org .scalamock .scalatest .AsyncMockFactory
4
- import org .scalatest .{AsyncFlatSpec , Matchers }
4
+ import org .scalatest .{AsyncFlatSpec , Inside , Matchers }
5
5
import play .api .libs .json .Json .{stringify , toJson }
6
6
import play .api .libs .json ._
7
7
import scommons .api .http .ApiHttpClient ._
@@ -12,6 +12,7 @@ import scala.concurrent.duration._
12
12
13
13
class ApiHttpClientSpec extends AsyncFlatSpec
14
14
with Matchers
15
+ with Inside
15
16
with AsyncMockFactory {
16
17
17
18
private val baseUrl = " http://test.api.client"
@@ -143,14 +144,17 @@ class ApiHttpClientSpec extends AsyncFlatSpec
143
144
it should " fail if timeout when parseResponse" in {
144
145
// given
145
146
val url = s " /some/url "
146
- val client = new TestHttpClient (null )
147
147
148
148
// when
149
- val ex = the[Exception ] thrownBy {
150
- client .parseResponse[TestRespData ](url, None )
149
+ val ex = the[ApiHttpTimeoutException ] thrownBy {
150
+ ApiHttpClient .parseResponse[TestRespData ](url, None )
151
151
}
152
152
153
153
// then
154
+ inside(ex) { case ApiHttpTimeoutException (resUrl) =>
155
+ resUrl shouldBe url
156
+ }
157
+
154
158
val message = ex.getMessage
155
159
message should include(" Request timed out, unable to get timely response" )
156
160
message should include(url)
@@ -162,14 +166,22 @@ class ApiHttpClientSpec extends AsyncFlatSpec
162
166
val statusCode = 200
163
167
val data = """ {"id": 1, "missing": "name"}"""
164
168
val response = ApiHttpResponse (statusCode, data)
165
- val client = new TestHttpClient (null )
166
169
167
170
// when
168
- val ex = the[Exception ] thrownBy {
169
- client .parseResponse[TestRespData ](url, Some (response))
171
+ val ex = the[ApiHttpStatusException ] thrownBy {
172
+ ApiHttpClient .parseResponse[TestRespData ](url, Some (response))
170
173
}
171
174
172
175
// then
176
+ inside(ex) { case ApiHttpStatusException (error, resUrl, status, body) =>
177
+ error shouldBe {
178
+ " Fail to parse http response, error: List((/name,List(JsonValidationError(List(error.path.missing),WrappedArray()))))"
179
+ }
180
+ resUrl shouldBe url
181
+ status shouldBe statusCode
182
+ body shouldBe data
183
+ }
184
+
173
185
val message = ex.getMessage
174
186
message should include(s " url: $url" )
175
187
message should include(s " status: $statusCode" )
@@ -183,14 +195,20 @@ class ApiHttpClientSpec extends AsyncFlatSpec
183
195
val statusCode = 400
184
196
val data = " testData"
185
197
val response = ApiHttpResponse (statusCode, data)
186
- val client = new TestHttpClient (null )
187
198
188
199
// when
189
- val ex = the[Exception ] thrownBy {
190
- client .parseResponse[List [TestRespData ]](url, Some (response))
200
+ val ex = the[ApiHttpStatusException ] thrownBy {
201
+ ApiHttpClient .parseResponse[List [TestRespData ]](url, Some (response))
191
202
}
192
203
193
204
// then
205
+ inside(ex) { case ApiHttpStatusException (error, resUrl, status, body) =>
206
+ error shouldBe " Received error response"
207
+ resUrl shouldBe url
208
+ status shouldBe statusCode
209
+ body shouldBe data
210
+ }
211
+
194
212
val message = ex.getMessage
195
213
message should include(s " url: $url" )
196
214
message should include(s " status: $statusCode" )
@@ -201,10 +219,9 @@ class ApiHttpClientSpec extends AsyncFlatSpec
201
219
// given
202
220
val respData = List (TestRespData (1 , " test" ))
203
221
val response = ApiHttpResponse (200 , stringify(toJson(respData)))
204
- val client = new TestHttpClient (null )
205
222
206
223
// when
207
- val result = client .parseResponse[List [TestRespData ]](s " /api/url " , Some (response))
224
+ val result = ApiHttpClient .parseResponse[List [TestRespData ]](s " /api/url " , Some (response))
208
225
209
226
// then
210
227
result shouldBe respData
@@ -214,10 +231,9 @@ class ApiHttpClientSpec extends AsyncFlatSpec
214
231
// given
215
232
val respData = TestRespData (1 , " test" )
216
233
val response = ApiHttpResponse (500 , stringify(toJson(respData)))
217
- val client = new TestHttpClient (null )
218
234
219
235
// when
220
- val result = client .parseResponse[TestRespData ](s " /api/url " , Some (response))
236
+ val result = ApiHttpClient .parseResponse[TestRespData ](s " /api/url " , Some (response))
221
237
222
238
// then
223
239
result shouldBe respData
0 commit comments