### Question:
ConvertToGaeRequest implements Function<HttpRequest, HTTPRequest> { @Override public HTTPRequest apply(HttpRequest request) { URL url = null; try { url = request.getEndpoint().toURL(); } catch (MalformedURLException e) { Throwables.propagate(e); } FetchOptions options = disallowTruncate(); options.doNotFollowRedirects(); if (utils.relaxHostname() || utils.trustAllCerts()) options.doNotFollowRedirects(); options.setDeadline(10.0); HTTPRequest gaeRequest = new HTTPRequest(url, HTTPMethod.valueOf(request.getMethod().toString()), options); for (Entry<String, String> entry : request.getHeaders().entries()) { String header = entry.getKey(); if (!prohibitedHeaders.contains(header)) gaeRequest.addHeader(new HTTPHeader(header, entry.getValue())); } gaeRequest.addHeader(new HTTPHeader(HttpHeaders.USER_AGENT, USER_AGENT)); if (request.getPayload() != null) { InputStream input = request.getPayload().getInput(); try { byte[] array = ByteStreams.toByteArray(input); if (!request.getPayload().isRepeatable()) { Payload oldPayload = request.getPayload(); request.setPayload(array); HttpUtils.copy(oldPayload.getContentMetadata(), request.getPayload().getContentMetadata()); } gaeRequest.setPayload(array); if (array.length > 0) { gaeRequest.setHeader(new HTTPHeader("Expect", "100-continue")); } } catch (IOException e) { Throwables.propagate(e); } finally { Closeables2.closeQuietly(input); } for (Entry<String, String> header : contentMetadataCodec.toHeaders( request.getPayload().getContentMetadata()).entries()) { if (!prohibitedHeaders.contains(header.getKey())) gaeRequest.setHeader(new HTTPHeader(header.getKey(), header.getValue())); } } return gaeRequest; } @Inject  ConvertToGaeRequest(HttpUtils utils, ContentMetadataCodec contentMetadataCodec); @Override HTTPRequest apply(HttpRequest request); static final String USER_AGENT; final Set<String> prohibitedHeaders; }### Answer:
@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 testConvertRequestSetsFetchOptions() throws IOException { HttpRequest request = HttpRequest.builder().method(HttpMethod.GET).endpoint(endPoint).build(); HTTPRequest gaeRequest = req.apply(request); assert gaeRequest.getFetchOptions() != null; }
@Test void testConvertRequestSetsHeaders() throws IOException { HttpRequest request = HttpRequest.builder() .method(HttpMethod.GET) .endpoint(endPoint) .addHeader("foo", "bar").build(); HTTPRequest gaeRequest = req.apply(request); assertEquals(gaeRequest.getHeaders().get(0).getName(), "foo"); assertEquals(gaeRequest.getHeaders().get(0).getValue(), "bar"); }
@Test void testConvertRequestNoContent() throws IOException { HttpRequest request = HttpRequest.builder().method(HttpMethod.GET).endpoint(endPoint).build(); HTTPRequest gaeRequest = req.apply(request); assert gaeRequest.getPayload() == null; assertEquals(gaeRequest.getHeaders().size(), 1); assertEquals(gaeRequest.getHeaders().get(0).getName(), HttpHeaders.USER_AGENT); assertEquals(gaeRequest.getHeaders().get(0).getValue(), "jclouds/1.0 urlfetch/1.4.3"); }
@Test(expectedExceptions = UnsupportedOperationException.class) void testConvertRequestBadContent() throws IOException { HttpRequest request = HttpRequest.builder() .method(HttpMethod.GET) .endpoint(endPoint) .payload(Payloads.newPayload(new Date())).build(); req.apply(request); }