-
Notifications
You must be signed in to change notification settings - Fork 888
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow cookies to be added to ResteasyHttpHeaders.
- Loading branch information
Showing
6 changed files
with
159 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
69 changes: 69 additions & 0 deletions
69
...ion-tests/src/test/java/org/jboss/resteasy/test/core/interceptors/FilteredCookieTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.jboss.resteasy.test.core.interceptors; | ||
|
||
import javax.ws.rs.client.Client; | ||
import javax.ws.rs.client.ClientBuilder; | ||
import javax.ws.rs.client.Invocation.Builder; | ||
import javax.ws.rs.client.WebTarget; | ||
import javax.ws.rs.core.Cookie; | ||
import javax.ws.rs.core.NewCookie; | ||
import javax.ws.rs.core.Response; | ||
|
||
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.core.interceptors.resource.FilteredCookieContainerRequestFilter; | ||
import org.jboss.resteasy.test.core.interceptors.resource.FilteredCookieResource; | ||
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.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
/** | ||
* @tpSubChapter Cookies and filters | ||
* @tpChapter Integration tests | ||
* @tpTestCaseDetails Regression test for RESTEASY-1266 | ||
* @tpSince RESTEasy 3.1.0.Final | ||
*/ | ||
@RunWith(Arquillian.class) | ||
@RunAsClient | ||
public class FilteredCookieTest { | ||
|
||
private static final String OLD_COOKIE_NAME = "old-cookie"; | ||
private static final String NEW_COOKIE_NAME = "new-cookie"; | ||
|
||
@Deployment | ||
public static Archive<?> deploySimpleResource() { | ||
WebArchive war = TestUtil.prepareArchive(FilteredCookieTest.class.getSimpleName()); | ||
return TestUtil.finishContainerPrepare(war, null, FilteredCookieResource.class, FilteredCookieContainerRequestFilter.class); | ||
} | ||
|
||
private String generateURL(String path) { | ||
return PortProviderUtil.generateURL(path, FilteredCookieTest.class.getSimpleName()); | ||
} | ||
|
||
@Test | ||
public void testServerHeaders() { | ||
|
||
Client client = ClientBuilder.newClient(); | ||
WebTarget target = client.target(generateURL("/test/get")); | ||
Response response = target.request().get(); | ||
NewCookie cookie = response.getCookies().get(OLD_COOKIE_NAME); | ||
Assert.assertEquals(200, response.getStatus()); | ||
Assert.assertNotNull(cookie); | ||
client.close(); | ||
|
||
client = ClientBuilder.newClient(); | ||
target = client.target(generateURL("/test/return")); | ||
Builder builder = target.request(); | ||
response = builder.cookie(cookie).get(); | ||
NewCookie oldCookie = response.getCookies().get(OLD_COOKIE_NAME); | ||
NewCookie newCookie = response.getCookies().get(NEW_COOKIE_NAME); | ||
Assert.assertEquals(200, response.getStatus()); | ||
Assert.assertNotNull(oldCookie); | ||
Assert.assertNotNull(newCookie); | ||
client.close(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
.../jboss/resteasy/test/core/interceptors/resource/FilteredCookieContainerRequestFilter.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.jboss.resteasy.test.core.interceptors.resource; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ContainerRequestFilter; | ||
import javax.ws.rs.container.PreMatching; | ||
import javax.ws.rs.core.Cookie; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
@Provider | ||
@PreMatching | ||
public class FilteredCookieContainerRequestFilter implements ContainerRequestFilter { | ||
|
||
private static final String OLD_COOKIE_NAME = "old-cookie"; | ||
private static final String NEW_COOKIE_NAME = "new-cookie"; | ||
|
||
@Override | ||
public void filter(ContainerRequestContext requestContext) throws IOException { | ||
|
||
final Cookie cookie = requestContext.getCookies().get(OLD_COOKIE_NAME); | ||
if (cookie != null) { | ||
requestContext.getHeaders().add(HttpHeaders.COOKIE, new Cookie(NEW_COOKIE_NAME, cookie.getValue()).toString()); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
.../test/java/org/jboss/resteasy/test/core/interceptors/resource/FilteredCookieResource.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.jboss.resteasy.test.core.interceptors.resource; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.Cookie; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.NewCookie; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.core.Response.ResponseBuilder; | ||
|
||
@Path("test") | ||
public class FilteredCookieResource { | ||
private static final String OLD_COOKIE_NAME = "old-cookie"; | ||
private static final String NEW_COOKIE_NAME = "new-cookie"; | ||
private @Context HttpHeaders headers; | ||
|
||
@GET | ||
@Path("get") | ||
public Response getCookie() { | ||
NewCookie cookie = new NewCookie(OLD_COOKIE_NAME, "value"); | ||
return Response.ok().cookie(cookie).build(); | ||
} | ||
|
||
@GET | ||
@Path("return") | ||
public Response returnCookie() { | ||
Cookie oldCookie = headers.getCookies().get(OLD_COOKIE_NAME); | ||
Cookie newCookie = headers.getCookies().get(NEW_COOKIE_NAME); | ||
ResponseBuilder builder = Response.ok(); | ||
builder.cookie(new NewCookie(oldCookie.getName(), oldCookie.getValue())); | ||
builder.cookie(new NewCookie(newCookie.getName(), newCookie.getValue())); | ||
return builder.build(); | ||
} | ||
} |