Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ internal fun PluginBuilder<CORSConfig>.buildPlugin() {
val allowNonSimpleContentTypes: Boolean = pluginConfig.allowNonSimpleContentTypes
val headersList = pluginConfig.headers.filterNot { it in CORSConfig.CorsSimpleRequestHeaders }
.let { if (allowNonSimpleContentTypes) it + HttpHeaders.ContentType else it }
val methodsListHeaderValue = methods.filterNot { it in CORSConfig.CorsDefaultMethods }
.map { it.value }
.sorted()
.joinToString(", ")
val methodsListHeaderValue = methods.map { it.value }.sorted().joinToString(", ")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I noticed in the other PR is they included a distinct() here - wondering if we'll end up with duplicates?

val maxAgeHeaderValue = pluginConfig.maxAgeInSeconds.let { if (it > 0) it.toString() else null }
val exposedHeaders = when {
pluginConfig.exposedHeaders.isNotEmpty() -> pluginConfig.exposedHeaders.sorted().joinToString(", ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1592,4 +1592,32 @@ class CORSTest {
assertEquals(response.status, HttpStatusCode.Forbidden)
}
}

@Test
fun testDefaultMethodsIncludedInAllowMethodsHeader() = testApplication {
install(CORS) {
allowMethod(HttpMethod.Post)
anyHost()
}

routing {
post("/") {
call.respond("OK")
}
}

val response = client.options("/") {
header(HttpHeaders.Origin, "https://example.com")
header(HttpHeaders.AccessControlRequestMethod, "POST")
}

assertEquals(HttpStatusCode.OK, response.status)
val allowMethodsHeader = response.headers[HttpHeaders.AccessControlAllowMethods]

assertNotNull(allowMethodsHeader, "Access-Control-Allow-Methods header should not be null")
assertTrue(
allowMethodsHeader.contains("POST"),
"Expected Access-Control-Allow-Methods to include POST, but got: $allowMethodsHeader"
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have an assertEquals here to ensure the default methods are included as well in a predictable way.

}
}