Anthony Whitford
05/27/2023, 5:00 AMPing test() FAILED
org.opentest4j.AssertionFailedError: expected: <HTTP/1.1 200 OK
access-control-allow-origin: null
access-control-allow-headers:
access-control-allow-methods: GET, POST
pong> but was: <HTTP/1.1 200 OK
pong>
at app//org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
at app//org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
at app//org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:177)
at app//org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1142)
at app//com.perusal.Perusalhttp4kTest.Ping test(Perusalhttp4kTest.kt:14)
1 test completed, 1 failed
How can I enable CORS but also make the Unit Test work?
I tried adding headers like:
.header("access-control-request-method", "GET")
.header("origin", "<http://localhost:3000>"),
but I get a similar failure:
Ping test() FAILED
org.opentest4j.AssertionFailedError: expected: <HTTP/1.1 200 OK
access-control-allow-origin: null
access-control-allow-headers:
access-control-allow-methods: GET, POST
access-control-request-method: GET
origin: <http://localhost:3000>
pong> but was: <HTTP/1.1 200 OK
pong>
at app//org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
at app//org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
at app//org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:177)
at app//org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1142)
at app//com.perusal.Perusalhttp4kTest.Ping test(Perusalhttp4kTest.kt:14)
1 test completed, 1 failed
dave
05/27/2023, 6:54 AMdave
05/27/2023, 6:56 AMdave
05/27/2023, 6:58 AMAnthony Whitford
05/27/2023, 7:12 AMval corsPolicy = CorsPolicy(
originPolicy = OriginPolicy.Only("<http://localhost:3000>"), // TODO Replace with the appropriate client origin(s)
headers = emptyList(), // listOf("Content-Type", "Authorization"), // TODO Consider adding back Authorization
methods = listOf(GET, POST /*, Method.PUT, Method.DELETE */) // TODO Double Check Completeness
)
val corsMiddleware = Cors(corsPolicy)
val app: HttpHandler = corsMiddleware.then(
routes(
"/ping" bind GET to {
Response(OK).body("pong")
},
Anthony Whitford
05/27/2023, 7:13 AM@Test
fun `Ping test`() {
assertEquals(app(
Request(GET, "/ping"))
.header("access-control-request-method", "GET")
.header("origin", "<http://localhost:3000>"),
Response(OK).body("pong"))
}
(I added the header thinking I was consistent with CORS, but perhaps not.)Anthony Whitford
05/27/2023, 7:14 AMdave
05/27/2023, 8:23 AMdave
05/27/2023, 8:24 AM@Test
fun `Ping test`() {
assertEquals(app(Request(GET, "/ping")),
Response(OK).body("pong")
.header("access-control-request-method", "GET")
.header("origin", "<http://localhost:3000>")
)
}
Anthony Whitford
05/27/2023, 2:48 PMPing test() FAILED
org.opentest4j.AssertionFailedError: expected: <HTTP/1.1 200 OK
access-control-allow-origin: null
access-control-allow-headers:
access-control-allow-methods: GET, POST
pong> but was: <HTTP/1.1 200 OK
access-control-request-method: GET
origin: <http://localhost:3000>
pong>
at app//org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
at app//org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
at app//org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:177)
at app//org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1142)
at app//com.perusal.Perusalhttp4kTest.Ping test(Perusalhttp4kTest.kt:13)
1 test completed, 1 failed
dave
05/27/2023, 2:57 PMdave
05/27/2023, 3:05 PMpackage org.http4k.connect.openai.auth.user
import org.http4k.core.HttpHandler
import org.http4k.core.Method.GET
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.core.then
import org.http4k.filter.CorsPolicy
import org.http4k.filter.Only
import org.http4k.filter.OriginPolicy
import org.http4k.filter.ServerFilters
import org.http4k.routing.bind
import org.http4k.routing.routes
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
class FooTest {
@Test
fun `test`() {
val corsPolicy = CorsPolicy(
originPolicy = OriginPolicy.Only("localhost:3000"), // TODO Replace with the appropriate client origin(s)
headers = emptyList(), // listOf("Content-Type", "Authorization"), // TODO Consider adding back Authorization
methods = listOf(GET /*, Method.PUT, Method.DELETE */) // TODO Double Check Completeness
)
val corsMiddleware = ServerFilters.Cors(corsPolicy)
val app: HttpHandler = corsMiddleware.then(
routes(
"/ping" bind GET to { Response(OK).body("pong") }
)
)
val expected: Response = Response(OK).body("pong")
.header("access-control-allow-origin", "localhost:3000")
.header("access-control-allow-headers", "")
.header("access-control-allow-methods", "GET")
val actual: Response = app(
Request(GET, "/ping").header("origin", "localhost:3000")
)
assertEquals(expected, actual)
}
}
Anthony Whitford
05/27/2023, 3:29 PMfun `Ping test`() {
assertEquals(
Response(OK).body("pong")
.header("access-control-allow-origin", "localhost:3000")
.header("access-control-allow-headers", "")
.header("access-control-allow-methods", "GET")
,
app(
Request(GET, "/ping")
.header("origin", "localhost:3000")
)
)
}
dave
05/27/2023, 3:31 PMAnthony Whitford
05/27/2023, 3:35 PMAccess to fetch at 'http://localhost:9000/jdbc' from origin 'http://localhost:3000' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'null' that is not equal to the supplied origin. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.Why would Chrome be sending null? 🤔
Anthony Whitford
05/27/2023, 3:45 PMAnthony Whitford
05/27/2023, 3:51 PMfun `Ping test`() {
assertEquals(
Response(OK).body("pong")
.header("access-control-allow-origin", "null")
.header("access-control-allow-headers", "")
.header("access-control-allow-methods", "GET")
,
app(
Request(GET, "/ping")
.header("origin", "localhost:3000")
)
)
}
Anthony Whitford
05/27/2023, 3:52 PM.header("access-control-allow-origin", "null")
for expected.Anthony Whitford
05/27/2023, 3:59 PMPOST
entry point to my server, then the access-control-allow-methods
value changes from GET
to GET, POST
-- this will require adjustment to the tests.
Is it best practice to include the CORS policy for Unit Testing? I wonder if unit tests should simply circumvent the CORS policy.dave
05/27/2023, 4:15 PMfun main() {
val endpoint = "/foo" bind GET to { req: Request -> Response(OK).body("foobar") }
val api = routes(endpoint)
val app = routes(
"/web" bind Cors(UnsafeGlobalPermissive).then(api),
"/api" bind ServerFilters.BearerAuth("apiKey").then(api)
)
endpoint(Request(GET, "/foo"))
api(Request(GET, "/foo"))
app(Request(GET, "/web/foo"))
app(Request(GET, "/app/foo"))
JavaHttpClient()(Request(GET, "<http://localhost:9000>"))
}
dave
05/27/2023, 4:16 PMdave
05/27/2023, 4:18 PMdave
05/27/2023, 4:21 PMval incomingStack: Filter = DebuggingFilters.PrintRequestAndResponse()
.then(Cors(UnsafeGlobalPermissive))
.then(ResponseFilters.ReportHttpTransaction {
println("${it.request} took ${it.duration}")
})
val forTestingStack = incomingStack.then { req: Request -> Response(OK) }
val appWithStack = incomingStack.then(app)