-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KTOR-6108 KtorServlet does not support yaml configuration (#3696)
- Loading branch information
Showing
12 changed files
with
321 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
ktor-server/ktor-server-servlet-jakarta/jvm/test-resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ktor: | ||
deployment: | ||
port: 1234 | ||
property: a |
4 changes: 4 additions & 0 deletions
4
ktor-server/ktor-server-servlet-jakarta/jvm/test-resources/custom-config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ktor: | ||
deployment: | ||
port: 1234 | ||
property: a-custom |
142 changes: 142 additions & 0 deletions
142
ktor-server/ktor-server-servlet-jakarta/jvm/test/io/ktor/tests/servlet/jakarta/ConfigTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.tests.servlet.jakarta | ||
|
||
import io.ktor.server.application.* | ||
import io.ktor.server.engine.* | ||
import io.ktor.server.servlet.jakarta.* | ||
import io.ktor.server.servlet.jakarta.ServletApplicationEngine.Companion.ApplicationEngineEnvironmentAttributeKey | ||
import io.ktor.server.servlet.jakarta.ServletApplicationEngine.Companion.ApplicationEnginePipelineAttributeKey | ||
import io.mockk.* | ||
import jakarta.servlet.* | ||
import jakarta.servlet.http.* | ||
import java.util.* | ||
import kotlin.test.* | ||
|
||
class ConfigTest { | ||
@Test | ||
fun resolveParametersFromCustomConfig() { | ||
val engine = ServletApplicationEngine() | ||
val pipeline = EnginePipeline() | ||
|
||
var interceptorCalled = false | ||
pipeline.intercept(EnginePipeline.Call) { | ||
val value = call.application.environment.config.property("var").getString() | ||
assertEquals("test", value) | ||
interceptorCalled = true | ||
} | ||
|
||
val context = mockk<ServletContext> { | ||
every { getAttribute(ApplicationEnginePipelineAttributeKey) } returns pipeline | ||
every { initParameterNames } returns Collections.enumeration(listOf("io.ktor.ktor.config")) | ||
every { classLoader } returns this::class.java.classLoader | ||
every { contextPath } returns "/" | ||
every { getAttribute(ApplicationEngineEnvironmentAttributeKey) } returns null | ||
} | ||
|
||
val config = mockk<ServletConfig> { | ||
every { getInitParameter("io.ktor.ktor.config") } returns "test.conf" | ||
every { servletContext } returns context | ||
every { servletName } returns "ktor-test" | ||
every { initParameterNames } returns Collections.enumeration(emptyList()) | ||
} | ||
|
||
engine.init(config) | ||
engine.service(getRequest(), getResponse()) | ||
engine.destroy() | ||
assertTrue(interceptorCalled) | ||
} | ||
|
||
@Test | ||
fun resolveYamlFromCustomConfig() { | ||
val engine = ServletApplicationEngine() | ||
val pipeline = EnginePipeline() | ||
|
||
var interceptorCalled = false | ||
pipeline.intercept(EnginePipeline.Call) { | ||
val value = call.application.environment.config.property("property").getString() | ||
assertEquals("a-custom", value) | ||
interceptorCalled = true | ||
} | ||
|
||
val context = mockk<ServletContext> { | ||
every { getAttribute(ApplicationEnginePipelineAttributeKey) } returns pipeline | ||
every { initParameterNames } returns Collections.enumeration(listOf("io.ktor.ktor.config")) | ||
every { classLoader } returns this::class.java.classLoader | ||
every { contextPath } returns "/" | ||
every { getAttribute(ApplicationEngineEnvironmentAttributeKey) } returns null | ||
} | ||
|
||
val config = mockk<ServletConfig> { | ||
every { getInitParameter("io.ktor.ktor.config") } returns "custom-config.yaml" | ||
every { servletContext } returns context | ||
every { servletName } returns "ktor-test" | ||
every { initParameterNames } returns Collections.enumeration(emptyList()) | ||
} | ||
|
||
engine.init(config) | ||
engine.service(getRequest(), getResponse()) | ||
engine.destroy() | ||
assertTrue(interceptorCalled) | ||
} | ||
|
||
@Test | ||
fun resolveYamlFromDefaultConfig() { | ||
val engine = ServletApplicationEngine() | ||
val pipeline = EnginePipeline() | ||
|
||
var interceptorCalled = false | ||
pipeline.intercept(EnginePipeline.Call) { | ||
val value = call.application.environment.config.property("property").getString() | ||
assertEquals("a", value) | ||
interceptorCalled = true | ||
} | ||
|
||
val context = mockk<ServletContext> { | ||
every { getAttribute(ApplicationEnginePipelineAttributeKey) } returns pipeline | ||
every { initParameterNames } returns Collections.enumeration(emptyList()) | ||
every { classLoader } returns this::class.java.classLoader | ||
every { contextPath } returns "/" | ||
every { getAttribute(ApplicationEngineEnvironmentAttributeKey) } returns null | ||
} | ||
|
||
val config = mockk<ServletConfig> { | ||
every { getInitParameter("io.ktor.ktor.config") } returns "custom-config.yaml" | ||
every { servletContext } returns context | ||
every { servletName } returns "ktor-test" | ||
every { initParameterNames } returns Collections.enumeration(emptyList()) | ||
} | ||
|
||
engine.init(config) | ||
engine.service(getRequest(), getResponse()) | ||
engine.destroy() | ||
assertTrue(interceptorCalled) | ||
} | ||
|
||
private fun getResponse(): HttpServletResponse { | ||
val error = slot<String>() | ||
return mockk { | ||
every { sendError(500, capture(error)) } answers { | ||
fail(error.captured) | ||
} | ||
every { isCommitted } returns false | ||
} | ||
} | ||
|
||
private fun getRequest(): HttpServletRequest { | ||
return mockk { | ||
every { isAsyncSupported } returns false | ||
every { queryString } returns "" | ||
every { requestURI } returns "/" | ||
every { protocol } returns "HTTP/1.1" | ||
every { scheme } returns "http" | ||
every { method } returns "GET" | ||
every { serverPort } returns 80 | ||
every { serverName } returns "server" | ||
every { remoteHost } returns "localhost" | ||
every { attributeNames } returns java.util.Collections.enumeration(emptyList()) | ||
} | ||
} | ||
} |
73 changes: 0 additions & 73 deletions
73
ktor-server/ktor-server-servlet-jakarta/jvm/test/io/ktor/tests/servlet/jakarta/HoconTest.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
ktor-server/ktor-server-servlet/jvm/test-resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ktor: | ||
deployment: | ||
port: 1234 | ||
property: a |
4 changes: 4 additions & 0 deletions
4
ktor-server/ktor-server-servlet/jvm/test-resources/custom-config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ktor: | ||
deployment: | ||
port: 1234 | ||
property: a-custom |
Oops, something went wrong.