rest-assured / rest-assured

Java DSL for easy testing of REST services
Apache License 2.0
6.81k stars 1.88k forks source link

Response builder rewrites Parsers defined in initial ResponseSpec #1506

Open smatytsin opened 2 years ago

smatytsin commented 2 years ago

I have a case when I need to set parser Parser.JSON for response content type = "text/plain" I have registered parsers different ways:

  1. Using RestAssured.registerParser("text/plain", Parser.JSON)
  2. Using given().response().parser("text/plain", Parser.JSON) but every times I tried to validate body, I got exception java.lang.IllegalStateException: Expected response body to be verified as JSON, HTML or XML but content-type 'text/plain' is not supported out of the box. Try registering a custom parser using: RestAssured.registerParser("text/plain", );

I have investigate the reason and found that ResponseBuilder rewrites original spec parsers with new default one in this line of code: https://github.com/rest-assured/rest-assured/blob/4eeb3d82b1d4672787b57ca45167a63085c8c1f1/rest-assured/src/main/java/io/restassured/builder/ResponseBuilder.java#L225

This case is actual only if set config RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); or if response has filter of class ResponseLoggingFilter.class

rkraneis commented 2 years ago

Same problem here: as soon as we use enableLoggingOfRequestAndResponseIfValidationFails in our tests, registerParser is ignored and tests fail :( This is for RestAssured version 4.4.0.