Alice
11/28/2018, 4:34 AM@Test
fun `can register user and get jwt`() = withApplication {
main()
val newUser = NewUser("someusername", "someemail", "Tr0ub4dor83")
val req = handleRequest(<http://HttpMethod.Post|HttpMethod.Post>, "/api/users/register") {
setBody(
Gson().toJson(newUser)
)
}
println(req.response.status().toString())
println(req.response.headers)
println(req.response.content)
val token = req.response.headers["Authorization"]
println(token)
}
am I doing something wrong without realizing?robin
11/28/2018, 8:13 AMmain
function, and the second one where you do your testing. Right now, you're doing both inside the testing lambda, which won't work properly. Without having tested it, your code should probably look closer to this:
@Test
fun `can register user and get jwt`() = withApplication({ main() }) {
val newUser = NewUser("someusername", "someemail", "Tr0ub4dor83")
val req = handleRequest(<http://HttpMethod.Post|HttpMethod.Post>, "/api/users/register") {
setBody(
Gson().toJson(newUser)
)
}
println(req.response.status().toString())
println(req.response.headers)
println(req.response.content)
val token = req.response.headers["Authorization"]
println(token)
}
Alice
11/28/2018, 3:34 PMrobin
11/28/2018, 5:10 PMAlice
11/28/2018, 5:28 PMhttp://shekels.wtf/i/P5etiGC.pngâ–¾
@Test
fun `can register user and get jwt`() = withApplication({ main() }) {
val newUser = NewUser("someusername", "someemail", "Tr0ub4dor83")
val req = handleRequest(<http://HttpMethod.Post|HttpMethod.Post>, "/api/users/register") {
setBody(
Gson().toJson(newUser)
)
}
println(req.response.status().toString())
println(req.response.headers)
println(req.response.content)
val token = req.response.headers["Authorization"]
println(token)
}
Alice
11/28/2018, 5:31 PMhttp://shekels.wtf/i/PxODe11.pngâ–¾
robin
11/28/2018, 5:35 PMApplication
. And that's what it has to be for the withApplication
function to work.
So you'll probably have an embeddedServer { ... }
-call in your main function, correct? If so, you need to factor the lambda body you're passing to that function out into its on function that's defined like this: fun Application.mainModule() { /* lambda body goes here */ }
, then call it from the main function like that: embeddedServer { mainModule() }
.
This mainModule
function you can now use in the test: fun test() = withTestApplication({ mainModule()}) { ... }
Alice
11/28/2018, 5:39 PMAlice
11/28/2018, 5:42 PMAlice
11/28/2018, 5:42 PMAlice
11/28/2018, 5:42 PM@KtorExperimentalAPI
@KtorExperimentalLocationsAPI
fun Application.module() {
install(DefaultHeaders)
install(CallLogging)
install(Locations)
install(ContentNegotiation) {
gson {
setPrettyPrinting()
serializeNulls()
setDateFormat(DateFormat.FULL)
}
}
install(Authentication) {
jwt("jwt") {
realm = config.realm
verifier(authenticationService.verifier)
validate { credential ->
credential.payload.getClaim("id").asLong()?.let { id ->
transaction {
User.findById(id)
}
}
}
}
}
install(Routing) {
get("/a") {
call.respondText("d", ContentType.Text.Html)
}
user(userService, authenticationService)
}
}
@KtorExperimentalLocationsAPI
fun main() {
userService.hi()
embeddedServer(
Netty,
8080,
module = Application::module
).start()
}
Alice
11/28/2018, 5:42 PMAlice
11/28/2018, 5:43 PMrobin
11/28/2018, 5:48 PM@Test
fun `can register user and get jwt`() = withApplication(Application::module) {
val newUser = NewUser("someusername", "someemail", "Tr0ub4dor83")
val req = handleRequest(<http://HttpMethod.Post|HttpMethod.Post>, "/api/users/register") {
setBody(
Gson().toJson(newUser)
)
}
println(req.response.status().toString())
println(req.response.headers)
println(req.response.content)
val token = req.response.headers["Authorization"]
println(token)
}
The important thing is that withTestApplication
does not actually start your server like your main function does, but instead starts a dummy server that does no actual HTTP processing and applies your configuration (your module-function) to it. Makes for faster testing without having to start the whole server 🙂Alice
11/28/2018, 6:36 PMhttp://shekels.wtf/i/gPEKHjk.pngâ–¾
Alice
11/28/2018, 6:36 PMAlice
11/28/2018, 6:37 PMrobin
11/28/2018, 6:37 PMwithTestApplication
, not withApplication
🙈Alice
11/28/2018, 6:38 PM