use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class AdaptiveImageServletTest method testWrongNumberOfSelectors.
@Test
public void testWrongNumberOfSelectors() throws Exception {
Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE0_PATH, "img.1.1", "png");
MockSlingHttpServletRequest request = requestResponsePair.getLeft();
MockSlingHttpServletResponse response = requestResponsePair.getRight();
servlet.doGet(request, response);
assertEquals("Expected a 404 response when the request has more selectors than expected.", HttpServletResponse.SC_NOT_FOUND, response.getStatus());
assertArrayEquals("Expected an empty response output.", new byte[0], response.getOutput());
}
use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class AdaptiveImageServletTest method testGIFFileDirectStream.
@Test
public void testGIFFileDirectStream() throws Exception {
Pair<MockSlingHttpServletRequest, MockSlingHttpServletResponse> requestResponsePair = prepareRequestResponsePair(IMAGE5_PATH, "img", "gif");
MockSlingHttpServletRequest request = requestResponsePair.getLeft();
MockSlingHttpServletResponse response = requestResponsePair.getRight();
servlet.doGet(request, response);
ByteArrayInputStream stream = new ByteArrayInputStream(response.getOutput());
InputStream directStream = this.getClass().getClassLoader().getResourceAsStream("image/Adobe_Systems_logo_and_wordmark.svg.gif");
assertTrue(IOUtils.contentEquals(stream, directStream));
}
use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest in project sling by apache.
the class SightlyCompiledScriptTest method testEvalSlingBindings.
/**
* Tests that SlingBindings are correctly handled by compiled scripts, by setting them from the script context to the request
* attributes.
* @throws ScriptException
*/
@Test
public void testEvalSlingBindings() throws ScriptException {
ScriptEngine scriptEngine = mock(ScriptEngine.class);
final RenderUnit renderUnit = mock(RenderUnit.class);
Whitebox.setInternalState(renderUnit, "subTemplates", new HashMap<String, Object>());
final BundleContext bundleContext = MockOsgi.newBundleContext();
bundleContext.registerService(ExtensionRegistryService.class.getName(), mock(ExtensionRegistryService.class), new Hashtable<String, Object>());
ResourceResolver resourceResolver = MockSling.newResourceResolver(bundleContext);
final MockSlingHttpServletRequest request = spy(new MockSlingHttpServletRequest(resourceResolver, bundleContext));
SightlyCompiledScript compiledScript = spy(new SightlyCompiledScript(scriptEngine, renderUnit));
ScriptContext scriptContext = mock(ScriptContext.class);
StringWriter writer = new StringWriter();
when(scriptContext.getWriter()).thenReturn(writer);
Bindings scriptContextBindings = new SimpleBindings() {
{
put("test", "testValue");
put(SlingBindings.REQUEST, request);
put(SlingBindings.SLING, MockSling.newSlingScriptHelper(bundleContext));
}
};
SlingBindings oldBindings = new SlingBindings();
oldBindings.put("old", "oldValue");
request.setAttribute(SlingBindings.class.getName(), oldBindings);
when(scriptContext.getBindings(ScriptContext.ENGINE_SCOPE)).thenReturn(scriptContextBindings);
compiledScript.eval(scriptContext);
ArgumentCaptor<SlingBindings> slingBindingsArgumentCaptor = ArgumentCaptor.forClass(SlingBindings.class);
ArgumentCaptor<String> attributeNameArgumentCaptor = ArgumentCaptor.forClass(String.class);
// request.setAttribute should have been invoked 3 times: once here, twice in the compiled script
verify(request, times(3)).setAttribute(attributeNameArgumentCaptor.capture(), slingBindingsArgumentCaptor.capture());
List<SlingBindings> slingBindingsValues = slingBindingsArgumentCaptor.getAllValues();
int invocation = 1;
for (SlingBindings bindings : slingBindingsValues) {
switch(invocation) {
case 1:
assertEquals(oldBindings, bindings);
break;
case 2:
assertEquals(3, bindings.size());
for (Map.Entry<String, Object> entry : scriptContextBindings.entrySet()) {
assertEquals(entry.getValue(), bindings.get(entry.getKey()));
}
break;
case 3:
assertEquals(oldBindings, bindings);
}
invocation++;
}
for (String key : attributeNameArgumentCaptor.getAllValues()) {
assertEquals(SlingBindings.class.getName(), key);
}
}
use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest in project sling by apache.
the class LogTracerTest method recordingWithoutTracing.
@Test
public void recordingWithoutTracing() throws Exception {
activateTracerAndServlet();
MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.bundleContext()) {
@Override
public RequestProgressTracker getRequestProgressTracker() {
return createTracker("x", "y");
}
@Override
public String getRequestURI() {
return "foo";
}
};
request.setHeader(TracerLogServlet.HEADER_TRACER_RECORDING, "true");
HttpServletResponse response = mock(HttpServletResponse.class);
FilterChain chain = new FilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
//No TurboFilter should be registered if tracing is not requested
assertNull(context.getService(TurboFilter.class));
}
};
prepareChain(chain).doFilter(request, response);
String requestId = getRequestId(response);
assertNotNull(requestId);
Recording r = ((TracerLogServlet) context.getService(Servlet.class)).getRecording(requestId);
assertTrue(r instanceof JSONRecording);
JSONRecording jr = (JSONRecording) r;
StringWriter sw = new StringWriter();
jr.render(sw);
JsonObject json = Json.createReader(new StringReader(sw.toString())).readObject();
assertEquals(2, json.getJsonArray("requestProgressLogs").size());
}
use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest in project sling by apache.
the class SlingContextImpl method request.
/**
* @return Sling request
*/
public final MockSlingHttpServletRequest request() {
if (this.request == null) {
this.request = new MockSlingHttpServletRequest(this.resourceResolver(), this.bundleContext());
// initialize sling bindings
SlingBindings bindings = new SlingBindings();
bindings.put(SlingBindings.REQUEST, this.request);
bindings.put(SlingBindings.RESPONSE, response());
bindings.put(SlingBindings.SLING, slingScriptHelper());
this.request.setAttribute(SlingBindings.class.getName(), bindings);
}
return this.request;
}
Aggregations