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:
parent
7a1b414d76
commit
763a424b69
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
appVersion: '1.0-test'
|
||||
logging:
|
||||
level: WARN
|
Loading…
Reference in New Issue