protected HttpResponseImpl fetch(HTTPRequest request, HTTPMethod headGetDelete) { try { Future<HTTPResponse> fetchAsync = fetchService.fetchAsync(request); return new HttpResponseImpl(fetchAsync, this, request.getURL()); } catch (Exception e) { throw new HttpRequestException(e, "Failed to create a %s request: %s", headGetDelete, e.getMessage()); } }
HTTPRequestInfo(HTTPRequest req) { method = req.getMethod().toString(); byte[] payload = req.getPayload(); length = payload == null ? -1 : payload.length; url = req.getURL(); h1 = null; h2 = req.getHeaders(); }
static Response parseResponse(HTTPResponse response, HTTPRequest creatingRequest) { // Response URL will be null if it is the same as the request URL. URL responseUrl = response.getFinalUrl(); String urlString = (responseUrl != null ? responseUrl : creatingRequest.getURL()).toString(); int status = response.getResponseCode(); List<HTTPHeader> fetchHeaders = response.getHeaders(); List<Header> headers = new ArrayList<Header>(fetchHeaders.size()); String contentType = "application/octet-stream"; for (HTTPHeader fetchHeader : fetchHeaders) { String name = fetchHeader.getName(); String value = fetchHeader.getValue(); if ("Content-Type".equalsIgnoreCase(name)) { contentType = value; } headers.add(new Header(name, value)); } TypedByteArray body = null; byte[] fetchBody = response.getContent(); if (fetchBody != null) { body = new TypedByteArray(contentType, fetchBody); } return new Response(urlString, status, "", headers, body); } }
@Test void testConvertRequestGetsTargetAndUri() throws IOException { HttpRequest request = HttpRequest.builder().method(HttpMethod.GET).endpoint(endPoint).build(); HTTPRequest gaeRequest = req.apply(request); assertEquals(gaeRequest.getURL().getPath(), "/foo"); }
@Test void testConvertRequestGetsTargetAndUri() throws IOException { HttpRequest request = HttpRequest.builder().method(HttpMethod.GET).endpoint(endPoint).build(); HTTPRequest gaeRequest = req.apply(request); assertEquals(gaeRequest.getURL().getPath(), "/foo"); }
static HTTPRequest copyRequest(HTTPRequest in) { HTTPRequest out = new HTTPRequest(in.getURL(), in.getMethod(), in.getFetchOptions()); for (HTTPHeader h : in.getHeaders()) { out.addHeader(h); } out.setPayload(in.getPayload()); return out; } }
@Test public void testGetters() throws Exception { HTTPRequest request = new HTTPRequest(getFetchUrl(), HTTPMethod.PATCH, FetchOptions.Builder.withDefaults()); request.addHeader(new HTTPHeader("foo", "bar")); request.setPayload("qwerty".getBytes()); Assert.assertEquals(getFetchUrl(), request.getURL()); Assert.assertEquals(HTTPMethod.PATCH, request.getMethod()); Assert.assertNotNull(request.getFetchOptions()); Assert.assertNotNull(request.getHeaders()); Assert.assertEquals(1, request.getHeaders().size()); assertEquals(new HTTPHeader("foo", "bar"), request.getHeaders().get(0)); Assert.assertArrayEquals("qwerty".getBytes(), request.getPayload()); }