Codearte / accurest

Accurest - Consumer Driven Contracts verifier for Java. Moved to:
https://github.com/spring-cloud/spring-cloud-contract
Apache License 2.0
99 stars 23 forks source link

Response DSL does not seem to support plain text on server side #263

Closed making closed 8 years ago

making commented 8 years ago

I'd like to test plain text response but it does not support regex, optional, execute and so on.

For example,

io.codearte.accurest.dsl.GroovyDsl.make {
    request {
        method 'GET'
        urlPath '/'
    }
    response {
        status 200
        body(value(
                client('123'),
                server(regex('[0-9]+'))
        ))
        headers {
            header('Content-Type': 'text/plain;charset=ISO-8859-1')
        }
    }
}

generates

    @Test
    public void validate_shouldSayHello() throws Exception {
        // given:
            MockMvcRequestSpecification request = given();

        // when:
            ResponseOptions response = given().spec(request)
                    .get("/");

        // then:
            assertThat(response.statusCode()).isEqualTo(200);
            assertThat(response.header("Content-Type")).isEqualTo("text/plain;charset=ISO-8859-1");
        // and:
            Object responseBody = (response.getBody().asString());
            assertThat(responseBody).isEqualTo("[0-9]+"); // .matches(...) is expected 😩 
    }

Of course, JSON response works well

io.codearte.accurest.dsl.GroovyDsl.make {
    request {
        method 'GET'
        urlPath '/'
    }
    response {
        status 200
        body(id : value(
                client('123'),
                server(regex('[0-9]+'))
        ))
        headers {
            header('Content-Type': 'application/json;charset=UTF-8')
        }
    }
}

generates

    @Test
    public void validate_shouldSayHello() throws Exception {
        // given:
            MockMvcRequestSpecification request = given();

        // when:
            ResponseOptions response = given().spec(request)
                    .get("/");

        // then:
            assertThat(response.statusCode()).isEqualTo(200);
            assertThat(response.header("Content-Type")).isEqualTo("application/json;charset=UTF-8");
        // and:
            DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
            assertThatJson(parsedJson).field("id").matches("[0-9]+"); // 😀
    }
marcingrzejszczak commented 8 years ago

This looks like a regression :/

marcingrzejszczak commented 8 years ago

Ah the regex etc might have never worked actually. I think we focused on jsons with those

making commented 8 years ago

I'm using 1.1.0-M4

making commented 8 years ago

Ah the regex etc might have never worked actually. I think we focused on jsons with those

Oh.. Actually, this is not critical. I just wanted to create a small demo project using accurest. https://github.com/making/jsug-spring-cloud/tree/accurest

I'll use a fixed value to test plain text response so far. https://github.com/making/jsug-spring-cloud/blob/1f0d5f766fb558051fc30ca08333dda1a4017f4b/jsug-producer/src/test/java/com/example/MvcTest.java#L14-L15

marcingrzejszczak commented 8 years ago

Or just use json as a response

marcingrzejszczak commented 8 years ago

For now we don't support fully this feature - https://github.com/Codearte/accurest/issues/269

marcingrzejszczak commented 8 years ago

This issue was moved to spring-cloud/spring-cloud-contract#15