Search in sources :

Example 11 with MockSlingHttpServletRequest

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());
}
Also used : MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) Test(org.junit.Test)

Example 12 with MockSlingHttpServletRequest

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));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) Test(org.junit.Test)

Example 13 with MockSlingHttpServletRequest

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);
    }
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) RenderUnit(org.apache.sling.scripting.sightly.java.compiler.RenderUnit) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) ScriptEngine(javax.script.ScriptEngine) StringWriter(java.io.StringWriter) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SimpleBindings(javax.script.SimpleBindings) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) HashMap(java.util.HashMap) Map(java.util.Map) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 14 with MockSlingHttpServletRequest

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());
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) JsonObject(javax.json.JsonObject) StringWriter(java.io.StringWriter) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) StringReader(java.io.StringReader) Servlet(javax.servlet.Servlet) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Test(org.junit.Test)

Example 15 with MockSlingHttpServletRequest

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;
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)

Aggregations

MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)39 MockSlingHttpServletResponse (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse)29 Test (org.junit.Test)29 ContentPolicy (com.day.cq.wcm.api.policies.ContentPolicy)17 ContentPolicyMapping (com.day.cq.wcm.api.policies.ContentPolicyMapping)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 BufferedImage (java.awt.image.BufferedImage)11 SlingBindings (org.apache.sling.api.scripting.SlingBindings)8 Resource (org.apache.sling.api.resource.Resource)7 InputStream (java.io.InputStream)4 Page (com.day.cq.wcm.api.Page)3 StringWriter (java.io.StringWriter)3 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)3 TurboFilter (ch.qos.logback.classic.turbo.TurboFilter)2 Style (com.day.cq.wcm.api.designer.Style)2 StringReader (java.io.StringReader)2 HashMap (java.util.HashMap)2 JsonObject (javax.json.JsonObject)2 FilterChain (javax.servlet.FilterChain)2 Servlet (javax.servlet.Servlet)2