Skip to content

Instantly share code, notes, and snippets.

@takaki
Last active July 7, 2016 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takaki/9e643578f9723a166866d3835469c617 to your computer and use it in GitHub Desktop.
Save takaki/9e643578f9723a166866d3835469c617 to your computer and use it in GitHub Desktop.
JAX-RSでコンテキストの取得 ref: http://qiita.com/takaki@github/items/7e5abaca0d96261cb3a9
package example.jaxrs;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriInfo;
@Path("/")
public class ContextResource {
@GET
@Path("uriinfo")
public String uriinfo(@Context UriInfo uriInfo) {
return uriInfo.getPath() + ";" + uriInfo.getQueryParameters().getFirst("foo");
}
@GET
@Path("headers")
public String headers(@Context HttpHeaders headers) {
return headers.getHeaderString("X-Qiita-Demo");
}
}
package example.jaxrs
import org.glassfish.jersey.server.ResourceConfig
import org.glassfish.jersey.test.JerseyTest
import spock.lang.Shared
import spock.lang.Specification
import javax.ws.rs.core.Application
class ContextResourceTest extends Specification {
@Shared
def jerseyTest = new JerseyTest() {
@Override
protected Application configure() {
return new ResourceConfig(ContextResource.class)
}
}
def setupSpec() {
jerseyTest.setUp()
}
def cleanupSpec() {
jerseyTest.tearDown()
}
def "test uriinfo"() {
when:
def response = jerseyTest.target("/uriinfo").queryParam("foo", "bar").request().get()
then:
response.readEntity(String.class) == "uriinfo;bar"
}
def "test headers"() {
when:
def response = jerseyTest.target("/headers").request().header("X-Qiita-Demo", "Foo Bar").get()
then:
response.readEntity(String.class) == "Foo Bar"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment