Add integration tests to ensure that text/html requests get web pages

And of course, application/json requests get JSON objects
This commit is contained in:
R. Tyler Croy 2016-12-19 07:47:19 -08:00
parent 7a1b414d76
commit 763a424b69
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
4 changed files with 95 additions and 19 deletions

View File

@ -33,9 +33,14 @@ class StandardResponse {
@JsonProperty @JsonProperty
String uri 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() StandardResponse r = new StandardResponse()
r.description = description
r.uri = info.absolutePath r.uri = info.absolutePath
r.headers = headers.requestHeaders r.headers = headers.requestHeaders
return r return r

View File

@ -29,8 +29,8 @@ import groovy.transform.TypeChecked
import javax.ws.rs.core.Context import javax.ws.rs.core.Context
import javax.ws.rs.core.HttpHeaders import javax.ws.rs.core.HttpHeaders
import javax.ws.rs.core.MediaType import javax.ws.rs.core.MediaType
//import javax.ws.rs.core.Response
import javax.ws.rs.core.UriInfo import javax.ws.rs.core.UriInfo
import javax.ws.rs.Consumes
import javax.ws.rs.DefaultValue import javax.ws.rs.DefaultValue
import javax.ws.rs.GET import javax.ws.rs.GET
import javax.ws.rs.QueryParam import javax.ws.rs.QueryParam
@ -42,20 +42,14 @@ import io.dropwizard.views.View
@TypeChecked @TypeChecked
class GetResource { class GetResource {
@GET final String DESCRIPTION_200 = 'Standard response for successful HTTP requests.'
@Path('200')
@Metered
@Produces(MediaType.APPLICATION_JSON)
StandardResponse returns200(@Context UriInfo ui,
@Context HttpHeaders headers) {
return StandardResponse.fromRequest(ui, headers)
}
@GET @GET
@Path('200') @Path('200')
@Metered @Metered
@Produces(MediaType.TEXT_HTML) @Produces([MediaType.TEXT_HTML, MediaType.APPLICATION_XHTML_XML])
View returns200(@DefaultValue('true') @QueryParam('pretty') boolean pretty, @Consumes([MediaType.TEXT_PLAIN, MediaType.TEXT_HTML])
View returns200WithHtml(@DefaultValue('true') @QueryParam('pretty') boolean pretty,
@Context UriInfo ui, @Context UriInfo ui,
@Context HttpHeaders headers) { @Context HttpHeaders headers) {
return new View('/views/standard-response.mustache', Charsets.UTF_8) { return new View('/views/standard-response.mustache', Charsets.UTF_8) {
@ -64,8 +58,18 @@ class GetResource {
String getJson() { String getJson() {
ObjectMapper mapper = new ObjectMapper() ObjectMapper mapper = new ObjectMapper()
mapper.enable(SerializationFeature.INDENT_OUTPUT) 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)
}
} }

View File

@ -18,8 +18,18 @@
package io.lasagna.httpwizard.resources 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.ResourceTestRule
import io.dropwizard.testing.junit.DropwizardAppRule
import io.dropwizard.testing.ResourceHelpers
import javax.ws.rs.client.Client 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 org.junit.Rule
import spock.lang.Specification import spock.lang.Specification
@ -28,15 +38,68 @@ class GetSpec extends Specification {
ResourceTestRule dropwizard = ResourceTestRule.builder() ResourceTestRule dropwizard = ResourceTestRule.builder()
.addResource(new GetResource()).build(); .addResource(new GetResource()).build();
def "simple GET returns 200"() { Client client
given:
Client client = dropwizard.client()
def response
def setup() {
client = dropwizard.client()
}
def "GET [application/json] returns 200"() {
when: 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: 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<HttpWizardConfiguration> 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
} }
} }

View File

@ -0,0 +1,4 @@
---
appVersion: '1.0-test'
logging:
level: WARN