swifter icon indicating copy to clipboard operation
swifter copied to clipboard

Content type not set correctly for `.badRequest` case

Open jerbeers opened this issue 7 years ago • 2 comments

If I have a test that needs to send a body with a .badRequest response, the content-type header is not set correctly. The headers() method on HttpResponse should evaluate the body of .badRequest the same way it does for .ok. It's a bit complicated by the fact that the associated data for .ok is not optional and .badRequest is optional, but if badRequest does have a body, it should be handled the same way.

jerbeers avatar Oct 23 '18 21:10 jerbeers

You can always use the HttpResponse.raw and provide your own Headers. See the following example:


let statusCode = 400
let reasonPhrase = "Bad Request"
let headers = ["Content-Type": "application/json"]

let response =  HttpResponse.raw(statusCode, reasonPhrase , headers,  { writer in
    // jsonData represent your JSON string to return converted to `Data` 
    // or any other implementing `HttpResponseBodyWriter`
    try writer.write(jsonData)
})

Vkt0r avatar Oct 24 '18 03:10 Vkt0r

Yeah, I used that as a workaround, but it still seems like the code should automatically handle the header correctly, so I filed the issue. Thanks!

jerbeers avatar Oct 24 '18 21:10 jerbeers