spring-cloud-contract
spring-cloud-contract copied to clipboard
Additional escape character added to the escaped header values results in test failure
Additional escape characters (\
) were added to the header in generated tests
Looks similar to: https://github.com/spring-cloud/spring-cloud-contract/issues/851
Spring cloud contract plugin version: 2.2.3.RELEASE
Contract :
import static org.springframework.cloud.contract.spec.Contract.make
[
make {
request {
name("test")
method 'GET'
urlPath('/test')
}
response {
status OK()
body( "test": "\"escaped\"")
headers {
contentType('application/pdf')
header(contentDisposition(), "attachment; filename=\"test.pdf\"")
}
}
}
]
Generated test:
@Test
public void validate_test() throws Exception {
// given:
RequestSpecification request = given();
// when:
Response response = given().spec(request)
.get("/test");
// then:
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.header("Content-Type")).matches("application/pdf.*");
assertThat(response.header("Content-Disposition")).isEqualTo("attachment; filename=\\\"test.pdf\\\"");
// and:
String responseBody = response.getBody().asString();
assertThat(responseBody).isEqualTo("{test=\"escaped\"}");
}
Note that the body is compiled correctly but the header Content-Disposition
value has an additional \\
resulting in a test failure.
The compiled header should have been assertThat(response.header("Content-Disposition")).isEqualTo("attachment; filename=\"test.pdf\"");
Can you check this against the latest version of Spring Cloud Contract - you're using version 2.x which is not supported anymore.
Can you check this against the latest version of Spring Cloud Contract - you're using version 2.x which is not supported anymore.
Tested with version 3.1.2
& Spring cloud 2021.0.2
, it didn't work.
Here is a small app I've created to test this. Hope it helps.
Same problem with 3.1.4
and Spring cloud 2021.0.4
It seems OK in 3.1.5
-> see:
header(contentDisposition(), 'attachment; filename="test.pdf"')
is generated as
response.header("Content-Disposition") == '''attachment; filename="test.pdf"'''
Note: maybe the trick is in using '
-> there's no need to evaluate the content 😏
It seems OK in
3.1.5
-> see:header(contentDisposition(), 'attachment; filename="test.pdf"')
is generated asresponse.header("Content-Disposition") == '''attachment; filename="test.pdf"'''
Note: maybe the trick is in using
'
-> there's no need to evaluate the content 😏
I tried with the repo provided above, but it still doesn't work
it's quite funny. I'm using it in another project and it ins't working now. However, the non-working tests is JUnit, but the old working is Spock (<testFramework>SPOCK</testFramework>
). I'll try to play with it more.
I found out a work around with regexp working for me:
header(contentDisposition(), regex('attachment; filename="oracle.sql"'))