List of usage examples for org.apache.http.message BasicStatusLine BasicStatusLine
public BasicStatusLine(ProtocolVersion protocolVersion, int i, String str)
From source file:com.telefonica.iot.cygnus.backends.http.HttpBackendTest.java
/** * [HttpBackend.finishTransaction] -------- Once a transaction is finished, * the byte counts are correct./*from w w w .j av a 2 s . c o m*/ */ @Test public void testFinishTransaction() { System.out.println(getTestTraceHead("[HttpBackend.finishTransaction]") + "-------- Once a transaction is finished, the byte counts are correct"); // Create a response HttpResponseFactory factory = new DefaultHttpResponseFactory(); HttpResponse response = factory .newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null), null); String responseStr = "{\"somefield1\":\"somevalue1\",\"somefield2\":\"somevalue2\"," + "\"somefield3\":\"somevalue3\"}"; try { response.setEntity(new StringEntity(responseStr)); } catch (UnsupportedEncodingException e) { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- FAIL - There was some problem when creating the HttpResponse object"); throw new AssertionError(e.getMessage()); } // try catch response.addHeader("Content-Type", "application/json"); // Create the http backend HttpBackend httpBackend = new HttpBackendImpl(host, port, false, false, null, null, null, null, maxConns, maxConnsPerRoute); // Start transaction httpBackend.startTransaction(); // Process the response try { httpBackend.createJsonResponse(response); } catch (Exception e) { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- FAIL - There was some problem when creating the JsonResponse object"); throw new AssertionError(e.getMessage()); } // try catch // Finish transaction ImmutablePair<Long, Long> transactionBytes = httpBackend.finishTransaction(); // Check the byte counts try { assertEquals(0, transactionBytes.left.longValue()); System.out.println( getTestTraceHead("[HttpBackend.finishTransaction]") + "- OK - Request bytes is equals to 0"); } catch (AssertionError e) { System.out.println(getTestTraceHead("[HttpBackend.finishTransaction]") + "- FAIL - Request bytes is not equals to 0"); throw e; } // try catch try { assertEquals(79, transactionBytes.right.longValue()); System.out.println(getTestTraceHead("[HttpBackend.finishTransaction]") + "- OK - Response bytes is equals to 79"); } catch (AssertionError e) { System.out.println(getTestTraceHead("[HttpBackend.finishTransaction]") + "- FAIL - Response bytes is not equals to 79"); throw e; } // try catch }
From source file:org.finra.herd.tools.downloader.DownloaderWebClientTest.java
@Test public void testGetBusinessObjectDataDownloadCredentialAssertNoAuthorizationHeaderWhenNoSsl() throws Exception { HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class); HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils .getField(downloaderWebClient, "httpClientOperations"); ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations); try {// w w w .j a v a2 s.co m CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class); when(mockHttpClientOperations.execute(any(), any())).thenReturn(closeableHttpResponse); when(closeableHttpResponse.getStatusLine()) .thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "SUCCESS")); when(closeableHttpResponse.getEntity()) .thenReturn(new StringEntity(xmlHelper.objectToXml(new StorageUnitDownloadCredential()))); DownloaderInputManifestDto downloaderInputManifestDto = new DownloaderInputManifestDto(); String storageName = "storageName"; boolean useSsl = false; downloaderWebClient.getRegServerAccessParamsDto().setUseSsl(useSsl); downloaderWebClient.getStorageUnitDownloadCredential(downloaderInputManifestDto, storageName); verify(mockHttpClientOperations).execute(any(), argThat(httpUriRequest -> httpUriRequest.getFirstHeader("Authorization") == null)); } finally { ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations); } }
From source file:org.wso2.carbon.apimgt.handlers.AuthenticationHandlerTest.java
private CloseableHttpResponse getInvalidResponse() throws UnsupportedEncodingException { CloseableHttpResponse mockDCRResponse = new MockHttpResponse(); BasicHttpEntity responseEntity = new BasicHttpEntity(); responseEntity// w w w. j a va 2 s . c o m .setContent(new ByteArrayInputStream("invalid response".getBytes(StandardCharsets.UTF_8.name()))); responseEntity.setContentType(TestUtils.CONTENT_TYPE); mockDCRResponse.setEntity(responseEntity); mockDCRResponse.setStatusLine(new BasicStatusLine(new ProtocolVersion("http", 1, 0), 400, "Bad Request")); return mockDCRResponse; }
From source file:sonata.kernel.vimadaptor.wrapper.openstack.javastackclient.JavaStackCore.java
public synchronized HttpResponse getStackTemplate(String stackName, String stackId) throws IOException, URISyntaxException { HttpResponseFactory factory = new DefaultHttpResponseFactory(); HttpClient httpclient = HttpClientBuilder.create().build(); HttpGet getStackTemplate = null;/* w ww.j a va2 s .c o m*/ HttpResponse response = null; if (isAuthenticated) { URIBuilder builder = new URIBuilder(); String path = String.format("/%s/%s/stacks/%s/%s/template", Constants.HEAT_VERSION.toString(), this.tenant_id, stackName, stackId); builder.setScheme("http").setHost(endpoint).setPort(Integer.parseInt(Constants.HEAT_PORT.toString())) .setPath(path); URI uri = builder.build(); getStackTemplate = new HttpGet(uri); getStackTemplate.addHeader(Constants.AUTHTOKEN_HEADER.toString(), this.token_id); Logger.debug("Request: " + getStackTemplate.toString()); response = httpclient.execute(getStackTemplate); int status_code = response.getStatusLine().getStatusCode(); Logger.debug("Response: " + response.toString()); return (status_code == 200) ? response : factory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, status_code, "Get Template Failed with Status: " + status_code), null); } else { throw new IOException("You must Authenticate before issuing this request, please re-authenticate. "); } }
From source file:sonata.kernel.vimadaptor.wrapper.openstack.javastackclient.JavaStackCore.java
public synchronized HttpResponse showResourceData(String stackName, String stackId, String resourceName) throws IOException, URISyntaxException { HttpResponseFactory factory = new DefaultHttpResponseFactory(); HttpClient httpclient = HttpClientBuilder.create().build(); HttpGet showResourceData = null;// w w w . j a v a2s . com HttpResponse response = null; if (isAuthenticated) { URIBuilder builder = new URIBuilder(); String path = String.format("/%s/%s/stacks/%s/%s/resources/%s", Constants.HEAT_VERSION.toString(), this.tenant_id, stackName, stackId, resourceName); builder.setScheme("http").setHost(endpoint).setPort(Integer.parseInt(Constants.HEAT_PORT.toString())) .setPath(path); URI uri = builder.build(); showResourceData = new HttpGet(uri); showResourceData.addHeader(Constants.AUTHTOKEN_HEADER.toString(), this.token_id); response = httpclient.execute(showResourceData); int status_code = response.getStatusLine().getStatusCode(); return (status_code == 200) ? response : factory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, status_code, "List Failed with Status: " + status_code), null); } else { throw new IOException("You must Authenticate before issuing this request, please re-authenticate. "); } }
From source file:com.basho.riak.client.http.util.TestClientHelper.java
private void stubResponse(boolean stubRequest) throws IOException { if (stubRequest) { when(mockHttpClient.execute(any(HttpRequestBase.class))).thenReturn(mockHttpResponse); }/* w ww. j ava2 s .com*/ when(mockHttpResponse.getStatusLine()) .thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK")); when(mockHttpResponse.getEntity()).thenReturn(mockHttpEntity); }
From source file:org.finra.herd.tools.downloader.DownloaderWebClientTest.java
/** * Asserts that the http client is closed and if an exception is thrown during closing of the http client, the same exception is bubbled up. *///from w w w . jav a 2s .c o m @Test public void testGetBusinessObjectDataDownloadCredentialAssertThrowIOExceptionWhenClosingHttpClientThrowsIOException() throws Exception { HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class); HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils .getField(downloaderWebClient, "httpClientOperations"); ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations); try { CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class); when(mockHttpClientOperations.createHttpClient()).thenReturn(closeableHttpClient); doThrow(IOException.class).when(closeableHttpClient).close(); CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class); when(mockHttpClientOperations.execute(any(), any())).thenReturn(closeableHttpResponse); when(closeableHttpResponse.getStatusLine()) .thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "SUCCESS")); when(closeableHttpResponse.getEntity()) .thenReturn(new StringEntity(xmlHelper.objectToXml(new StorageUnitDownloadCredential()))); DownloaderInputManifestDto downloaderInputManifestDto = new DownloaderInputManifestDto(); String storageName = "storageName"; try { downloaderWebClient.getStorageUnitDownloadCredential(downloaderInputManifestDto, storageName); verify(closeableHttpClient).close(); fail(); } catch (Exception e) { assertEquals(IOException.class, e.getClass()); } } finally { ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations); } }
From source file:sonata.kernel.vimadaptor.wrapper.openstack.javastackclient.JavaStackCore.java
public synchronized HttpResponse listStackResources(String stackName, String stackId, ArrayList<String> resources) throws IOException, URISyntaxException { HttpResponseFactory factory = new DefaultHttpResponseFactory(); HttpClient httpclient = HttpClientBuilder.create().build(); HttpGet listResources = null;//from w ww .j a va2 s .com HttpResponse response = null; if (isAuthenticated) { URIBuilder builder = new URIBuilder(); String path = String.format("/%s/%s/stacks/%s/%s/resources", Constants.HEAT_VERSION.toString(), this.tenant_id, stackName, stackId); builder.setScheme("http").setHost(endpoint).setPort(Integer.parseInt(Constants.HEAT_PORT.toString())) .setPath(path); URI uri = builder.build(); listResources = new HttpGet(uri); listResources.addHeader(Constants.AUTHTOKEN_HEADER.toString(), this.token_id); response = httpclient.execute(listResources); int status_code = response.getStatusLine().getStatusCode(); return (status_code == 200) ? response : factory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, status_code, "List Failed with Status: " + status_code), null); } else { throw new IOException("You must Authenticate before issuing this request, please re-authenticate. "); } }
From source file:de.stklcode.jvault.connector.HTTPVaultConnectorOfflineTest.java
private void mockResponse(int status, String body, ContentType type) throws IOException { when(httpMock.execute(any())).thenReturn(responseMock); when(responseMock.getStatusLine())// www . j a v a 2 s. c om .thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), status, "")); when(responseMock.getEntity()).thenReturn(new StringEntity(body, type)); }
From source file:sonata.kernel.vimadaptor.wrapper.openstack.javastackclient.JavaStackCore.java
public HttpResponse listImages() throws IOException { HttpGet listImages = null;/*w ww .ja v a 2s . com*/ HttpResponse response = null; HttpClient httpClient = HttpClientBuilder.create().build(); HttpResponseFactory factory = new DefaultHttpResponseFactory(); if (isAuthenticated) { StringBuilder buildUrl = new StringBuilder(); buildUrl.append("http://"); buildUrl.append(endpoint); buildUrl.append(":"); buildUrl.append(Constants.IMAGE_PORT.toString()); buildUrl.append(String.format("/%s/images", Constants.IMAGE_VERSION.toString())); listImages = new HttpGet(buildUrl.toString()); listImages.addHeader(Constants.AUTHTOKEN_HEADER.toString(), this.token_id); response = httpClient.execute(listImages); int status_code = response.getStatusLine().getStatusCode(); return (status_code == 200) ? response : factory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, status_code, "Listing Images Failed with Status: " + status_code), null); } return response; }