@Test public void testResponseLowercaseLocationOk() { Function<HttpResponse, URI> function = new ParseURIFromListOrLocationHeaderIf20x(); HttpResponse response = createMock(HttpResponse.class); Payload payload = createMock(Payload.class); expect(response.getStatusCode()).andReturn(200).atLeastOnce(); expect(response.getFirstHeaderOrNull(CONTENT_TYPE)).andReturn("text/plain"); expect(response.getFirstHeaderOrNull(LOCATION)).andReturn(null); expect(response.getFirstHeaderOrNull("location")).andReturn("http: expect(response.getPayload()).andReturn(payload).atLeastOnce(); payload.release(); replay(payload); replay(response); assertEquals(function.apply(response), URI.create("http: verify(response); verify(payload); }
public URI apply(HttpResponse from) { if (from.getStatusCode() > 206) throw new HttpException(String.format("Unhandled status code - %1$s", from)); if ("text/uri-list".equals(from.getFirstHeaderOrNull(CONTENT_TYPE))) { try { if (from.getPayload().getInput() == null) throw new HttpResponseException("no content", null, from); String toParse = Strings2.toStringAndClose(from.getPayload().getInput()); return URI.create(toParse.trim()); } catch (IOException e) { throw new HttpResponseException("couldn't parse uri from content", null, from, e); } finally { releasePayload(from); } } else { releasePayload(from); String location = from.getFirstHeaderOrNull(LOCATION); if (location == null) location = from.getFirstHeaderOrNull("location"); if (location != null) { URI locationUri = URI.create(location); if (locationUri.getHost() != null) return locationUri; checkState(request != null, "request should have been initialized"); if (!location.startsWith("/")) location = "/" + location; locationUri = URI.create(location); return Uris.uriBuilder(request.getEndpoint()).path(locationUri.getPath()).query(locationUri.getQuery()).build(); } else { return null; } } }
ParseURIFromListOrLocationHeaderIf20x implements Function<HttpResponse, URI>,
      InvocationContext<ParseURIFromListOrLocationHeaderIf20x> { public URI apply(HttpResponse from) { if (from.getStatusCode() > 206) throw new HttpException(String.format("Unhandled status code - %1$s", from)); if ("text/uri-list".equals(from.getFirstHeaderOrNull(CONTENT_TYPE))) { try { if (from.getPayload().getInput() == null) throw new HttpResponseException("no content", null, from); String toParse = Strings2.toStringAndClose(from.getPayload().getInput()); return URI.create(toParse.trim()); } catch (IOException e) { throw new HttpResponseException("couldn't parse uri from content", null, from, e); } finally { releasePayload(from); } } else { releasePayload(from); String location = from.getFirstHeaderOrNull(LOCATION); if (location == null) location = from.getFirstHeaderOrNull("location"); if (location != null) { URI locationUri = URI.create(location); if (locationUri.getHost() != null) return locationUri; checkState(request != null, "request should have been initialized"); if (!location.startsWith("/")) location = "/" + location; locationUri = URI.create(location); return Uris.uriBuilder(request.getEndpoint()).path(locationUri.getPath()).query(locationUri.getQuery()).build(); } else { return null; } } } }
ParseURIFromListOrLocationHeaderIf20x implements Function<HttpResponse, URI>,
      InvocationContext<ParseURIFromListOrLocationHeaderIf20x> { public URI apply(HttpResponse from) { if (from.getStatusCode() > 206) throw new HttpException(String.format("Unhandled status code - %1$s", from)); if ("text/uri-list".equals(from.getFirstHeaderOrNull(CONTENT_TYPE))) { try { if (from.getPayload().getInput() == null) throw new HttpResponseException("no content", null, from); String toParse = Strings2.toStringAndClose(from.getPayload().getInput()); return URI.create(toParse.trim()); } catch (IOException e) { throw new HttpResponseException("couldn't parse uri from content", null, from, e); } finally { releasePayload(from); } } else { releasePayload(from); String location = from.getFirstHeaderOrNull(LOCATION); if (location == null) location = from.getFirstHeaderOrNull("location"); if (location != null) { URI locationUri = URI.create(location); if (locationUri.getHost() != null) return locationUri; checkState(request != null, "request should have been initialized"); if (!location.startsWith("/")) location = "/" + location; locationUri = URI.create(location); return Uris.uriBuilder(request.getEndpoint()).path(locationUri.getPath()).query(locationUri.getQuery()).build(); } else { return null; } } }  }
ParseURIFromListOrLocationHeaderIf20x implements Function<HttpResponse, URI>,
      InvocationContext<ParseURIFromListOrLocationHeaderIf20x> { public URI apply(HttpResponse from) { if (from.getStatusCode() > 206) throw new HttpException(String.format("Unhandled status code - %1$s", from)); if ("text/uri-list".equals(from.getFirstHeaderOrNull(CONTENT_TYPE))) { try { if (from.getPayload().getInput() == null) throw new HttpResponseException("no content", null, from); String toParse = Strings2.toStringAndClose(from.getPayload().getInput()); return URI.create(toParse.trim()); } catch (IOException e) { throw new HttpResponseException("couldn't parse uri from content", null, from, e); } finally { releasePayload(from); } } else { releasePayload(from); String location = from.getFirstHeaderOrNull(LOCATION); if (location == null) location = from.getFirstHeaderOrNull("location"); if (location != null) { URI locationUri = URI.create(location); if (locationUri.getHost() != null) return locationUri; checkState(request != null, "request should have been initialized"); if (!location.startsWith("/")) location = "/" + location; locationUri = URI.create(location); return Uris.uriBuilder(request.getEndpoint()).path(locationUri.getPath()).query(locationUri.getQuery()).build(); } else { return null; } } }  URI apply(HttpResponse from); @Override ParseURIFromListOrLocationHeaderIf20x setContext(HttpRequest request); }
ParseURIFromListOrLocationHeaderIf20x implements Function<HttpResponse, URI>,
      InvocationContext<ParseURIFromListOrLocationHeaderIf20x> { public URI apply(HttpResponse from) { if (from.getStatusCode() > 206) throw new HttpException(String.format("Unhandled status code - %1$s", from)); if ("text/uri-list".equals(from.getFirstHeaderOrNull(CONTENT_TYPE))) { try { if (from.getPayload().getInput() == null) throw new HttpResponseException("no content", null, from); String toParse = Strings2.toStringAndClose(from.getPayload().getInput()); return URI.create(toParse.trim()); } catch (IOException e) { throw new HttpResponseException("couldn't parse uri from content", null, from, e); } finally { releasePayload(from); } } else { releasePayload(from); String location = from.getFirstHeaderOrNull(LOCATION); if (location == null) location = from.getFirstHeaderOrNull("location"); if (location != null) { URI locationUri = URI.create(location); if (locationUri.getHost() != null) return locationUri; checkState(request != null, "request should have been initialized"); if (!location.startsWith("/")) location = "/" + location; locationUri = URI.create(location); return Uris.uriBuilder(request.getEndpoint()).path(locationUri.getPath()).query(locationUri.getQuery()).build(); } else { return null; } } }  URI apply(HttpResponse from); @Override ParseURIFromListOrLocationHeaderIf20x setContext(HttpRequest request);  }