EmbedUrlProcessorServlet extends SlingSafeMethodsServlet { @Override protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws IOException { String url = request.getParameter(PARAM_URL); UrlProcessor.Result result = getResult(url); if (result != null) { writeJson(result, response); } else { response.sendError(HttpServletResponse.SC_NOT_FOUND); } }    }
@Test public void testWithoutUrlParameter() throws Exception { servlet.doGet(context.request(), context.response()); assertEquals(HttpServletResponse.SC_NOT_FOUND, context.response().getStatus(), "Expected the 404 status code."); assertEquals("", context.response().getOutputAsString(), "Expected empty response output."); }
@Test public void testUrlWithoutRegisteredProvider() throws Exception { context.request().setQueryString("url=http: servlet.doGet(context.request(), context.response()); assertEquals(HttpServletResponse.SC_NOT_FOUND, context.response().getStatus(), "Expected the 404 status code."); assertEquals("", context.response().getOutputAsString(), "Expected empty response output."); }
@Test public void testUrlWithRegisteredProvider() throws Exception { context.request().setQueryString("url=https: servlet.doGet(context.request(), context.response()); String expectedOutput = "{\"processor\":\"pinterest\",\"options\":{\"pinId\":\"99360735500167749\"}}"; assertEquals(HttpServletResponse.SC_OK, context.response().getStatus(), "Expected the 200 status code."); assertEquals("application/json;charset=utf-8", context.response().getContentType(), "Expected the JSON content type."); assertEquals(expectedOutput, context.response().getOutputAsString(), "Does not match the expected response output."); }