Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2941: Cors, accessControlAllowHeaders in option request (prepend pre… #2943

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Changes from all 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
@@ -35,7 +35,73 @@ object CorsSpec extends ZIOHttpSpec with HttpAppTestExtensions {
Response(Status.InternalServerError, body = Body.fromString(cause.prettyPrint))
} @@ cors(CorsConfig(allowedMethods = AccessControlAllowMethods(Method.GET)))

val appAllowAllHeaders = Routes(
Method.GET / "success" -> handler(Response.ok),
).handleErrorCause { cause =>
Response(Status.InternalServerError, body = Body.fromString(cause.prettyPrint))
} @@ cors(
CorsConfig(
allowedOrigin = { case _ =>
Some(Header.AccessControlAllowOrigin.All)
},
allowedMethods = Header.AccessControlAllowMethods.All,
allowedHeaders = Header.AccessControlAllowHeaders.All,
),
)

val appNoServerHeaders = Routes(
Method.GET / "success" -> handler(Response.ok),
).handleErrorCause { cause =>
Response(Status.InternalServerError, body = Body.fromString(cause.prettyPrint))
} @@ cors(
CorsConfig(
allowedOrigin = { case _ =>
Some(Header.AccessControlAllowOrigin.All)
},
allowedMethods = Header.AccessControlAllowMethods.All,
allowedHeaders = Header.AccessControlAllowHeaders.None,
),
)

override def spec = suite("CorsSpec")(
test("OPTIONS request with allowAllHeaders server config") {
val request =
Request
.options(URL(Path.root / "success"))
.copy(
headers = Headers(
Header.Origin("http", "test-env"),
Header.AccessControlRequestMethod(Method.GET),
),
)

for {
res <- appAllowAllHeaders.runZIO(request)
} yield assertTrue(
extractStatus(res) == Status.NoContent,
res.hasHeader(Header.AccessControlAllowCredentials.Allow),
res.hasHeader(Header.AccessControlAllowHeaders.All),
)
},
test("OPTIONS request with no headers allowed in server config") {
val request =
Request
.options(URL(Path.root / "success"))
.copy(
headers = Headers(
Header.Origin("http", "test-env"),
Header.AccessControlRequestMethod(Method.GET),
),
)

for {
res <- appNoServerHeaders.runZIO(request)
} yield assertTrue(
extractStatus(res) == Status.NoContent,
res.hasHeader(Header.AccessControlAllowCredentials.Allow),
!res.hasHeader(Header.AccessControlAllowHeaders.All),
)
},
test("OPTIONS request") {
val request = Request
.options(URL(Path.root / "success"))
2 changes: 1 addition & 1 deletion zio-http/shared/src/main/scala/zio/http/Middleware.scala
Original file line number Diff line number Diff line change
@@ -170,7 +170,7 @@ object Middleware extends HandlerAspects {

new Middleware[Any] {
def apply[Env1, Err](routes: Routes[Env1, Err]): Routes[Env1, Err] =
(routes @@ aspect) :+ optionsRoute
optionsRoute +: (routes @@ aspect)
}
}

Loading