@Override public String getMethodValue() { return this.httpRequest.getMethod(); }
@Override public String getMethodValue() { return this.httpRequest.getMethod(); }
@Override public String getMethodValue() { return this.httpRequest.getMethod(); }
@Override public String getMethodValue() { return this.httpRequest.getMethod(); }
@Override public String getMethodValue() { return this.httpRequest.getMethod(); }
@Override public String getMethodValue() { return this.httpRequest.getMethod(); }
LOG.trace(method.getMethod() + " " + uri + " " + resp.getStatusLine().getStatusCode() + " " + resp.getStatusLine().getReasonPhrase() + " in " + (endTime - startTime) + " ms");
public RequestWrapper(final HttpRequest request) throws ProtocolException { super(); if (request == null) { throw new IllegalArgumentException("HTTP request may not be null"); } this.original = request; setParams(request.getParams()); // Make a copy of the original URI if (request instanceof HttpUriRequest) { this.uri = ((HttpUriRequest) request).getURI(); this.method = ((HttpUriRequest) request).getMethod(); this.version = null; } else { RequestLine requestLine = request.getRequestLine(); try { this.uri = new URI(requestLine.getUri()); } catch (URISyntaxException ex) { throw new ProtocolException("Invalid request URI: " + requestLine.getUri(), ex); } this.method = requestLine.getMethod(); this.version = request.getProtocolVersion(); } this.execCount = 0; }
@Override protected String sendAndReturn(final HttpUriRequest request) throws IOException { final HttpResponseFactory factory = new DefaultHttpResponseFactory(); final HttpResponse response = factory.newHttpResponse( new BasicStatusLine(HttpVersion.HTTP_1_1, this.status, null), null); final StringBuilder sb = new StringBuilder(); sb.append(String.format("%s = %s;", "METHOD", request.getMethod())); sb.append(String.format("%s = %s;", "URI", request.getURI())); final HttpEntity entity = ((HttpEntityEnclosingRequestBase) request).getEntity(); sb.append(String.format("%s = %s;", "BODY", EntityUtils.toString(entity))); response.setEntity(new StringEntity(sb.toString())); return parseResponse(response); }
@Test public void shouldHandleMultipleInvocationsOfExecute() throws Exception { FakeHttp.addPendingHttpResponse(200, "a happy response body"); FakeHttp.addPendingHttpResponse(201, "another happy response body"); requestDirector.execute(null, new HttpGet("http://example.com"), null); requestDirector.execute(null, new HttpGet("www.example.com"), null); HttpUriRequest request1 = (HttpUriRequest) FakeHttp.getSentHttpRequest(0); assertThat(request1.getMethod()).isEqualTo(HttpGet.METHOD_NAME); assertThat(request1.getURI()).isEqualTo(URI.create("http://example.com")); HttpUriRequest request2 = (HttpUriRequest) FakeHttp.getSentHttpRequest(1); assertThat(request2.getMethod()).isEqualTo(HttpGet.METHOD_NAME); assertThat(request2.getURI()).isEqualTo(URI.create("www.example.com")); }
public void testUpsertSuccess() throws IOException, URISyntaxException { setup(SalesforceRestWriter.Operation.UPSERT); CloseableHttpResponse response = mock(CloseableHttpResponse.class); StatusLine statusLine = mock(StatusLine.class); when(client.execute(any(HttpUriRequest.class))).thenReturn(response); when(response.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(200); RestEntry<JsonObject> restEntry = new RestEntry<JsonObject>("test", new JsonObject()); Optional<HttpUriRequest> request = writer.onNewRecord(restEntry); Assert.assertTrue(request.isPresent(), "No HttpUriRequest from onNewRecord"); Assert.assertEquals("PATCH", request.get().getMethod()); writer = spy(writer); writer.write(restEntry); verify(writer, times(1)).writeImpl(restEntry); verify(writer, times(1)).onNewRecord(restEntry); verify(writer, times(1)).sendRequest(any(HttpUriRequest.class)); verify(client, times(1)).execute(any(HttpUriRequest.class)); verify(writer, times(1)).waitForResponse(any(ListenableFuture.class)); verify(writer, times(1)).processResponse(any(CloseableHttpResponse.class)); verify(writer, never()).onConnect(any(URI.class)); }
public void testInsertSuccess() throws IOException, URISyntaxException { setup(SalesforceRestWriter.Operation.INSERT_ONLY_NOT_EXIST); CloseableHttpResponse response = mock(CloseableHttpResponse.class); StatusLine statusLine = mock(StatusLine.class); when(client.execute(any(HttpUriRequest.class))).thenReturn(response); when(response.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(200); RestEntry<JsonObject> restEntry = new RestEntry<JsonObject>("test", new JsonObject()); Optional<HttpUriRequest> request = writer.onNewRecord(restEntry); Assert.assertTrue(request.isPresent(), "No HttpUriRequest from onNewRecord"); Assert.assertEquals("POST", request.get().getMethod()); writer = spy(writer); writer.write(restEntry); verify(writer, times(1)).writeImpl(restEntry); verify(writer, times(1)).onNewRecord(restEntry); verify(writer, times(1)).sendRequest(any(HttpUriRequest.class)); verify(client, times(1)).execute(any(HttpUriRequest.class)); verify(writer, times(1)).waitForResponse(any(ListenableFuture.class)); verify(writer, times(1)).processResponse(any(CloseableHttpResponse.class)); verify(writer, never()).onConnect(any(URI.class)); }
@Override protected void fillHttpOutputData(Schema httpOutputSchema, GenericRecord outputRecord, HttpUriRequest rawRequest, ResponseStatus status) throws IOException { ApacheHttpResponseStatus apacheStatus = (ApacheHttpResponseStatus) status; HttpRequestResponseRecord record = new HttpRequestResponseRecord(); record.setRequestUrl(rawRequest.getURI().toASCIIString()); record.setMethod(rawRequest.getMethod()); record.setStatusCode(apacheStatus.getStatusCode()); record.setContentType(apacheStatus.getContentType()); record.setBody(apacheStatus.getContent() == null? null: ByteBuffer.wrap(apacheStatus.getContent())); outputRecord.put(HTTP_REQUEST_RESPONSE_FIELD, record); } }
public static void assertEqual(RequestBuilder actual, RequestBuilder expect) throws IOException { // Check entity HttpEntity actualEntity = actual.getEntity(); HttpEntity expectedEntity = expect.getEntity(); if (actualEntity == null) { Assert.assertTrue(expectedEntity == null); } else { Assert.assertEquals(actualEntity.getContentLength(), expectedEntity.getContentLength()); String actualContent = IOUtils.toString(actualEntity.getContent(), StandardCharsets.UTF_8); String expectedContent = IOUtils.toString(expectedEntity.getContent(), StandardCharsets.UTF_8); Assert.assertEquals(actualContent, expectedContent); } // Check request HttpUriRequest actualRequest = actual.build(); HttpUriRequest expectedRequest = expect.build(); Assert.assertEquals(actualRequest.getMethod(), expectedRequest.getMethod()); Assert.assertEquals(actualRequest.getURI().toString(), expectedRequest.getURI().toString()); Header[] actualHeaders = actualRequest.getAllHeaders(); Header[] expectedHeaders = expectedRequest.getAllHeaders(); Assert.assertEquals(actualHeaders.length, expectedHeaders.length); for (int i = 0; i < actualHeaders.length; i++) { Assert.assertEquals(actualHeaders[i].toString(), expectedHeaders[i].toString()); } }
public void testFailure() throws IOException, URISyntaxException { setup(SalesforceRestWriter.Operation.INSERT_ONLY_NOT_EXIST); CloseableHttpResponse response = mock(CloseableHttpResponse.class); StatusLine statusLine = mock(StatusLine.class); when(client.execute(any(HttpUriRequest.class))).thenReturn(response); when(response.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(400); RestEntry<JsonObject> restEntry = new RestEntry<JsonObject>("test", new JsonObject()); Optional<HttpUriRequest> request = writer.onNewRecord(restEntry); Assert.assertTrue(request.isPresent(), "No HttpUriRequest from onNewRecord"); Assert.assertEquals("POST", request.get().getMethod()); writer = spy(writer); try { writer.write(restEntry); Assert.fail("Should fail on 400 status code"); } catch (Exception e) {} verify(writer, times(1)).writeImpl(restEntry); verify(writer, times(1)).onNewRecord(restEntry); verify(writer, times(1)).sendRequest(any(HttpUriRequest.class)); verify(client, times(1)).execute(any(HttpUriRequest.class)); verify(writer, times(1)).waitForResponse(any(ListenableFuture.class)); verify(writer, times(1)).processResponse(any(CloseableHttpResponse.class)); verify(writer, never()).onConnect(any(URI.class)); }
public void testInsertDuplicate() throws IOException, URISyntaxException { setup(SalesforceRestWriter.Operation.INSERT_ONLY_NOT_EXIST); CloseableHttpResponse response = mock(CloseableHttpResponse.class); StatusLine statusLine = mock(StatusLine.class); when(client.execute(any(HttpUriRequest.class))).thenReturn(response); when(response.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(400); HttpEntity entity = mock(HttpEntity.class); when(response.getEntity()).thenReturn(entity); JsonObject json = new JsonObject(); json.addProperty("errorCode", SalesforceRestWriter.DUPLICATE_VALUE_ERR_CODE); JsonArray jsonArray = new JsonArray(); jsonArray.add(json); when(entity.getContent()).thenReturn(new ByteArrayInputStream(jsonArray.toString().getBytes())); RestEntry<JsonObject> restEntry = new RestEntry<JsonObject>("test", new JsonObject()); Optional<HttpUriRequest> request = writer.onNewRecord(restEntry); Assert.assertTrue(request.isPresent(), "No HttpUriRequest from onNewRecord"); Assert.assertEquals("POST", request.get().getMethod()); writer = spy(writer); writer.write(restEntry); verify(writer, times(1)).writeImpl(restEntry); verify(writer, times(1)).onNewRecord(restEntry); verify(writer, times(1)).sendRequest(any(HttpUriRequest.class)); verify(client, times(1)).execute(any(HttpUriRequest.class)); verify(writer, times(1)).waitForResponse(any(ListenableFuture.class)); verify(writer, times(1)).processResponse(any(CloseableHttpResponse.class)); verify(writer, never()).onConnect(any(URI.class)); }
public void testAccessTokenReacquire() throws IOException, URISyntaxException { setup(SalesforceRestWriter.Operation.INSERT_ONLY_NOT_EXIST); CloseableHttpResponse response = mock(CloseableHttpResponse.class); StatusLine statusLine = mock(StatusLine.class); when(client.execute(any(HttpUriRequest.class))).thenReturn(response); when(response.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(401); RestEntry<JsonObject> restEntry = new RestEntry<JsonObject>("test", new JsonObject()); Optional<HttpUriRequest> request = writer.onNewRecord(restEntry); Assert.assertTrue(request.isPresent(), "No HttpUriRequest from onNewRecord"); Assert.assertEquals("POST", request.get().getMethod()); writer = spy(writer); try { writer.write(restEntry); } catch (Exception e) { } verify(writer, times(1)).writeImpl(restEntry); verify(writer, times(1)).onNewRecord(restEntry); verify(writer, times(1)).sendRequest(any(HttpUriRequest.class)); verify(client, times(1)).execute(any(HttpUriRequest.class)); verify(writer, times(1)).waitForResponse(any(ListenableFuture.class)); verify(writer, times(1)).processResponse(any(CloseableHttpResponse.class)); verify(writer, times(1)).onConnect(any(URI.class)); } }
private void logRequestExecution(final HttpUriRequest request) { final URI uri = request.getURI(); String query = uri.getQuery(); query = query != null ? "?" + query : ""; s_logger.debug("Executig " + request.getMethod() + " request on " + clientContext.getTargetHost() + uri.getPath() + query); }
private void log(HttpUriRequest request) { if (logger != null) { List<String> headers = new ArrayList<>(); for (Header h : request.getAllHeaders()) { headers.add(h.toString()); } logger.logRequest(request.getMethod(), request.getURI().toString(), headers, null); } } private void log(HttpEntityEnclosingRequestBase request) {
@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()); }