Skip to content

Commit

Permalink
[RESTEASY-1569] changed calculation to get proper path substring. Add…
Browse files Browse the repository at this point in the history
…ed tests for regex capturing groups
  • Loading branch information
rsearls authored and asoldano committed Mar 20, 2017
1 parent d121c26 commit 56633f0
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 1 deletion.
Expand Up @@ -101,7 +101,23 @@ public ResourceInvoker match(HttpRequest request, int start)
}
else
{
String substring = path.substring(0, length);
// must find the end of the matched pattern
// and get the substring from 1st char thru end
// of matched chars
Pattern p = expression.getPattern();
Matcher m = p.matcher(path);
m.region(start, path.length());
String substring = path;
while(m.find()) {
String endText = m.group(m.groupCount());
if (endText != null && !endText.isEmpty()) {
int indx = path.indexOf(endText, length);
if (indx > -1) {
substring = path.substring(0, indx);
}
}
}

uriInfo.pushMatchedPath(substring);
uriInfo.pushMatchedURI(substring);
}
Expand Down
@@ -0,0 +1,118 @@
package org.jboss.resteasy.test.resource.path;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.resteasy.test.resource.path.resource.ResourceLocatorRegexCapturingGroup;
import org.jboss.resteasy.test.resource.path.resource.ResourceLocatorRegexCapturingGroupSubResourceNoPath;
import org.jboss.resteasy.test.resource.path.resource.ResourceLocatorRegexCapturingGroupSubResourceWithPath;
import org.jboss.resteasy.utils.PortProviderUtil;
import org.jboss.resteasy.utils.TestUtil;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

