use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest in project sling by apache.
the class MockSlingScriptHelperTest method setUp.
@Before
public void setUp() throws Exception {
this.bundleContext = MockOsgi.newBundleContext();
this.resourceResolver = MockSling.newResourceResolver(bundleContext);
this.request = new MockSlingHttpServletRequest(this.resourceResolver, bundleContext);
this.response = new MockSlingHttpServletResponse();
this.scriptHelper = MockSling.newSlingScriptHelper(this.request, this.response, this.bundleContext);
}
use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest in project sling by apache.
the class RatingPostServletTest method successfulSave.
// @Test
public void successfulSave() throws Exception {
Map<String, Object> params = new HashMap<String, Object>();
params.put(RatingsUtil.PROPERTY_RATING, "5");
context.registerService(RatingsService.class, Mockito.mock(RatingsService.class));
RatingPostServlet servlet = context.registerInjectActivateService(new RatingPostServlet());
MockSlingHttpServletRequest request = context.request();
request.setRemoteUser("admin");
request.setParameterMap(params);
request.setResource(context.create().resource(SlingshotConstants.APP_ROOT_PATH + "/content/admin/travel"));
MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
servlet.doPost(request, response);
assertThat(response.getStatus(), Matchers.equalTo(SC_OK));
String output = response.getOutputAsString();
assertThat(output, equalTo("{ \"rating\" : 0}"));
}
use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest in project sling by apache.
the class LogTracerTest method recordingWithTracing.
@Test
public void recordingWithTracing() 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");
request.setHeader(LogTracer.HEADER_TRACER_CONFIG, "a.b.c;level=trace,a.b;level=debug");
HttpServletResponse response = mock(HttpServletResponse.class);
FilterChain chain = new FilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
assertNotNull(context.getService(TurboFilter.class));
getLogContext().addTurboFilter(context.getService(TurboFilter.class));
getLogger("a.b").info("a.b-info");
}
};
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());
assertEquals(1, json.getJsonArray("logs").size());
}
use of org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest in project sling by apache.
the class TracerLogServletTest method recordingWhenRequested.
@Test
public void recordingWhenRequested() throws Exception {
TracerLogServlet logServlet = new TracerLogServlet(context.bundleContext());
request = new MockSlingHttpServletRequest(context.bundleContext());
Recording recording = logServlet.startRecording(request, response);
assertNotNull(recording);
//Once recording is created then it should be returned
Recording recording2 = logServlet.getRecordingForRequest(request);
assertSame(recording, recording2);
//Repeated call should return same recording instance
Recording recording3 = logServlet.startRecording(request, response);
assertSame(recording, recording3);
logServlet.resetCache();
//If recording gets lost then NOOP must be returned
Recording recording4 = logServlet.getRecordingForRequest(request);
assertSame(Recording.NOOP, recording4);
}
Aggregations