question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to test POST request?

See original GitHub issue

Hi!

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:closed
  • Created 3 years ago
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

10reactions
Stexxecommented, Jul 7, 2020

@LookBad the root cause is BadContentTypeFormatException that is thrown, because of how you add Content-Type header in the app/test/src/handler/api/AdminAuthHandlerTest.kt:

addHeader(HttpHeaders.ContentType, ContentType.Application.Json.contentType)

contentType is a property of ContentType class that represents the first part of the media type — in your case application (from application/json). To fix this you can replace property access with toString() method call:

addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
0reactions
LookBadcommented, Jul 12, 2020

Wow! Thank you! I love it. It should be better documented.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found