I started with a boilerplate http4k project generated by the Project Wizard.
I have a Web App user interface that needs to make REST calls to the http4k backend.
This initially failed because CORS is required.
As a result, I added the CORS Filter to the http4k project to get the Web App REST calls working.
Alas, while CORS enabled the Web App UI to talk to the http4k server, this broke the simple Unit Test.
I get an error like:
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
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