From 763a424b69158ca70433b499d6fd95e5a9e51fee Mon Sep 17 00:00:00 2001 From: "R. Tyler Croy" Date: Mon, 19 Dec 2016 07:47:19 -0800 Subject: [PATCH] Add integration tests to ensure that text/html requests get web pages And of course, application/json requests get JSON objects --- .../httpwizard/StandardResponse.groovy | 7 +- .../lasagna/httpwizard/resources/Get.groovy | 28 ++++--- .../httpwizard/resources/GetSpec.groovy | 75 +++++++++++++++++-- src/test/resources/test-config.yml | 4 + 4 files changed, 95 insertions(+), 19 deletions(-) create mode 100644 src/test/resources/test-config.yml diff --git a/src/main/groovy/io/lasagna/httpwizard/StandardResponse.groovy b/src/main/groovy/io/lasagna/httpwizard/StandardResponse.groovy index d5db800..3811086 100644 --- a/src/main/groovy/io/lasagna/httpwizard/StandardResponse.groovy +++ b/src/main/groovy/io/lasagna/httpwizard/StandardResponse.groovy @@ -33,9 +33,14 @@ class StandardResponse { @JsonProperty String uri + @JsonProperty + String description - static StandardResponse fromRequest(UriInfo info, HttpHeaders headers) { + static StandardResponse fromRequest(String description, + UriInfo info, + HttpHeaders headers) { StandardResponse r = new StandardResponse() + r.description = description r.uri = info.absolutePath r.headers = headers.requestHeaders return r diff --git a/src/main/groovy/io/lasagna/httpwizard/resources/Get.groovy b/src/main/groovy/io/lasagna/httpwizard/resources/Get.groovy index f8023d7..23e861b 100644 --- a/src/main/groovy/io/lasagna/httpwizard/resources/Get.groovy +++ b/src/main/groovy/io/lasagna/httpwizard/resources/Get.groovy @@ -29,8 +29,8 @@ import groovy.transform.TypeChecked import javax.ws.rs.core.Context import javax.ws.rs.core.HttpHeaders import javax.ws.rs.core.MediaType -//import javax.ws.rs.core.Response import javax.ws.rs.core.UriInfo +import javax.ws.rs.Consumes import javax.ws.rs.DefaultValue import javax.ws.rs.GET import javax.ws.rs.QueryParam @@ -42,20 +42,14 @@ import io.dropwizard.views.View @TypeChecked class GetResource { - @GET - @Path('200') - @Metered - @Produces(MediaType.APPLICATION_JSON) - StandardResponse returns200(@Context UriInfo ui, - @Context HttpHeaders headers) { - return StandardResponse.fromRequest(ui, headers) - } + final String DESCRIPTION_200 = 'Standard response for successful HTTP requests.' @GET @Path('200') @Metered - @Produces(MediaType.TEXT_HTML) - View returns200(@DefaultValue('true') @QueryParam('pretty') boolean pretty, + @Produces([MediaType.TEXT_HTML, MediaType.APPLICATION_XHTML_XML]) + @Consumes([MediaType.TEXT_PLAIN, MediaType.TEXT_HTML]) + View returns200WithHtml(@DefaultValue('true') @QueryParam('pretty') boolean pretty, @Context UriInfo ui, @Context HttpHeaders headers) { return new View('/views/standard-response.mustache', Charsets.UTF_8) { @@ -64,8 +58,18 @@ class GetResource { String getJson() { ObjectMapper mapper = new ObjectMapper() mapper.enable(SerializationFeature.INDENT_OUTPUT) - return mapper.writeValueAsString(StandardResponse.fromRequest(ui, headers)) + return mapper.writeValueAsString(StandardResponse.fromRequest(DESCRIPTION_200, ui, headers)) } } } + + @GET + @Path('200') + @Metered + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + StandardResponse returns200(@Context UriInfo ui, + @Context HttpHeaders headers) { + return StandardResponse.fromRequest(DESCRIPTION_200, ui, headers) + } } diff --git a/src/test/groovy/io/lasagna/httpwizard/resources/GetSpec.groovy b/src/test/groovy/io/lasagna/httpwizard/resources/GetSpec.groovy index b4de65d..77209cc 100644 --- a/src/test/groovy/io/lasagna/httpwizard/resources/GetSpec.groovy +++ b/src/test/groovy/io/lasagna/httpwizard/resources/GetSpec.groovy @@ -18,8 +18,18 @@ package io.lasagna.httpwizard.resources + +import io.lasagna.httpwizard.HttpWizard +import io.lasagna.httpwizard.HttpWizardConfiguration + +import groovy.json.JsonSlurper import io.dropwizard.testing.junit.ResourceTestRule +import io.dropwizard.testing.junit.DropwizardAppRule +import io.dropwizard.testing.ResourceHelpers import javax.ws.rs.client.Client +import org.glassfish.jersey.client.JerseyClientBuilder +import javax.ws.rs.core.Response +import javax.ws.rs.core.MediaType import org.junit.Rule import spock.lang.Specification @@ -28,15 +38,68 @@ class GetSpec extends Specification { ResourceTestRule dropwizard = ResourceTestRule.builder() .addResource(new GetResource()).build(); - def "simple GET returns 200"() { - given: - Client client = dropwizard.client() - def response + Client client + def setup() { + client = dropwizard.client() + } + + def "GET [application/json] returns 200"() { when: - response = dropwizard.client().target('/get/200').request().get() + def jerseyResponse = dropwizard.client().target('/get/200').request(MediaType.APPLICATION_JSON).get() + def data = jsonResponseToMap(jerseyResponse) then: - response.status == 200 + jerseyResponse.status == 200 + data instanceof Map + data.uri == '/get/200' + } + + private Map jsonResponseToMap(Object response) { + return new JsonSlurper().parseText( + response.readEntity(String.class) + ) + } +} + +class GetViewSpec extends Specification { + @Rule + final DropwizardAppRule app = new DropwizardAppRule<>(HttpWizard.class, ResourceHelpers.resourceFilePath('test-config.yml')) + + Client client + + def setup() { + client = JerseyClientBuilder.createClient() + } + + def "GET [text/html] returns 200 and HTML"() { + when: + Response jerseyResponse = client.target(getFullPathOf('/get/200')) + .request(MediaType.TEXT_HTML) + .get() + + then: + jerseyResponse.status == 200 + isHtml(jerseyResponse) + } + + def "GET [] returns 200 and HTML"() { + when: + Response jerseyResponse = client.target(getFullPathOf('/get/200')) + .request() + .get() + + then: + jerseyResponse.status == 200 + isHtml jerseyResponse + } + + private String getFullPathOf(String path) { + return String.format('http://localhost:%d%s', app.localPort, path) + } + + private boolean isHtml(Response response) { + String body = response.readEntity(String.class) + return body.findAll(/<\/html>/)?.size > 0 } } diff --git a/src/test/resources/test-config.yml b/src/test/resources/test-config.yml new file mode 100644 index 0000000..95d7904 --- /dev/null +++ b/src/test/resources/test-config.yml @@ -0,0 +1,4 @@ +--- +appVersion: '1.0-test' +logging: + level: WARN