@Test public void testExecuteRequestStatusCodeIsNotOk() throws Exception { when(mockResponse.getStatusLine()).thenReturn(HTTP_503_STATUSLINE); doReturn(mockResponse).when(httpClient).execute(any(HttpHost.class), HttpRequestMatcher.eq(request), any(HttpClientContext.class)); final BasicRestClient restClient = BasicRestClient.create() .host(LOCALHOST) .client(httpClient) .build(); final CloseableHttpResponse response = restClient.execute(request); assertThat(response, notNullValue()); assertThat(response, sameInstance(mockResponse)); assertThat(response.getStatusLine(), sameInstance(HTTP_503_STATUSLINE)); }
private CloseableHttpResponse execute(final HttpUriRequest request, final int previousStatusCode) throws CloudstackRESTException { if (counter.hasReachedExecutionLimit()) { throw new CloudstackRESTException("Reached max executions limit of " + executionLimit); } counter.incrementExecutionCounter(); s_logger.debug("Executing " + request.getMethod() + " request [execution count = " + counter.getValue() + "]"); final CloseableHttpResponse response = super.execute(request); final StatusLine statusLine = response.getStatusLine(); final int statusCode = statusLine.getStatusCode(); s_logger.debug("Status of last request: " + statusLine.toString()); if (HttpStatusCodeHelper.isUnauthorized(statusCode)) { return handleUnauthorizedResponse(request, previousStatusCode, response, statusCode); } else if (HttpStatusCodeHelper.isSuccess(statusCode)) { return handleSuccessResponse(request, response); } else if (HttpStatusCodeHelper.isConflict(statusCode)) { throw new CloudstackRESTException("Conflict: " + statusLine.getReasonPhrase(), statusCode); } else { throw new CloudstackRESTException("Unexpected status code: " + statusCode, statusCode); } }
@Test public void testExecuteRequest() throws Exception { when(mockResponse.getStatusLine()).thenReturn(HTTP_200_REPSONSE); doReturn(mockResponse).when(httpClient).execute(any(HttpHost.class), HttpRequestMatcher.eq(request), any(HttpClientContext.class)); final BasicRestClient restClient = BasicRestClient.create() .host(LOCALHOST) .client(httpClient) .build(); final CloseableHttpResponse response = restClient.execute(request); assertThat(response, notNullValue()); assertThat(response, sameInstance(mockResponse)); assertThat(response.getStatusLine(), sameInstance(HTTP_200_REPSONSE)); }
@Test(expected = CloudstackRESTException.class) public void testExecuteRequestWhenClientThrowsIOException() throws Exception { final BasicRestClient restClient = BasicRestClient.create() .host(LOCALHOST) .client(HttpClientHelper.createHttpClient(5)) .build(); final CloseableHttpResponse response = restClient.execute(request); fail("A CloudstackRESTException should have been thrown by now."+ "\nWe got " + response.getStatusLine() + "\nMake sure you cannot reach '" + request.getURI() + "' by method " + request.getMethod()); }
@Test public void testExecuteRequestStatusCodeIsNotOk() throws Exception { when(mockResponse.getStatusLine()).thenReturn(HTTP_503_STATUSLINE); doReturn(mockResponse).when(httpClient).execute(any(HttpHost.class), HttpRequestMatcher.eq(request), any(HttpClientContext.class)); final BasicRestClient restClient = BasicRestClient.create() .host(LOCALHOST) .client(httpClient) .build(); final CloseableHttpResponse response = restClient.execute(request); assertThat(response, notNullValue()); assertThat(response, sameInstance(mockResponse)); assertThat(response.getStatusLine(), sameInstance(HTTP_503_STATUSLINE)); }
@Test public void testExecuteRequest() throws Exception { when(mockResponse.getStatusLine()).thenReturn(HTTP_200_REPSONSE); doReturn(mockResponse).when(httpClient).execute(any(HttpHost.class), HttpRequestMatcher.eq(request), any(HttpClientContext.class)); final BasicRestClient restClient = BasicRestClient.create() .host(LOCALHOST) .client(httpClient) .build(); final CloseableHttpResponse response = restClient.execute(request); assertThat(response, notNullValue()); assertThat(response, sameInstance(mockResponse)); assertThat(response.getStatusLine(), sameInstance(HTTP_200_REPSONSE)); }
@Test(expected = CosmicRESTException.class) public void testExecuteRequestWhenClientThrowsIOException() throws Exception { final BasicRestClient restClient = BasicRestClient.create() .host(LOCALHOST) .client(HttpClientHelper.createHttpClient(5)) .build(); final CloseableHttpResponse response = restClient.execute(request); fail("A CloudstackRESTException should have been thrown by now." + "\nWe got " + response.getStatusLine() + "\nMake sure you cannot reach '" + request.getURI() + "' by method " + request.getMethod()); } }