protected ResponseEntity<String> executeRequest(URI url, HttpMethod method, HttpHeaders headers, @Nullable String body) { Request httpRequest = this.httpClient.newRequest(url).method(method); addHttpHeaders(httpRequest, headers); if (body != null) { httpRequest.content(new StringContentProvider(body)); } ContentResponse response; try { response = httpRequest.send(); } catch (Exception ex) { throw new SockJsTransportFailureException("Failed to execute request to " + url, ex); } HttpStatus status = HttpStatus.valueOf(response.getStatus()); HttpHeaders responseHeaders = toHttpHeaders(response.getHeaders()); return (response.getContent() != null ? new ResponseEntity<>(response.getContentAsString(), responseHeaders, status) : new ResponseEntity<>(responseHeaders, status)); }
@Override public ClientResponse apply(final ClientRequest jerseyRequest) throws ProcessingException { final Request jettyRequest = translateRequest(jerseyRequest); final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(jerseyRequest.getHeaders(), jettyRequest); final ContentProvider entity = getBytesProvider(jerseyRequest); if (entity != null) { jettyRequest.content(entity); } try { final ContentResponse jettyResponse = jettyRequest.send(); HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, jerseyRequest.getHeaders(), JettyConnector.this.getClass().getName()); final javax.ws.rs.core.Response.StatusType status = jettyResponse.getReason() == null ? Statuses.from(jettyResponse.getStatus()) : Statuses.from(jettyResponse.getStatus(), jettyResponse.getReason()); final ClientResponse jerseyResponse = new ClientResponse(status, jerseyRequest); processResponseHeaders(jettyResponse.getHeaders(), jerseyResponse); try { jerseyResponse.setEntityStream(new HttpClientResponseInputStream(jettyResponse)); } catch (final IOException e) { LOGGER.log(Level.SEVERE, null, e); } return jerseyResponse; } catch (final Exception e) { throw new ProcessingException(e); } }
private void readConsulIndexHeader(ContentResponse httpResponse) { String value = httpResponse.getHeaders().get(CONSUL_INDEX); if (value != null) { consulIndex = value; } }
@Before public void setup() throws Exception { HttpClient httpClient = mock(HttpClient.class); response = mock(ContentResponse.class); when(response.getStatus()).thenReturn(200); when(response.getContentAsString()).thenReturn(healthInfo); HttpFields headers = new HttpFields(); headers.add(CONSUL_INDEX, "42"); when(response.getHeaders()).thenReturn(headers); Request request = mock(Request.class); when(httpClient.newRequest(anyString())).thenReturn(request); when(request.send()).thenReturn(response); props = new ServiceProperties(); props.addProperty(ServiceProperties.REGISTRY_SERVER_KEY, "localhost:1234"); worker = new RegistrationMonitorWorker(httpClient, props, mock(ServiceDependencyHealthCheck.class)); worker.setServiceName("foobar"); }
private void initialize() { httpResponse = mock(ContentResponse.class); when(httpResponse.getHeaders()).thenReturn(new HttpFields()); when(httpResponse.getStatus()).thenReturn(200); try { RpcEnvelope.Response.Builder responseBuilder = RpcEnvelope.Response.newBuilder(); responseBuilder.setServiceMethod("Test.test"); if (requestsFail) { RpcCallException callException = new RpcCallException( RpcCallException.Category.InternalServerError, "requests fail!"); responseBuilder.setError(callException.toJson().toString()); } RpcEnvelope.Response rpcResponse = responseBuilder.build(); byte[] responseHeader = rpcResponse.toByteArray(); byte[] payload = FrameworkTest.Foobar.newBuilder().build().toByteArray(); ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(Ints.toByteArray(responseHeader.length)); out.write(responseHeader); out.write(Ints.toByteArray(payload.length)); out.write(payload); out.flush(); when(httpResponse.getContent()).thenReturn(out.toByteArray()); } catch (Exception e) { e.printStackTrace(); } }
@Override public Map<String, String> getHttpHeader() { return convertHeaderToMap(response.getHeaders()); } };
public ResponseAssertion hasGzipContentEncoding() { Assertions.assertThat(response.getHeaders().get("Content-Encoding")).isEqualTo("gzip"); return this; }
public ResponseAssertion hasContentType(String contentType) { Assertions.assertThat(response.getHeaders().get(HttpHeader.CONTENT_TYPE).replace(" ", "")).contains(contentType.replace(" ", "")); return this; }
public ResponseAssertion hasContentLength(Integer value) { Assertions.assertThat(response.getHeaders().get("Content-Length")).isEqualTo(value.toString()); return this; }
public ResponseAssertion containsAllHeaders(String... headers) { Assertions.assertThat(response.getHeaders().stream().map(HttpField::getName)).contains(headers); return this; }
public ResponseAssertion hasNoContentEncoding() { Assertions.assertThat(response.getHeaders().get(HttpHeader.CONTENT_ENCODING)).isNull(); return this; }
public ResponseAssertion hasHeaderMatching(String header, String regexp) { Assertions.assertThat(response.getHeaders().get(header)).matches(regexp); return this; }
public ResponseAssertion hasNoContentType() { Assertions.assertThat(response.getHeaders().get(HttpHeader.CONTENT_TYPE)).isNull(); return this; }
public ResponseAssertion hasNoCharset() { Assertions.assertThat(response.getHeaders().get(HttpHeader.CONTENT_TYPE)).doesNotContain("charset"); return this; }
/** * Parse out information from LemonGraph response * @param res ContentResponse */ public void parseContentResponse(ContentResponse res) { JSONObject ret = new JSONObject(); try { ret = new JSONObject(res.getContentAsString()); } catch (Exception e) {} parseHeadersAndSetVariables(res.getHeaders()); parseJobId(ret); setContent(ret); setResponseCode(res.getStatus()); setSuccess(true); }
public ResponseAssertion isUnauthorized() { Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED_401); Assertions.assertThat(response.getHeaders().get(HttpHeader.WWW_AUTHENTICATE)).isNotNull().isNotEmpty(); return this; }
public ResponseAssertion hasExtendedHeaders() { Assertions.assertThat(response.getHeaders().get("Cluster-Primary-Owner")).isNotNull().isNotEmpty(); Assertions.assertThat(response.getHeaders().get("Cluster-Node-Name")).isNotNull().isNotEmpty(); Assertions.assertThat(response.getHeaders().get("Cluster-Server-Address")).isNotNull().isNotEmpty(); return this; }
public ResponseAssertion hasEtag() { Assertions.assertThat(response.getHeaders().get(HttpHeader.ETAG)).isNotNull().isNotEmpty(); return this; }
protected ResponseEntity<String> executeRequest(URI url, HttpMethod method, HttpHeaders headers, @Nullable String body) { Request httpRequest = this.httpClient.newRequest(url).method(method); addHttpHeaders(httpRequest, headers); if (body != null) { httpRequest.content(new StringContentProvider(body)); } ContentResponse response; try { response = httpRequest.send(); } catch (Exception ex) { throw new SockJsTransportFailureException("Failed to execute request to " + url, ex); } HttpStatus status = HttpStatus.valueOf(response.getStatus()); HttpHeaders responseHeaders = toHttpHeaders(response.getHeaders()); return (response.getContent() != null ? new ResponseEntity<>(response.getContentAsString(), responseHeaders, status) : new ResponseEntity<>(responseHeaders, status)); }
@Test public void shouldNotReturnValueIfSendingCorrectETag() throws Exception { //given putStringValueInCache("default", "test", "test"); //when ContentResponse firstResponse = client .newRequest(String.format("http://localhost:%d/rest/%s/%s", restServer().getPort(), "default", "test")) .method(HttpMethod.GET) .send(); String etagFromFirstCall = firstResponse.getHeaders().get("ETag"); ContentResponse secondResponse = client .newRequest(String.format("http://localhost:%d/rest/%s/%s", restServer().getPort(), "default", "test")) .header("If-None-Match", etagFromFirstCall) .method(HttpMethod.GET) .send(); //then Assertions.assertThat(etagFromFirstCall).isNotNull().isNotEmpty(); ResponseAssertion.assertThat(secondResponse).isNotModified(); }