EndpointsServlet extends HttpServlet { @Override public void service(HttpServletRequest request, HttpServletResponse response) throws IOException { String method = getRequestMethod(request); if ("OPTIONS".equals(method)) { corsHandler.handle(request, response); } else { String path = Strings.stripSlash( request.getRequestURI().substring(request.getServletPath().length())); EndpointsContext context = new EndpointsContext(method, path, request, response, initParameters.isPrettyPrintEnabled()); if (!dispatcher.dispatch(method, path, context)) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); response.getWriter().append("Not Found"); } } }  @Override void init(ServletConfig config); @Override void service(HttpServletRequest request, HttpServletResponse response);  }
@Test public void explorer() throws IOException { req.setRequestURI("/_ah/api/explorer/"); req.setMethod("GET"); servlet.service(req, resp); assertThat(resp.getStatus()).isEqualTo(HttpServletResponse.SC_FOUND); assertThat(resp.getHeader("Location")).isEqualTo( "http: }
@Test public void notFound() throws IOException { req.setRequestURI("/_ah/api/notfound"); req.setMethod("GET"); servlet.service(req, resp); assertThat(resp.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_FOUND); }
@Test public void empty() throws IOException { req.setRequestURI("/_ah/api/test/v2/empty"); req.setMethod("GET"); servlet.service(req, resp); assertThat(resp.getStatus()).isEqualTo(HttpServletResponse.SC_NO_CONTENT); }
@Test public void echo() throws IOException { req.setRequestURI("/_ah/api/test/v2/echo"); req.setMethod("POST"); req.setParameter("x", "1"); servlet.service(req, resp); assertThat(resp.getStatus()).isEqualTo(HttpServletResponse.SC_OK); ObjectMapper mapper = ObjectMapperUtil.createStandardObjectMapper(); ObjectNode actual = mapper.readValue(resp.getContentAsString(), ObjectNode.class); assertThat(actual.size()).isEqualTo(1); assertThat(actual.get("x").asInt()).isEqualTo(1); }
@Test public void contentLengthHeaderNull() throws IOException { req.setRequestURI("/_ah/api/test/v2/echo"); req.setMethod("POST"); req.setParameter("x", "1"); servlet.service(req, resp); assertThat(resp.getHeader("Content-Length")).isNull(); }
@Test public void methodOverride() throws IOException { req.setRequestURI("/_ah/api/test/v2/increment"); req.setMethod("POST"); req.addHeader("X-HTTP-Method-Override", "PATCH"); req.setParameter("x", "1"); servlet.service(req, resp); assertThat(resp.getStatus()).isEqualTo(HttpServletResponse.SC_OK); ObjectMapper mapper = ObjectMapperUtil.createStandardObjectMapper(); ObjectNode actual = mapper.readValue(resp.getContentAsString(), ObjectNode.class); assertThat(actual.size()).isEqualTo(1); assertThat(actual.get("x").asInt()).isEqualTo(2); }
@Test public void proxy() throws IOException { req.setRequestURI("/_ah/api/static/proxy.html"); req.setMethod("GET"); servlet.service(req, resp); assertThat(resp.getStatus()).isEqualTo(HttpServletResponse.SC_OK); assertThat(resp.getContentAsString()).contains("googleapis.server.init()"); }
@Test public void cors() throws IOException { req.setRequestURI("/does/not/matter"); req.setMethod("OPTIONS"); req.addHeader("Access-Control-Request-Method", "DELETE"); req.addHeader("Access-Control-Request-Headers", "Test-Header"); req.addHeader("Origin", "http: servlet.service(req, resp); assertThat(resp.getStatus()).isEqualTo(HttpServletResponse.SC_OK); assertThat(resp.getHeader("Access-Control-Allow-Origin")).isEqualTo("http: assertThat(resp.getHeader("Access-Control-Allow-Headers")).isEqualTo("Test-Header"); assertThat(resp.getHeader("Access-Control-Max-Age")).isEqualTo("3600"); assertThat(resp.getHeader("Access-Control-Allow-Credentials")).isEqualTo("true"); assertThat(COMMA_SPLITTER.split(resp.getHeader("Access-Control-Allow-Methods"))) .containsExactly("HEAD", "DELETE", "GET", "PATCH", "POST", "PUT"); }