Search in sources :

Example 6 with Response

use of org.sonar.api.server.ws.Response in project sonarqube by SonarSource.

the class WebServiceEngineTest method does_not_fail_when_request_is_aborted_and_response_is_committed.

@Test
public void does_not_fail_when_request_is_aborted_and_response_is_committed() throws Exception {
    ValidatingRequest request = new TestRequest().setMethod("GET").setPath("/api/system/fail_with_client_abort_exception");
    Response response = mock(Response.class);
    ServletResponse.ServletStream servletStream = mock(ServletResponse.ServletStream.class);
    when(response.stream()).thenReturn(servletStream);
    HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
    when(httpServletResponse.isCommitted()).thenReturn(true);
    when(servletStream.response()).thenReturn(httpServletResponse);
    underTest.execute(request, response);
    assertThat(logTester.logs(LoggerLevel.DEBUG)).isNotEmpty();
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.sonar.api.server.ws.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) ValidatingRequest(org.sonar.api.server.ws.internal.ValidatingRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 7 with Response

use of org.sonar.api.server.ws.Response in project sonarqube by SonarSource.

the class WebServiceEngineTest method internal_error.

@Test
public void internal_error() {
    ValidatingRequest request = new TestRequest().setMethod("GET").setPath("/api/system/fail");
    DumbResponse response = new DumbResponse();
    underTest.execute(request, response);
    assertThat(response.stream().outputAsString()).isEqualTo("{\"errors\":[{\"msg\":\"An error has occurred. Please contact your administrator\"}]}");
    assertThat(response.stream().status()).isEqualTo(500);
    assertThat(response.stream().mediaType()).isEqualTo(MediaTypes.JSON);
    assertThat(logTester.logs(LoggerLevel.ERROR)).filteredOn(l -> l.contains("Fail to process request")).isNotEmpty();
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) ClientAbortException(org.apache.catalina.connector.ClientAbortException) Mockito.when(org.mockito.Mockito.when) BadRequestException(org.sonar.server.exceptions.BadRequestException) ArrayList(java.util.ArrayList) ValidatingRequest(org.sonar.api.server.ws.internal.ValidatingRequest) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) WebService(org.sonar.api.server.ws.WebService) After(org.junit.After) Response(org.sonar.api.server.ws.Response) MediaTypes(org.sonarqube.ws.MediaTypes) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) Mockito.mock(org.mockito.Mockito.mock) ValidatingRequest(org.sonar.api.server.ws.internal.ValidatingRequest) Test(org.junit.Test)

Aggregations

Response (org.sonar.api.server.ws.Response)7 WebService (org.sonar.api.server.ws.WebService)5 List (java.util.List)4 Request (org.sonar.api.server.ws.Request)4 DbClient (org.sonar.db.DbClient)4 DbSession (org.sonar.db.DbSession)4 UserSession (org.sonar.server.user.UserSession)4 ComponentDto (org.sonar.db.component.ComponentDto)3 String.format (java.lang.String.format)2 Optional (java.util.Optional)2 Nullable (javax.annotation.Nullable)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Test (org.junit.Test)2 ValidatingRequest (org.sonar.api.server.ws.internal.ValidatingRequest)2 UserRole (org.sonar.api.web.UserRole)2 Collectors (org.sonar.core.util.stream.Collectors)2 OrganizationDto (org.sonar.db.organization.OrganizationDto)2 ADMINISTER (org.sonar.db.permission.OrganizationPermission.ADMINISTER)2 ComponentFinder (org.sonar.server.component.ComponentFinder)2 BadRequestException (org.sonar.server.exceptions.BadRequestException)2