/**
* @Path annotation paths can consist of Regex Capturing groups used with
* Resource Locator scenarios.
*
* User: rsearls
* Date: 2/17/17
*/
@RunWith(Arquillian.class)
@RunAsClient
public class ResourceLocatorRegexCapturingGroupTest {
private static final String ERROR_MSG = "Response contain wrong content";
static Client client;

@BeforeClass
public static void setup() throws Exception {
client = ClientBuilder.newClient();
}

@Deployment
public static Archive<?> deploy() {
WebArchive war = TestUtil.prepareArchive(ResourceLocatorRegexCapturingGroupTest.class.getSimpleName());
war.addClasses(ResourceLocatorRegexCapturingGroupSubResourceNoPath.class,
ResourceLocatorRegexCapturingGroupSubResourceWithPath.class);
war.addAsWebInfResource(ResourceLocatorRegexCapturingGroupTest.class.getPackage(), "web.xml", "web.xml");
return TestUtil.finishContainerPrepare(war, null, ResourceLocatorRegexCapturingGroup.class);
}

private String generateURL(String path) {
return PortProviderUtil.generateURL(path, ResourceLocatorRegexCapturingGroupTest.class.getSimpleName());
}

@AfterClass
public static void close() throws Exception {
client.close();
}

@AfterClass
public static void after() throws Exception {

}

/**
* @tpTestDetails Test for root resource and for subresource.
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testBasic() throws Exception {
{
Response response = client.target(generateURL("/capture/basic")).request().get();
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
Assert.assertEquals(ERROR_MSG, "basic success", response.readEntity(String.class));
response.close();
}
{
Response response = client.target(generateURL("/capture/BASIC/test")).request().get();
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
Assert.assertEquals(ERROR_MSG, "BASIC test", response.readEntity(String.class));
response.close();
}
}

@Test
public void testBird() throws Exception {
{
Response response = client.target(generateURL("/capture/nobird")).request().get();
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
Assert.assertEquals(ERROR_MSG, "nobird success", response.readEntity(String.class));
response.close();
}

{
Response response = client.target(generateURL("/capture/BIRD/test")).request().get();
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
Assert.assertEquals(ERROR_MSG, "BIRD test", response.readEntity(String.class));
response.close();
}
}

@Test
public void testFly() throws Exception {
{
Response response = client.target(generateURL("/capture/a/nofly/b")).request().get();
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
Assert.assertEquals(ERROR_MSG, "a/nofly/b success", response.readEntity(String.class));
response.close();
}

{
Response response = client.target(generateURL("/capture/a/FLY/b/test")).request().get();
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
Assert.assertEquals(ERROR_MSG, "a/FLY/b test", response.readEntity(String.class));
response.close();
}
}
}
@@ -0,0 +1,97 @@
package org.jboss.resteasy.test.resource.path;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.resteasy.test.asynch.AsyncPostProcessingTest;
import org.jboss.resteasy.test.resource.path.resource.*;
import org.jboss.resteasy.utils.PortProviderUtil;
import org.jboss.resteasy.utils.TestUtil;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

/**
* @Path annotation paths can consist of Regex Non-Capturing groups used with
* Resource Locator scenarios.
*
* User: rsearls
* Date: 2/18/17
*/
@RunWith(Arquillian.class)
@RunAsClient
public class ResourceLocatorRegexNonCapturingGroupTest {
private static final String ERROR_MSG = "Response contain wrong content";
static Client client;

@BeforeClass
public static void setup() throws Exception {
client = ClientBuilder.newClient();
}

@Deployment
public static Archive<?> deploy() {
WebArchive war = TestUtil.prepareArchive(ResourceLocatorRegexNonCapturingGroupTest.class.getSimpleName());
war.addClasses(ResourceLocatorRegexCapturingGroupSubResourceNoPath.class,
ResourceLocatorRegexCapturingGroupSubResourceWithPath.class);
war.addAsWebInfResource(ResourceLocatorRegexNonCapturingGroupTest.class.getPackage(), "web.xml", "web.xml");
return TestUtil.finishContainerPrepare(war, null, ResourceLocatorRegexNonCapturingGroup.class);
}

private String generateURL(String path) {
return PortProviderUtil.generateURL(path, ResourceLocatorRegexNonCapturingGroupTest.class.getSimpleName());
}

@AfterClass
public static void close() throws Exception {
client.close();
}

@AfterClass
public static void after() throws Exception {

}

@Test
public void testBird() throws Exception {
{
Response response = client.target(generateURL("/noCapture/nobird")).request().get();
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
Assert.assertEquals(ERROR_MSG, "nobird success", response.readEntity(String.class));
response.close();
}

{
Response response = client.target(generateURL("/noCapture/BIRD/test")).request().get();
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
Assert.assertEquals(ERROR_MSG, "BIRD test", response.readEntity(String.class));
response.close();
}
}

@Test
public void testFly() throws Exception {
{
Response response = client.target(generateURL("/noCapture/a/nofly/b")).request().get();
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
Assert.assertEquals(ERROR_MSG, "a/nofly/b success", response.readEntity(String.class));
response.close();
}

{
Response response = client.target(generateURL("/noCapture/a/FLY/b/test")).request().get();
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
Assert.assertEquals(ERROR_MSG, "a/FLY/b test", response.readEntity(String.class));
response.close();
}
}
}
@@ -0,0 +1,42 @@
package org.jboss.resteasy.test.resource.path.resource;

import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

/**
* User: rsearls
* Date: 2/17/17
*/
@Path("/capture")
public class ResourceLocatorRegexCapturingGroup {

@Path("basic")
public ResourceLocatorRegexCapturingGroupSubResourceNoPath basic() {
return new ResourceLocatorRegexCapturingGroupSubResourceNoPath ("basic");
}

@Path("BASIC")
public ResourceLocatorRegexCapturingGroupSubResourceWithPath basicTwo() {
return new ResourceLocatorRegexCapturingGroupSubResourceWithPath("BASIC");
}

@Path("{name: (nobird|NOBIRD)}")
public ResourceLocatorRegexCapturingGroupSubResourceNoPath nobird(@PathParam("name") String name) {
return new ResourceLocatorRegexCapturingGroupSubResourceNoPath(name);
}

@Path("{name: (bird|BIRD)}")
public ResourceLocatorRegexCapturingGroupSubResourceWithPath bird(@PathParam("name") String name) {
return new ResourceLocatorRegexCapturingGroupSubResourceWithPath(name);
}

@Path("{name: a/(fly|FLY)/b}")
public ResourceLocatorRegexCapturingGroupSubResourceWithPath fly(@PathParam("name") String name) {
return new ResourceLocatorRegexCapturingGroupSubResourceWithPath(name);
}

@Path("{name: a/(nofly|NOFLY)/b}")
public ResourceLocatorRegexCapturingGroupSubResourceNoPath nofly(@PathParam("name") String name) {
return new ResourceLocatorRegexCapturingGroupSubResourceNoPath(name);
}
}
@@ -0,0 +1,24 @@
package org.jboss.resteasy.test.resource.path.resource;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

/**
* User: rsearls
* Date: 2/17/17
*/
@Produces("text/plain")
public class ResourceLocatorRegexCapturingGroupSubResourceNoPath {
private String name;

public ResourceLocatorRegexCapturingGroupSubResourceNoPath(String name) {
this.name = name;
}

@GET
public Response get() {
return Response.ok(name +" success").build();
}
}

@@ -0,0 +1,25 @@
package org.jboss.resteasy.test.resource.path.resource;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

/**
* User: rsearls
* Date: 2/17/17
*/
@Produces("text/plain")
public class ResourceLocatorRegexCapturingGroupSubResourceWithPath {
private String name;

public ResourceLocatorRegexCapturingGroupSubResourceWithPath(String name) {
this.name = name;
}

@GET
@Path("/test")
public Response get() {
return Response.ok(name + " test").build();
}
}
@@ -0,0 +1,33 @@
package org.jboss.resteasy.test.resource.path.resource;

import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

/**
* User: rsearls
* Date: 2/17/17
*/
@Path("/noCapture")
public class ResourceLocatorRegexNonCapturingGroup {

@Path("{name: (?:nobird|NOBIRD)}")
public ResourceLocatorRegexCapturingGroupSubResourceNoPath nobird(@PathParam("name") String name) {
return new ResourceLocatorRegexCapturingGroupSubResourceNoPath(name);
}

@Path("{name: (?:bird|BIRD)}")
public ResourceLocatorRegexCapturingGroupSubResourceWithPath bird(@PathParam("name") String name) {
return new ResourceLocatorRegexCapturingGroupSubResourceWithPath(name);
}

@Path("{name: a/(?:fly|FLY)/b}")
public ResourceLocatorRegexCapturingGroupSubResourceWithPath fly(@PathParam("name") String name) {
return new ResourceLocatorRegexCapturingGroupSubResourceWithPath(name);
}

@Path("{name: a/(?:nofly|NOFLY)/b}")
public ResourceLocatorRegexCapturingGroupSubResourceNoPath nofly(@PathParam("name") String name) {
return new ResourceLocatorRegexCapturingGroupSubResourceNoPath(name);
}

}
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>

<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>

0 comments on commit 56633f0

Please sign in to comment.