Skip to content

Instantly share code, notes, and snippets.

@psamsotha
Created April 18, 2016 15:07
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 psamsotha/8737363cf85a797bf789ddccdadc42b6 to your computer and use it in GitHub Desktop.
Save psamsotha/8737363cf85a797bf789ddccdadc42b6 to your computer and use it in GitHub Desktop.
Test for spaces in path with Jersey. Stack Overflow http://stackoverflow.com/q/36693635/2587435
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.filter.LoggingFilter;
import com.sun.jersey.api.core.DefaultResourceConfig;
import com.sun.jersey.spi.container.servlet.WebComponent;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.WebAppDescriptor;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import org.junit.Test;
import static junit.framework.Assert.assertEquals;
/**
* Stack Overflow question http://stackoverflow.com/q/36693635/2587435
*
* Run this like any other JUnit test. Only requires one test dependency to run
*
* <dependency>
* <groupId>com.sun.jersey.jersey-test-framework</groupId>
* <artifactId>jersey-test-framework-grizzly2</artifactId>
* <version>1.19</version>
* <scope>test</scope>
* </dependency>
*
* @author Paul Samsotha
*/
public class SpaceInPathTest extends JerseyTest {
@Path("service")
public static class Service {
@GET
@Path("test/{param1}/{param2}")
public String getTest(@PathParam("param1") String param1,
@PathParam("param2") String param2) {
return param1 + ":" + param2;
}
}
public static class AppConfig extends DefaultResourceConfig {
public AppConfig() {
super(Service.class);
}
}
@Override
public WebAppDescriptor configure() {
return new WebAppDescriptor.Builder()
.initParam(WebComponent.RESOURCE_CONFIG_CLASS,
AppConfig.class.getName())
.build();
}
@Test
public void testSpaceInPath() {
client().addFilter(new LoggingFilter());
ClientResponse response = resource()
.path("service/test/hello world/hello again")
.get(ClientResponse.class);
assertEquals(200, response.getStatus());
assertEquals("hello world:hello again", response.getEntity(String.class));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment