Last active
July 7, 2016 10:29
-
-
Save takaki/9e643578f9723a166866d3835469c617 to your computer and use it in GitHub Desktop.
JAX-RSでコンテキストの取得 ref: http://qiita.com/takaki@github/items/7e5abaca0d96261cb3a9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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