Skip to content

Commit

Permalink
[RESTEASY-1808] Tests for client redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
marekkopecky authored and asoldano committed Feb 16, 2018
1 parent a276040 commit 5fce22d
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 23 deletions.
Expand Up @@ -8,9 +8,11 @@
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.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine;
import org.jboss.resteasy.test.client.resource.TestResource;
import org.jboss.resteasy.test.client.resource.RedirectProxyResource;
import org.jboss.resteasy.test.client.resource.RedirectResource;
import org.jboss.resteasy.utils.PortProviderUtil;
import org.jboss.resteasy.utils.TestUtil;
import org.jboss.shrinkwrap.api.Archive;
Expand All @@ -23,6 +25,7 @@
* @tpSubChapter Resteasy-client
* @tpChapter Integration tests
* @tpSince RESTEasy 3.5
* @tpTestCaseDetails https://issues.jboss.org/browse/RESTEASY-1075
*/
@RunWith(Arquillian.class)
@RunAsClient
Expand All @@ -33,9 +36,19 @@ public static Archive<?> deploy()
{
WebArchive war = TestUtil.prepareArchive(RedirectTest.class.getSimpleName());
war.addClasses(PortProviderUtil.class);
return TestUtil.finishContainerPrepare(war, null, TestResource.class);
return TestUtil.finishContainerPrepare(war, null, RedirectResource.class);
}

/**
* @tpTestDetails Set client to following the redirect
* Do not use RESTEasy proxy
* Use GET HTTP request
* Send request to "end-point 1", that returns 307 HTTP code (Temporary Redirect) and redirect client to another URL (handled by "end-point 2")
* Client should follow redirect URL
* Client should return data from "end-point 2"
* Both end-points return Response data type
* @tpSince RESTEasy 3.5
*/
@Test
public void testRedirect()
{
Expand All @@ -56,6 +69,16 @@ public void testRedirect()
}
}

/**
* @tpTestDetails Set client to following the redirect
* Do not use RESTEasy proxy
* Use POST HTTP request
* Send request to "end-point 1", that returns 307 HTTP code (Temporary Redirect) and redirect client to another URL (handled by "end-point 2")
* Client should follow redirect URL
* Client should return data from "end-point 2"
* Both end-points return Response data type
* @tpSince RESTEasy 3.5
*/
@Test
public void testPostRedirect()
{
Expand All @@ -76,6 +99,139 @@ public void testPostRedirect()
}
}

/**
* @tpTestDetails Set client to following the redirect
* Use RESTEasy proxy
* Use GET HTTP request
* Send request to "end-point 1", that returns 307 HTTP code (Temporary Redirect) and redirect client to another URL (handled by "end-point 2")
* Client should follow redirect URL
* Client should return data from "end-point 2"
* Both end-points return Response data type
* @tpSince RESTEasy 3.5
*/
@Test
public void testRedirectProxy()
{
ApacheHttpClient43Engine engine = new ApacheHttpClient43Engine();
engine.setFollowRedirects(true);
ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
try
{
RedirectProxyResource proxy = client.target(generateURL("/"))
.proxy(RedirectProxyResource.class);
Response response = proxy.redirect(RedirectTest.class.getSimpleName());
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("OK", response.readEntity(String.class));
response.close();
}
finally
{
client.close();
}
}

/**
* @tpTestDetails Set client to following the redirect
* Use RESTEasy proxy
* Use GET HTTP request
* Send request to "end-point 1", that returns 307 HTTP code (Temporary Redirect) and redirect client to another URL (handled by "end-point 2")
* Client should follow redirect URL
* Client should return data from "end-point 2"
* "end-point 1" returns Response data type, "end-point 2" returns String data type
* @tpSince RESTEasy 3.5
*/
@Test
public void testRedirectDirectResponseProxy()
{
ApacheHttpClient43Engine engine = new ApacheHttpClient43Engine();
engine.setFollowRedirects(true);
ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
try
{
RedirectProxyResource proxy = client.target(generateURL("/"))
.proxy(RedirectProxyResource.class);
Response response = proxy.redirectDirectResponse(RedirectTest.class.getSimpleName());
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("ok - direct response", response.readEntity(String.class));
response.close();
}
finally
{
client.close();
}
}

/**
* @tpTestDetails Set client to following the redirect
* Use RESTEasy proxy
* Use GET HTTP request
* Send request to "end-point 1", that returns 301 HTTP code (Moved Permanently) and redirect client to another URL (handled by "end-point 2")
* Client should follow redirect URL
* Client should return data from "end-point 2"
* "end-point 1" returns Response data type, "end-point 2" returns String data type
* @tpSince RESTEasy 3.5
*/
@Test
public void testMovedPermanentlyDirectResponseProxy()
{
ApacheHttpClient43Engine engine = new ApacheHttpClient43Engine();
engine.setFollowRedirects(true);
ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
try
{
RedirectProxyResource proxy = client.target(generateURL("/"))
.proxy(RedirectProxyResource.class);
Response response = proxy.movedPermanently(RedirectTest.class.getSimpleName());
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("ok - direct response", response.readEntity(String.class));
response.close();
}
finally
{
client.close();
}
}

