public EagerHttpResponse<Response> withStatusLine(StatusLine statusLine) { return new EagerHttpResponse<>(getLibResponse().orElse(null), getRequest().orElse(null), statusLine, getHeaders(), getBody().orElse(null)); }
public EagerHttpResponse<Response> withStatusLine(StatusLine statusLine) { return new EagerHttpResponse<>(getLibResponse().orElse(null), getRequest().orElse(null), statusLine, getHeaders(), getBody().orElse(null)); }
@Override public EagerHttpResponse<Response> withHeaders(RawHttpHeaders headers, boolean append) { return new EagerHttpResponse<>(getLibResponse().orElse(null), getRequest().orElse(null), getStartLine(), append ? getHeaders().and(headers) : headers.and(getHeaders()), getBody().orElse(null)); }
@Override public EagerHttpResponse<Response> withHeaders(RawHttpHeaders headers, boolean append) { return new EagerHttpResponse<>(getLibResponse().orElse(null), getRequest().orElse(null), getStartLine(), append ? getHeaders().and(headers) : headers.and(getHeaders()), getBody().orElse(null)); }
@Test public void goingRawWithoutFancyClient() throws IOException { RawHttp rawHttp = new RawHttp(); RawHttpRequest request = rawHttp.parseRequest(String.format("GET localhost:%d/hello HTTP/1.0", PORT)); Socket socket = new Socket("localhost", PORT); request.writeTo(socket.getOutputStream()); EagerHttpResponse<?> response = rawHttp.parseResponse(socket.getInputStream()).eagerly(); assertThat(response.getStatusCode(), is(200)); assertThat(response.getBody().map(EagerBodyReader::toString) .orElseThrow(() -> new RuntimeException("No body")), equalTo("Hello")); }
@Test public void usingRawHttpWithHttpComponents() throws IOException { RawHttpClient<?> client = new RawHttpComponentsClient(); RawHttpRequest request = new RawHttp().parseRequest(String.format("GET localhost:%d/hello HTTP/1.0", PORT)); EagerHttpResponse<?> response = client.send(request).eagerly(); assertThat(response.getStatusCode(), is(200)); assertThat(response.getBody().map(EagerBodyReader::toString) .orElseThrow(() -> new RuntimeException("No body")), equalTo("Hello")); }
@Test public void frontPageExample() throws IOException { RawHttp rawHttp = new RawHttp(); RawHttpRequest request = rawHttp.parseRequest( "GET /hello.txt HTTP/1.1\r\n" + "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n" + "Host: www.example.com\r\n" + "Accept-Language: en, mi"); Socket socket = new Socket("www.example.com", 80); request.writeTo(socket.getOutputStream()); EagerHttpResponse<?> response = rawHttp.parseResponse(socket.getInputStream()).eagerly(); // call "eagerly()" in order to download the body System.out.println(response.eagerly()); assertThat(response.getStatusCode(), equalTo(404)); assertTrue(response.getBody().isPresent()); File responseFile = Files.createTempFile("rawhttp", ".http").toFile(); try (OutputStream out = Files.newOutputStream(responseFile.toPath())) { response.writeTo(out); } System.out.printf("Response parsed from file (%s):", responseFile); System.out.println(rawHttp.parseResponse(responseFile).eagerly()); }
rawHttpStatusCode = rawResponse.getStatusCode(); rawHttpContentType = rawResponse.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE).orElse(""); rawHttpResponseBody = rawResponse.getBody().map(EagerBodyReader::toString) .orElseThrow(() -> new RuntimeException("No body")); } catch (IOException e) {