예제 #1
0
  @Override
  public void handle(
      final HttpServletRequest request,
      final HttpServletResponseWithGetStatus wrapper,
      final StubbedDataManager stubbedDataManager)
      throws Exception {

    if (!request.getRequestURI().equals(AdminHandler.ADMIN_ROOT)) {
      wrapper.setStatus(HttpStatus.METHOD_NOT_ALLOWED_405);
      wrapper.getWriter().println("Method POST is not allowed on URI " + request.getRequestURI());
      return;
    }

    final String post = HandlerUtils.extractPostRequestBody(request, AdminHandler.NAME);
    if (!StringUtils.isSet(post)) {
      final String errorMessage =
          String.format(
              "%s request on URI %s was empty", request.getMethod(), request.getRequestURI());
      HandlerUtils.configureErrorResponse(wrapper, HttpStatus.NO_CONTENT_204, errorMessage);
      return;
    }

    stubbedDataManager.refreshStubbedData(new YamlParser(), post);

    if (stubbedDataManager.getStubHttpLifecycles().size() == 1) {
      wrapper.addHeader(HttpHeaders.LOCATION, stubbedDataManager.getOnlyStubRequestUrl());
    }

    wrapper.setStatus(HttpStatus.CREATED_201);
    wrapper.getWriter().println("Configuration created successfully");
  }
예제 #2
0
  @Test
  public void shouldPrintToConsoleExpectedErrorWithColor_WhenStatus_LessThan100() throws Exception {

    final int expectedStatus = 99;
    final String expectedHandlerName = "stubs";
    final String expectedConsoleOutput =
        String.format("%s [%s]%s 99\u001B[0m", expectedStatus, expectedHandlerName, URI);

    final HttpServletResponseWithGetStatus wrapper =
        new HttpServletResponseWithGetStatus(httpServletResponseMock);
    wrapper.setStatus(expectedStatus);

    ConsoleUtils.logOutgoingResponse(httpServletRequestMock.getRequestURI(), wrapper, "stubs");
    final String actualConsoleOutput = consoleCaptor.toString(StringUtils.UTF_8).trim();

    assertThat(actualConsoleOutput).contains(expectedConsoleOutput);
  }