use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.
the class WrongStepContentInterceptor_Test method testServiceRequestNoStepError.
@Test
public void testServiceRequestNoStepError() {
MyApp app = new MyApp();
app.setLocked(true);
MockResponse response = sendContentRequest(app.appContent, 1, 1);
Assert.assertEquals("Should have returned content", MyContent.CONTENT, new String(response.getOutput()));
}
use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.
the class WrongStepContentInterceptor_Test method sendContentRequest.
/**
* Utility method to send a mock request to the application.
*
* @param target the target content.
* @param clientStep the client-side step count
* @param serverStep the server-side step count
* @return the response.
*/
private MockResponse sendContentRequest(final WComponent target, final int clientStep, final int serverStep) {
UIContext uic = createUIContext();
uic.setUI(WebUtilities.getTop(target));
WServlet.WServletEnvironment env = new WServlet.WServletEnvironment("/app", "http://localhost", "");
env.setStep(serverStep);
uic.setEnvironment(env);
setActiveContext(uic);
MockRequest request = new MockRequest();
request.setParameter(Environment.TARGET_ID, target.getId());
request.setParameter(Environment.STEP_VARIABLE, String.valueOf(clientStep));
MockResponse response = new MockResponse();
InterceptorComponent interceptor = new WrongStepContentInterceptor();
interceptor.attachUI(uic.getUI());
interceptor.attachResponse(response);
// Handle the request. This will either return the content or a step error
try {
interceptor.serviceRequest(request);
interceptor.preparePaint(request);
interceptor.paint(new WebXmlRenderContext(response.getWriter()));
} catch (ContentEscape escape) {
try {
// Content has been returned
escape.setRequest(request);
escape.setResponse(response);
escape.escape();
} catch (IOException e) {
Assert.fail("Failed to write content");
}
} catch (ErrorCodeEscape escape) {
try {
escape.setRequest(request);
escape.setResponse(response);
escape.escape();
} catch (IOException e) {
Assert.fail("Failed to write error content");
}
} catch (ActionEscape ignored) {
// don't care
}
// Step error
return response;
}
use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.
the class ContentEscape_Test method testEscapeNoContent.
@Test
public void testEscapeNoContent() throws IOException {
// ContentEscape with no content
MockResponse response = new MockResponse();
ContentEscape contentEscape = new ContentEscape(null);
contentEscape.setResponse(response);
contentEscape.escape();
Assert.assertEquals("Content length should be zero", 0, response.getOutput().length);
Assert.assertNull("Mime type should be null", response.getContentType());
Assert.assertTrue("Headers should be empty", response.getHeaders().isEmpty());
}
use of com.github.bordertech.wcomponents.util.mock.MockResponse in project wcomponents by BorderTech.
the class WAudio_Test method testHandleRequest.
@Test
public void testHandleRequest() throws IOException {
MockAudio clip1 = new MockAudio();
clip1.setBytes("WAudio_Test.testHandleRequest.one".getBytes(CHAR_ENCODING));
MockAudio clip2 = new MockAudio();
clip2.setBytes("WAudio_Test.testHandleRequest.two".getBytes(CHAR_ENCODING));
WAudio audio = new WAudio(new Audio[] { clip1, clip2 });
MockRequest request = new MockRequest();
setActiveContext(createUIContext());
// Should not do anything when target is not present
audio.handleRequest(request);
try {
request.setParameter(Environment.TARGET_ID, audio.getTargetId());
request.setParameter("WAudio.index", "0");
audio.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(clip1.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
audio.setCacheKey("key");
// Should produce the content with cache flag set
try {
request.setParameter("WAudio.index", "1");
audio.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(clip2.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 WImage_Test method testHandleRequest.
@Test
public void testHandleRequest() throws IOException {
byte[] data = "WImage_Test.testHandleRequest".getBytes(CHAR_ENCODING);
MockRequest request = new MockRequest();
MockImage content = new MockImage();
content.setBytes(data);
WImage image = new WImage();
image.setLocked(true);
setActiveContext(createUIContext());
image.setImage(content);
// Should not do anything when target is not present
image.handleRequest(request);
try {
request.setParameter(Environment.TARGET_ID, image.getTargetId());
image.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(data, 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
image.setCacheKey("key");
// Should produce the content with cache flag set
try {
image.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(data, 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"));
}
}
Aggregations