@@ -36,8 +36,9 @@ import io.ktor.http.contentType
36
36
import io.ktor.serialization.jackson.jackson
37
37
import io.ktor.server.application.Application
38
38
import io.ktor.server.application.install
39
+ import io.ktor.server.config.*
39
40
import io.ktor.server.plugins.statuspages.StatusPages
40
- import io.ktor.server.routing.Routing
41
+ import io.ktor.server.routing.*
41
42
import io.ktor.server.testing.testApplication
42
43
import io.ktor.websocket.Frame
43
44
import io.ktor.websocket.readText
@@ -104,7 +105,7 @@ class GraphQLPluginTest {
104
105
flow: Int!
105
106
}
106
107
""" .trimIndent()
107
- testApplication {
108
+ testModule {
108
109
val response = client.get(" /sdl" )
109
110
assertEquals(HttpStatusCode .OK , response.status)
110
111
assertEquals(expectedSchema, response.bodyAsText().trim())
@@ -113,7 +114,7 @@ class GraphQLPluginTest {
113
114
114
115
@Test
115
116
fun `server should handle valid GET requests` () {
116
- testApplication {
117
+ testModule {
117
118
val response = client.get(" /graphql" ) {
118
119
parameter(" query" , " query HelloQuery(\$ name: String){ hello(name: \$ name) }" )
119
120
parameter(" operationName" , " HelloQuery" )
@@ -126,7 +127,7 @@ class GraphQLPluginTest {
126
127
127
128
@Test
128
129
fun `server should return Method Not Allowed for Mutation GET requests` () {
129
- testApplication {
130
+ testModule {
130
131
val response = client.get(" /graphql" ) {
131
132
parameter(" query" , " mutation { foo }" )
132
133
}
@@ -136,15 +137,15 @@ class GraphQLPluginTest {
136
137
137
138
@Test
138
139
fun `server should return Bad Request for invalid GET requests` () {
139
- testApplication {
140
+ testModule {
140
141
val response = client.get(" /graphql" )
141
142
assertEquals(HttpStatusCode .BadRequest , response.status)
142
143
}
143
144
}
144
145
145
146
@Test
146
147
fun `server should handle valid POST requests` () {
147
- testApplication {
148
+ testModule {
148
149
val client = createClient {
149
150
install(ContentNegotiation ) {
150
151
jackson()
@@ -161,7 +162,7 @@ class GraphQLPluginTest {
161
162
162
163
@Test
163
164
fun `server should handle valid POST batch requests` () {
164
- testApplication {
165
+ testModule {
165
166
val client = createClient {
166
167
install(ContentNegotiation ) {
167
168
jackson()
@@ -185,7 +186,7 @@ class GraphQLPluginTest {
185
186
186
187
@Test
187
188
fun `server should return Bad Request for invalid POST requests with correct content type` () {
188
- testApplication {
189
+ testModule {
189
190
val response = client.post(" /graphql" ) {
190
191
contentType(ContentType .Application .Json )
191
192
}
@@ -195,15 +196,15 @@ class GraphQLPluginTest {
195
196
196
197
@Test
197
198
fun `server should return Unsupported Media Type for POST requests with invalid content type` () {
198
- testApplication {
199
+ testModule {
199
200
val response = client.post(" /graphql" )
200
201
assertEquals(HttpStatusCode .UnsupportedMediaType , response.status)
201
202
}
202
203
}
203
204
204
205
@Test
205
206
fun `server should handle subscription requests` () {
206
- testApplication {
207
+ testModule {
207
208
val client = createClient {
208
209
install(ContentNegotiation ) {
209
210
jackson()
@@ -233,7 +234,7 @@ class GraphQLPluginTest {
233
234
234
235
@Test
235
236
fun `server should provide GraphiQL endpoint` () {
236
- testApplication {
237
+ testModule {
237
238
val response = client.get(" /graphiql" )
238
239
assertEquals(HttpStatusCode .OK , response.status)
239
240
@@ -260,11 +261,19 @@ fun Application.testGraphQLModule() {
260
261
}
261
262
}
262
263
install(io.ktor.server.websocket.WebSockets )
263
- install( Routing ) {
264
+ routing {
264
265
graphQLGetRoute()
265
266
graphQLPostRoute()
266
267
graphQLSubscriptionsRoute()
267
268
graphQLSDLRoute()
268
269
graphiQLRoute()
269
270
}
270
271
}
272
+
273
+ private fun testModule (block : suspend io.ktor.server.testing.ApplicationTestBuilder .() -> kotlin.Unit ) = testApplication {
274
+ environment {
275
+ config = ApplicationConfig ((" application.conf" ))
276
+ }
277
+ block()
278
+ }
279
+
0 commit comments