Skip to content

Instantly share code, notes, and snippets.

@jyeary
Last active August 7, 2017 02:32
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 jyeary/41341578177fca802a280c24fa57f88c to your computer and use it in GitHub Desktop.
Save jyeary/41341578177fca802a280c24fa57f88c to your computer and use it in GitHub Desktop.
package com.bluelotussoftware.example.filter;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.drone.api.annotation.Drone;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.warp.Activity;
import org.jboss.arquillian.warp.Inspection;
import org.jboss.arquillian.warp.Warp;
import org.jboss.arquillian.warp.WarpTest;
import org.jboss.arquillian.warp.servlet.AfterServlet;
import org.jboss.arquillian.warp.servlet.BeforeServlet;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;
/**
* <p>
* Checks the {@link HeaderFilter} initialization parameter set the enabled to
* confirm that headers are added to the {@link HttpServletRequest}, and
* {@link HttpServletResponse}.</p>
* <p>
* This test class deployment represents the simplest test form for deployment
* of a {@literal Servlet 3.0} based Filter that takes advantage of
* annotations.</p>
*
* @author John Yeary
* @version 1.0
* @see HeaderFilterRegistrationIntegrationTest
*/
@RunWith(value = Arquillian.class)
@WarpTest
@RunAsClient
public class HeaderFilterIntegrationTest {
@ArquillianResource
private URL contextPath;
@Drone
private WebDriver webdriver;
/**
* Default constructor.
*/
public HeaderFilterIntegrationTest() {
}
/**
* Creates a {@link WebArchive} for auto-deployment in Arquillian.
*
* @return an Web ARchive (war) file representing the filter to be tested.
*/
@Deployment
public static WebArchive createDeployment() {
WebArchive war = ShrinkWrap.create(WebArchive.class)
.addClass(HeaderFilter.class);
System.out.println(war.toString(true));
return war;
}
/**
* Executes the doFilter() method of the filter and checks the expected
* results.
*/
@Test
public void testDoFilter() {
Warp.initiate(new Activity() {
@Override
public void perform() {
System.out.println("TEST URL: " + contextPath.toString());
webdriver.navigate().to(contextPath);
}
}).inspect(new Inspection() {
private static final long serialVersionUID = 1489207995240215531L;
@ArquillianResource
HttpServletRequest request;
@ArquillianResource
HttpServletResponse response;
@BeforeServlet
public void before() {
List attributeNames = Collections.list(request.getAttributeNames());
System.out.println("Attributes: " + Arrays.toString(attributeNames.toArray()));
String attribute = (String) request.getAttribute("X-ATTRIBUTE");
assertEquals("Experimental Attribute", attribute);
}
@AfterServlet
public void after() {
System.out.println("Headers: " + Arrays.toString(response.getHeaderNames().toArray()));
assertEquals("Experimental Header", response.getHeader("X-HEADER"));
}
});
}
}
package com.bluelotussoftware.example.filter;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.drone.api.annotation.Drone;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.warp.Activity;
import org.jboss.arquillian.warp.Inspection;
import org.jboss.arquillian.warp.Warp;
import org.jboss.arquillian.warp.WarpTest;
import org.jboss.arquillian.warp.servlet.AfterServlet;
import org.jboss.arquillian.warp.servlet.BeforeServlet;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
import org.jboss.shrinkwrap.descriptor.api.webapp30.WebAppDescriptor;
import org.jboss.shrinkwrap.descriptor.api.webcommon30.WebAppVersionType;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;
/**
* Checks the {@link HeaderFilter} initialization parameter set the disabled to
* confirm that headers are not added to the {@link HttpServletRequest}, or
* {@link HttpServletResponse}.
*
* @author John Yeary
* @version 1.0
* @see HeaderFilterDisabledIntegrationTest
*/
@RunWith(Arquillian.class)
@WarpTest
@RunAsClient
public class HeaderFilterRegistrationIntegrationDisabledTest {
public static final String URL_PATTERN = "/*";
public static final String PARAM_NAME = "enabled";
public static final String PARAM_VALUE = "false";
@ArquillianResource
private URL contextPath;
@Drone
private WebDriver webdriver;
/**
* Default constructor.
*/
public HeaderFilterRegistrationIntegrationDisabledTest() {
}
/**
* Creates a {@link WebArchive} for auto-deployment in Arquillian.
*
* @return an Web ARchive (war) file representing the filter to be tested.
*/
@Deployment
public static WebArchive createDeployment() {
WebArchive war = ShrinkWrap.create(WebArchive.class)
.addClass(HeaderFilter.class)
.setWebXML(generateWebXMLAsset());
System.out.println(war.toString(true));
return war;
}
/**
* This builder constructs a web.xml descriptor for a filter and converts it
* to a {@link StringAsset}.
*
* @return the web.xml represented as an {@link Asset}.
*/
private static Asset generateWebXMLAsset() {
WebAppDescriptor wad = Descriptors.create(WebAppDescriptor.class);
wad.version(WebAppVersionType._3_0)
.createFilter()
.filterName(HeaderFilter.class.getSimpleName())
.filterClass(HeaderFilter.class.getName())
.createInitParam()
.paramName(PARAM_NAME)
.paramValue(PARAM_VALUE)
.up()
.up()
.createFilterMapping()
.filterName(HeaderFilter.class.getSimpleName())
.urlPattern(URL_PATTERN)
.up();
System.out.println(wad.exportAsString());
return new StringAsset(wad.exportAsString());
}
/**
* Executes the doFilter() method of the filter and checks the expected
* results.
*/
@Test
public void testDoFilter() {
Warp.initiate(new Activity() {
@Override
public void perform() {
System.out.println("TEST URL: " + contextPath.toString());
webdriver.navigate().to(contextPath);
}
}).inspect(new Inspection() {
private static final long serialVersionUID = 1489207995240215531L;
@ArquillianResource
HttpServletRequest request;
@ArquillianResource
HttpServletResponse response;
@BeforeServlet
public void before() {
List attributeNames = Collections.list(request.getAttributeNames());
System.out.println("Attributes: " + Arrays.toString(attributeNames.toArray()));
assertNull(request.getAttribute("X-ATTRIBUTE"));
}
@AfterServlet
public void after() {
System.out.println("Headers: " + Arrays.toString(response.getHeaderNames().toArray()));
assertNull(response.getHeader("X-HEADER"));
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment