6
6
7
7
package com.datadog.benchmark.sample.network
8
8
9
- import fr.xgouchet.elmyr.junit5.ForgeExtension
10
9
import io.ktor.client.HttpClient
11
10
import io.ktor.client.engine.mock.MockEngine
12
11
import io.ktor.client.engine.mock.respond
@@ -24,103 +23,93 @@ import kotlinx.serialization.json.Json
24
23
import org.junit.jupiter.api.Assertions.assertEquals
25
24
import org.junit.jupiter.api.Assertions.assertInstanceOf
26
25
import org.junit.jupiter.api.Test
27
- import org.junit.jupiter.api.extension.ExtendWith
28
- import org.junit.jupiter.api.extension.Extensions
29
- import org.mockito.junit.jupiter.MockitoExtension
30
- import org.mockito.junit.jupiter.MockitoSettings
31
- import org.mockito.quality.Strictness
32
-
33
- @Extensions(
34
- ExtendWith (MockitoExtension ::class ),
35
- ExtendWith (ForgeExtension ::class )
36
- )
37
- @MockitoSettings(strictness = Strictness .LENIENT )
26
+
38
27
class KtorHttpClientUtilsTest {
39
28
@Test
40
29
fun `M return deserialized body W safeGet() { server returns 200 and proper json }` () {
41
- val mockEngine = MockEngine { request ->
30
+ // Given
31
+ val mockEngine = MockEngine {
42
32
respond(
43
33
content = Json .Default .encodeToString(ResponseData (1 )),
44
34
status = HttpStatusCode .OK ,
45
35
headers = headersOf(HttpHeaders .ContentType , " application/json" )
46
36
)
47
37
}
48
38
49
- val client = HttpClient (mockEngine) {
50
- install(ContentNegotiation ) {
51
- json(Json { ignoreUnknownKeys = true })
52
- }
53
- }
39
+ val client = createClient(mockEngine)
54
40
55
41
val url = URLBuilder (" " ).build()
56
42
57
- runBlocking {
58
- val response = client.safeGet<ResponseData >(url)
59
- assertEquals(ResponseData (1 ), response.optionalResult)
60
- }
43
+ // When
44
+ val response = runBlocking { client.safeGet<ResponseData >(url) }
45
+
46
+ // Then
47
+ assertEquals(ResponseData (1 ), response.optionalResult)
61
48
}
62
49
63
50
@Test
64
51
fun `M return ServerError W safeGet() { server returns 500 }` () {
65
- val mockEngine = MockEngine { request ->
52
+ // Given
53
+ val mockEngine = MockEngine {
66
54
respondError(status = HttpStatusCode .InternalServerError )
67
55
}
68
56
69
- val client = HttpClient (mockEngine) {
70
- install(ContentNegotiation ) {
71
- json(Json { ignoreUnknownKeys = true })
72
- }
73
- }
57
+ val client = createClient(mockEngine)
74
58
75
59
val url = URLBuilder (" " ).build()
76
60
77
- runBlocking {
78
- val response = client.safeGet<ResponseData >(url)
79
- assertEquals(KtorHttpResponse .ServerError (HttpStatusCode .InternalServerError ), response)
80
- }
61
+ // When
62
+ val response = runBlocking { client.safeGet<ResponseData >(url) }
63
+
64
+ // Then
65
+ assertEquals(KtorHttpResponse .ServerError (HttpStatusCode .InternalServerError ), response)
81
66
}
82
67
83
68
@Test
84
69
fun `M return ClientError W safeGet() { server returns 404 }` () {
85
- val mockEngine = MockEngine { request ->
70
+ // Given
71
+ val mockEngine = MockEngine {
86
72
respondError(status = HttpStatusCode .NotFound )
87
73
}
88
74
89
- val client = HttpClient (mockEngine) {
90
- install(ContentNegotiation ) {
91
- json(Json { ignoreUnknownKeys = true })
92
- }
93
- }
75
+ val client = createClient(mockEngine)
94
76
95
77
val url = URLBuilder (" " ).build()
96
78
97
- runBlocking {
98
- val response = client.safeGet<ResponseData >(url)
99
- assertEquals(KtorHttpResponse .ClientError (HttpStatusCode .NotFound ), response)
100
- }
79
+ // When
80
+ val response = runBlocking { client.safeGet<ResponseData >(url) }
81
+
82
+ // Then
83
+ assertEquals(KtorHttpResponse .ClientError (HttpStatusCode .NotFound ), response)
101
84
}
102
85
103
86
@Test
104
87
fun `M return UnknownException W safeGet() { server returns 200 and invalid json }` () {
105
- val mockEngine = MockEngine { request ->
88
+ // Given
89
+ val mockEngine = MockEngine {
106
90
respond(
107
91
content = " some_invalid_json" ,
108
92
status = HttpStatusCode .OK ,
109
93
headers = headersOf(HttpHeaders .ContentType , " application/json" )
110
94
)
111
95
}
112
96
113
- val client = HttpClient (mockEngine) {
114
- install(ContentNegotiation ) {
115
- json(Json { ignoreUnknownKeys = true })
116
- }
117
- }
97
+ val client = createClient(mockEngine)
118
98
119
99
val url = URLBuilder (" " ).build()
120
100
121
- runBlocking {
122
- val response = client.safeGet<ResponseData >(url)
123
- assertInstanceOf(KtorHttpResponse .UnknownException ::class .java, response)
101
+ // When
102
+ val response = runBlocking { client.safeGet<ResponseData >(url) }
103
+
104
+ // Then
105
+ assertInstanceOf(KtorHttpResponse .UnknownException ::class .java, response)
106
+ }
107
+
108
+ private fun createClient (engine : MockEngine ): HttpClient {
109
+ return HttpClient (engine) {
110
+ install(ContentNegotiation ) {
111
+ json(Json { ignoreUnknownKeys = true })
112
+ }
124
113
}
125
114
}
126
115
}
0 commit comments