How to test POST request?
See original GitHub issueHi!
I wrote a test:
object AdminAuthHandlerTest : Spek({
with(ktorTestEngine()) {
describe("A sign-in endpoint") {
lateinit var call: TestApplicationCall
beforeGroup {
call = handleRequest(HttpMethod.Post, "/api/admin/auth/sign-in") {
addHeader(HttpHeaders.Accept, ContentType.Text.Plain.contentType)
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.contentType)
setBody(jsonAsString(AdminSignInRequest("admin", "admin123")))
}
}
it("should return 200") {
println("${call.request.bodyChannel.asString()} ${call.requestHandled}")
assertThat(call.response.status()).isEqualTo(HttpStatusCode.OK)
}
it("should return token in response body") {
assertThat(call.response.content).contains("token")
}
it("should return profile object in response body") {
assertThat(call.response.content).contains("profile")
}
it("should set admin in call") {
assertThat(call.admin).isNotNull()
}
}
}
})
My test fails. In response, I always get 400 code, but I provide correct data in setBody method. When I do the request from Postman (or my client app) with the same data it works correctly.
fun jsonAsString(any: Any): String {
return Gson().toJson(any)
}
I cannot find any information about a similar situation in docs. Do you have any idea what do I wrong?
Issue Analytics
- State:
- Created 3 years ago
- Comments:17 (6 by maintainers)
Top Results From Across the Web
HTTP test server accepting GET/POST requests - Stack Overflow
I need a live test server that accepts my requests for basic information via HTTP GET and also allows me to POST (even...
Read more >Online API Testing Tool | Test Your API Online
ReqBin API Tester is a free online API testing tool. Test your API, website or web service by sending REST, SOAP and HTTP...
Read more >An Easy Way to Test HTTP Requests During Development
You can also initiate a test by importing a request from a WADL (Web Application Description Language) file, which provides a machine-readable ...
Read more >Post Request with Postman - How to Do it ( API Testing Series )
In This Tutorial, You are going to learn about the Post Example postman, Basically, Post How to perform POST Request using Postman &...
Read more >Perform HTTP GET and POST Requests MockMvc to Test a ...
This is the second video of the MockMvc mini-series. It covers testing REST API endpoints with MockMvc.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@LookBad the root cause is
BadContentTypeFormatException
that is thrown, because of how you addContent-Type
header in theapp/test/src/handler/api/AdminAuthHandlerTest.kt
:contentType
is a property ofContentType
class that represents the first part of the media type — in your caseapplication
(fromapplication/json
). To fix this you can replace property access withtoString()
method call:Wow! Thank you! I love it. It should be better documented.