Hello, question about HttpsRedirect feature. I jus...
# ktor
v
Hello, question about HttpsRedirect feature. I just install it and all my tests are failed now because handleRequest call are made with http so the request is redirected. Is it possible to have directly https call in handleRequest please ?
1
d
I’m not sure if I understand what’s going on do you have a small snippet with the feature install + route + test that I can try?
v
I don’t have a small snippet but I can show you the problem without I think
To install the feature I just put install(HttpsRedirect)
d
aha
v
I have a route for authentication /authenticate
And a small part of the test is handleRequest(HttpMethod.Get, “/authenticate”) { addHeader(“Host”, “127.0.0.1”) }.let { call -> val location = call.response.headers[“Location”] ?: “” assertEquals( “https://accounts.google.com/o/oauth2/auth?client_id=$configClientID&redirect_uri=http%3A%2F%2F127.0.0.1%2Fauthenticate&scope=email&state=****&response_type=code“, location.maskState() ) val stateInfo = Regex(“state=(\\w+)“).find(location) state = stateInfo!!.groupValues[1] }
but the assert failed because I have https://127.0.0.1/authenticate instead of google address
d
aha
okay, let me check, I think you was able to specify to handleRequest with the https protocol, but have to check how
and it seems that in the obvious pages of the documentation: • https://ktor.io/servers/testing.htmlhttps://ktor.io/features/https-redirect.html nothing is said, so I will update the documentation too
Copy code
handleRequest(HttpMethod.Get, "/", {
    addHeader(HttpHeaders.XForwardedProto, "https")
}).let { call ->
    assertEquals(HttpStatusCode.OK, call.response.status())
}
can you try this?
addHeader(HttpHeaders.XForwardedProto, "https")
you will probably need to add
application.install(XForwardedHeadersSupport)
too for it to work I guess. But you can add it only to the tests without affecting your main app
the other thing I can think of, is to just not install the
HttpsRedirect
when doing the tests. For example by adding a
installHttpsRedirect: Boolean
to the function that installs it and conditionally install it based on that flag
I guess you can also use the uninstall method:
application.uninstall(HttpsRedirect)
v
Sorry I was not here, I will try right now, thanks for your help
👍 1
So this addHeader(HttpHeaders.XForwardedProto, “https”) didn’t work
d
addHeader(HttpHeaders.XForwardedProto, "https")
with or without adding the
application.install(XForwardedHeadersSupport)
?
v
Sorry I forgot to install it
So ok that’s working but can you develop how can I put a flag to just don’t install this feature because it’s will be really more convenient. With the other method, I have to change each test …
v
Ok perfect, problem solved ! Thanks 🙂
d
nice 🙂
v
Just to inform you, I won’t use application.uninstall(HttpsRedirect) but in my case that didn’t work with this line and the feature keep be installed
d
Uhm. That might be a bug then. I’ll investigate. Thanks for informing 👍
👍 1