/**
* @tpTestDetails Set client to following the redirect
* Use RESTEasy proxy
* Use GET HTTP request
* Send request to "end-point 1", that returns 302 HTTP code (Found) and redirect client to another URL (handled by "end-point 2")
* Client should follow redirect URL
* Client should return data from "end-point 2"
* "end-point 1" returns Response data type, "end-point 2" returns String data type
* @tpSince RESTEasy 3.5
*/
@Test
public void testFoundDirectResponseProxy()
{
ApacheHttpClient43Engine engine = new ApacheHttpClient43Engine();
engine.setFollowRedirects(true);
ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
try
{
RedirectProxyResource proxy = client.target(generateURL("/"))
.proxy(RedirectProxyResource.class);
Response response = proxy.found(RedirectTest.class.getSimpleName());
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("ok - direct response", response.readEntity(String.class));
response.close();
}
finally
{
client.close();
}
}

/**
* @tpTestDetails Do not use RESTEasy proxy
* Use GET HTTP request
* Do not set client to following the redirect
* Send request to "end-point 1", that returns 307 HTTP code (Temporary Redirect) and redirect client to another URL (handled by "end-point 2")
* Client should not follow redirect URL
* Client should return 307 HTTP code
* @tpSince RESTEasy 3.5
*/
@Test
public void testNoRedirect()
{
Expand All @@ -93,6 +249,15 @@ public void testNoRedirect()
}
}

/**
* @tpTestDetails Do not use RESTEasy proxy
* Use POST HTTP request
* Do not set client to following the redirect
* Send request to "end-point 1", that returns 307 HTTP code (Temporary Redirect) and redirect client to another URL (handled by "end-point 2")
* Client should not follow redirect URL
* Client should return 307 HTTP code
* @tpSince RESTEasy 3.5
*/
@Test
public void testNoPostRedirect()
{
Expand All @@ -109,4 +274,30 @@ public void testNoPostRedirect()
client.close();
}
}

/**
* @tpTestDetails Use RESTEasy proxy
* Do not set client to following the redirect
* Send request to "end-point 1", that returns 307 HTTP code (Temporary Redirect) and redirect client to another URL (handled by "end-point 2")
* Client should not follow redirect URL
* Client should return 307 HTTP code
* @tpSince RESTEasy 3.5
*/
@Test
public void testNoRedirectProxy()
{
ResteasyClient client = new ResteasyClientBuilder().build();
try
{
RedirectProxyResource proxy = client.target(generateURL("/"))
.proxy(RedirectProxyResource.class);
Response response = proxy.redirect(RedirectTest.class.getSimpleName());
Assert.assertEquals(307, response.getStatus());
response.close();
}
finally
{
client.close();
}
}
}
@@ -0,0 +1,37 @@
package org.jboss.resteasy.test.client.resource;

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

@Path("/")
public interface RedirectProxyResource
{

@Path("redirect/{p}")
@GET
Response redirect(@PathParam("p") String p);

@Path("redirected")
@GET
Response redirected();


@Path("redirectDirectResponse/{p}")
@GET
Response redirectDirectResponse(@PathParam("p") String p);

@Path("redirectedDirectResponse")
@GET
String redirectedDirectResponse();

@Path("movedPermanently/{p}")
@GET
Response movedPermanently(@PathParam("p") String p);


@Path("found/{p}")
@GET
Response found(@PathParam("p") String p);
}
@@ -0,0 +1,63 @@
package org.jboss.resteasy.test.client.resource;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.utils.PortProviderUtil;

@Path("/")
public class RedirectResource
{
@Path("redirect/{p}")
@GET
public Response redirect(@PathParam("p") String p)
{
return Response.temporaryRedirect(PortProviderUtil.createURI("/redirected", p)).build();
}

@Path("redirected")
@GET
public Response redirected()
{
return Response.ok("OK").build();
}

@Path("post-redirect")
@POST
public Response postRedirect(String p)
{
return Response.seeOther(PortProviderUtil.createURI("/redirected", p)).build();
}

@Path("redirectDirectResponse/{p}")
@GET
public Response redirectDirectResponse(@PathParam("p") String p)
{
return Response.temporaryRedirect(PortProviderUtil.createURI("/redirectedDirectResponse", p)).build();
}

@Path("redirectedDirectResponse")
@GET
public String redirectedDirectResponse()
{
return "ok - direct response";
}


@Path("movedPermanently/{p}")
@GET
public Response movedPermanently(@PathParam("p") String p)
{
return Response.status(301).header("location", PortProviderUtil.createURI("/redirectedDirectResponse", p)).build();
}

@Path("found/{p}")
@GET
public Response found(@PathParam("p") String p)
{
return Response.status(302).header("location", PortProviderUtil.createURI("/redirectedDirectResponse", p)).build();
}
}
Expand Up @@ -99,25 +99,4 @@ public Response methodEntity(String s)
{
return Response.ok(s).build();
}

@Path("redirect/{p}")
@GET
public Response redirect(@PathParam("p") String p)
{
return Response.temporaryRedirect(PortProviderUtil.createURI("/redirected", p)).build();
}

@Path("redirected")
@GET
public Response redirected()
{
return Response.ok("OK").build();
}

@Path("post-redirect")
@POST
public Response postRedirect(String p)
{
return Response.seeOther(PortProviderUtil.createURI("/redirected", p)).build();
}
}

0 comments on commit 5fce22d

Please sign in to comment.