use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.
the class WebXmlRenderingPerformance_Test method sendRequest.
/**
* Invokes WComponent request processing, so that this test case can more closely match a production scenario.
*
* @param comp the component to invoke request processing on.
* @param uic the user context to use.
*/
private void sendRequest(final WComponent comp, final UIContext uic) {
PrintWriter writer = new PrintWriter(new NullWriter());
uic.setEnvironment(new WServlet.WServletEnvironment("", "http://localhost", ""));
uic.setUI(comp);
InterceptorComponent root = ServletUtil.createInterceptorChain(new MockHttpServletRequest());
root.attachUI(comp);
Response response = new MockResponse();
root.attachResponse(response);
setActiveContext(uic);
MockRequest request = new MockRequest();
try {
root.serviceRequest(request);
root.preparePaint(request);
root.paint(new WebXmlRenderContext(writer));
} finally {
resetContext();
}
}
use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.
the class WVideo_Test method testHandleTrackRequest.
@Test
public void testHandleTrackRequest() throws IOException {
MockTrack track1 = new MockTrack();
track1.setBytes("WVideo_Test.testHandleTrackRequest.one".getBytes(CHAR_ENCODING));
MockTrack track2 = new MockTrack();
track2.setBytes("WVideo_Test.testHandleTrackRequest.two".getBytes(CHAR_ENCODING));
WVideo video = new WVideo(new MockVideo());
video.setTracks(new Track[] { track1, track2 });
setActiveContext(createUIContext());
MockRequest request = new MockRequest();
// Should not do anything when target is not present
video.handleRequest(request);
try {
request.setParameter(Environment.TARGET_ID, video.getTargetId());
request.setParameter("WVideo.trackIndex", "0");
video.handleRequest(request);
Assert.fail("Should have thrown a content escape");
} catch (ContentEscape escape) {
MockResponse response = new MockResponse();
escape.setResponse(response);
escape.escape();
String output = new String(response.getOutput(), CHAR_ENCODING);
Assert.assertEquals("Incorrect content returned", new String(track1.getBytes(), CHAR_ENCODING), output);
Assert.assertFalse("Cache flag should not be set", escape.isCacheable());
Assert.assertEquals("Response should have header set for no caching", ConfigurationProperties.RESPONSE_DEFAULT_NO_CACHE_SETTINGS, response.getHeaders().get("Cache-Control"));
}
// Test Cached Response
video.setCacheKey("key");
// Should produce the content with cache flag set
try {
request.setParameter("WVideo.trackIndex", "1");
video.handleRequest(request);
Assert.fail("Should have thrown a content escape");
} catch (ContentEscape escape) {
MockResponse response = new MockResponse();
escape.setResponse(response);
escape.escape();
String output = new String(response.getOutput(), CHAR_ENCODING);
Assert.assertEquals("Incorrect content returned", new String(track2.getBytes(), CHAR_ENCODING), output);
Assert.assertTrue("Cache flag should be set", escape.isCacheable());
Assert.assertEquals("Response should have header set for caching", ConfigurationProperties.RESPONSE_DEFAULT_CACHE_SETTINGS, response.getHeaders().get("Cache-Control"));
}
}
use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.
the class AjaxDebugStructureInterceptor_Test method doAjaxRequest.
/**
* Does an AJAX request for the app.
*
* @param app the MyApp instance to do an AJAX request for.
* @return the AJAX xml output.
*/
private String doAjaxRequest(final MyApp app) {
UIContext uic = createUIContext();
uic.setUI(app);
setActiveContext(uic);
// Create interceptors
AjaxSetupInterceptor ajaxSetupInterceptor = new AjaxSetupInterceptor();
AjaxPageShellInterceptor ajaxPageInterceptor = new AjaxPageShellInterceptor();
AjaxInterceptor ajaxInterceptor = new AjaxInterceptor();
AjaxDebugStructureInterceptor debugInterceptor = new AjaxDebugStructureInterceptor();
// Create chain
debugInterceptor.setBackingComponent(ajaxInterceptor);
ajaxPageInterceptor.setBackingComponent(debugInterceptor);
ajaxSetupInterceptor.setBackingComponent(ajaxPageInterceptor);
ajaxSetupInterceptor.attachUI(app);
// Action phase
MockRequest request = new MockRequest();
AjaxHelper.registerComponent(app.target.getId(), app.trigger.getId());
request.setParameter(WServlet.AJAX_TRIGGER_PARAM_NAME, app.trigger.getId());
ajaxSetupInterceptor.serviceRequest(request);
ajaxSetupInterceptor.preparePaint(request);
// Render phase
MockResponse response = new MockResponse();
ajaxSetupInterceptor.attachResponse(response);
ajaxSetupInterceptor.paint(new WebXmlRenderContext(response.getWriter()));
return response.getWriterOutput();
}
use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.
the class DataListInterceptor_Test method testInterecptorWithNullOption.
@Test
public void testInterecptorWithNullOption() throws XpathException, SAXException, IOException {
String tableKey = TestLookupTable.CACHEABLE_DAY_OF_WEEK_TABLE_WITH_NULL_OPTION;
// Create interceptor
DataListInterceptor interceptor = new DataListInterceptor();
interceptor.attachUI(new DefaultWComponent());
// Action phase
MockRequest request = new MockRequest();
request.setParameter(WServlet.DATA_LIST_PARAM_NAME, tableKey);
interceptor.serviceRequest(request);
// Render phase
MockResponse response = new MockResponse();
interceptor.attachResponse(response);
interceptor.paint(new WebXmlRenderContext(new PrintWriter(response.getWriter())));
String xml = response.getWriterOutput();
// Ensure that the data matches the test table.
List<TestLookupTable.TableEntry> table = (List<TestLookupTable.TableEntry>) Factory.newInstance(LookupTable.class).getTable(tableKey);
assertXpathEvaluatesTo(String.valueOf(table.size()), "count(/ui:datalist/ui:option)", xml);
for (int i = 0; i < table.size(); i++) {
if (table.get(i) == null) {
assertXpathEvaluatesTo("", "/ui:datalist/ui:option[" + (i + 1) + "]/@value", xml);
assertXpathEvaluatesTo("", "/ui:datalist/ui:option[" + (i + 1) + "]/text()", xml);
assertXpathEvaluatesTo("true", "/ui:datalist/ui:option[" + (i + 1) + "]/@isNull", xml);
} else {
assertXpathEvaluatesTo(table.get(i).getCode(), "/ui:datalist/ui:option[" + (i + 1) + "]/@value", xml);
assertXpathEvaluatesTo(table.get(i).getDesc(), "/ui:datalist/ui:option[" + (i + 1) + "]/text()", xml);
assertXpathNotExists("/ui:datalist/ui:option[" + (i + 1) + "]/@isNull", xml);
}
}
}
use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.
the class ResponseCacheInterceptor_Test method testOverridePageNoCache.
@Test
public void testOverridePageNoCache() {
/**
* Original config.
*/
Configuration originalConfig;
originalConfig = Config.getInstance();
String override = "OVERRIDE CONTENT NO CACHE";
try {
// Test override cache settings
Configuration config = Config.copyConfiguration(originalConfig);
config.setProperty(ConfigurationProperties.RESPONSE_NO_CACHE_SETTINGS, override);
Config.setConfiguration(config);
// Create interceptor
ResponseCacheInterceptor interceptor = new ResponseCacheInterceptor(CacheType.CONTENT_NO_CACHE);
interceptor.setBackingComponent(new WText());
// Mock Response
MockResponse response = new MockResponse();
interceptor.attachResponse(response);
// Render phase
interceptor.paint(new WebXmlRenderContext(response.getWriter()));
// Check Override
Assert.assertEquals("Cache-Control header not overriden correctly for CONTENT NO CACHE", override, response.getHeaders().get("Cache-Control"));
} finally {
// Remove overrides
Config.setConfiguration(originalConfig);
}
}
Aggregations