spring-cloud-contract icon indicating copy to clipboard operation
spring-cloud-contract copied to clipboard

Additional escape character added to the escaped header values results in test failure

Open hegdevishwa opened this issue 2 years ago • 7 comments

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\"");

hegdevishwa avatar May 26 '22 14:05 hegdevishwa

Can you check this against the latest version of Spring Cloud Contract - you're using version 2.x which is not supported anymore.

marcingrzejszczak avatar May 27 '22 07:05 marcingrzejszczak

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.

hegdevishwa avatar May 27 '22 14:05 hegdevishwa

Same problem with 3.1.4 and Spring cloud 2021.0.4

Ligio avatar Nov 28 '22 15:11 Ligio

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 😏

arnosthavelka avatar Dec 23 '22 13:12 arnosthavelka

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 😏

I tried with the repo provided above, but it still doesn't work

hegdevishwa avatar Jan 25 '23 10:01 hegdevishwa

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.

arnosthavelka avatar Feb 21 '23 13:02 arnosthavelka

I found out a work around with regexp working for me:

header(contentDisposition(), regex('attachment; filename="oracle.sql"'))

arnosthavelka avatar Feb 21 '23 14:02 arnosthavelka