While writing integration test in spock for my rest api secured with JWT i faced problem with response content type. For swagger or postman there's no problem. It's only happens with integration test.
Log from test start:
18:24:57.101 [Test worker] DEBUG org.springframework.web.client.RestTemplate - HTTP POST http://localhost:8080/login
18:24:57.110 [Test worker] DEBUG org.springframework.web.client.RestTemplate - Accept=[text/plain, application/json, application/*+json, */*]
18:24:57.138 [Test worker] DEBUG org.springframework.web.client.RestTemplate - Writing [{login=test, password=test}] as "application/json"
18:24:57.282 [Test worker] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
18:24:57.284 [Test worker] DEBUG org.springframework.web.client.RestTemplate - Reading to [java.lang.String] as "application/json;charset=ISO-8859-1"
Here is response from server
<200,###BODY###,[Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers", Authorization:###JWT_TOKEN###, X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY", Content-Type:"application/json;charset=ISO-8859-1", Transfer-Encoding:"chunked", Date:"Sun, 31 Jan 2021 16:58:29 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]>
as result i obviously received:
Unable to determine the current character, it is not a string, number, array, or object
Here is my test:
def 'should login player' (){
given:
def credentials = [
"login" : "test",
"password": "test"
]
when:
def response = sendPost(credentials, "login", String.class)
def body = parseResponse(response)
then:
(body.Authentication as String).contains("Bearer")
}
def rest = new RestTemplate()
def headers = new HttpHeaders()
def url = "http://localhost:8080/"
def setup() {
rest.messageConverters.add(new FormHttpMessageConverter())
rest.messageConverters.add(new StringHttpMessageConverter())
headers.setContentType(APPLICATION_JSON)
headers.remove("Accept")
headers.add("Accept", APPLICATION_JSON_VALUE)
}
def sendPost(def body, String endpoint, Class returnType) {
def httpEntity = new HttpEntity<>(body, headers)
rest.postForEntity((url + endpoint), httpEntity, returnType)
}
Can't understand why in log i can see Accept=[text/plain, application/json, application/*+json, /]
if i set only application/json. Even setting contentType to json in successfulyAuthentication method doesn't help.
Any idea what i'm doing wrong?
Sorry for delay.
Yeah the problem was argument of parseResponse. I've been working on whole response instead of only body.
It should looks like def body = parseResponse(response.body)
Thanks for your time investigating it!!