@Test public void shouldAvoidDoubleSlashWhenBothEndpointAndPathSupplyOne() throws Exception { Request<?> amazonRequest = HttpClient.convertToRequest(new HttpRequest(HttpMethodName.GET)); amazonRequest.setEndpoint(new URI("https://endpoint/")); amazonRequest.setResourcePath("/resource/path/"); HTTPRequest googleRequest = new AmazonHttpRequestToGoogleHttpRequestAdaptor().convert(amazonRequest); assertEquals("https://endpoint/resource/path/", googleRequest.getURL().toString()); }
@Test public void shouldNotAddAdditionalSlashWhenEndpointContainsOneButPathIsEmpty() throws Exception { Request<?> amazonRequest = HttpClient.convertToRequest(new HttpRequest(HttpMethodName.GET)); amazonRequest.setEndpoint(new URI("https://endpoint.with.hostname/and_a_path")); amazonRequest.setResourcePath(""); HTTPRequest googleRequest = new AmazonHttpRequestToGoogleHttpRequestAdaptor().convert(amazonRequest); assertEquals("https://endpoint.with.hostname/and_a_path", googleRequest.getURL().toString()); }
@Test public void shouldAppendTrailingSlashWhenEndpointLacksOneAndNeitherPathNorQueryStringSupplied() throws Exception { Request<?> amazonRequest = HttpClient.convertToRequest(new HttpRequest(HttpMethodName.GET)); amazonRequest.setEndpoint(new URI("https://hostname.without.a.trailing.slash")); HTTPRequest googleRequest = new AmazonHttpRequestToGoogleHttpRequestAdaptor().convert(amazonRequest); assertEquals("https://hostname.without.a.trailing.slash/", googleRequest.getURL().toString()); }
@Test public void shouldAddSlashBetweenHostAndQueryStringWhenEndpointHasNoSlashAndPathIsEmpty() throws Exception { Request<?> amazonRequest = HttpClient.convertToRequest(new HttpRequest(HttpMethodName.GET)); amazonRequest.setEndpoint(new URI("https://hostname.without.a.trailing.slash")); amazonRequest.addParameter("key", "value"); HTTPRequest googleRequest = new AmazonHttpRequestToGoogleHttpRequestAdaptor().convert(amazonRequest); assertEquals( "https://hostname.without.a.trailing.slash/?key=value", googleRequest.getURL().toString()); }
@Test public void shouldInsertSlashBetweenEndpointAndPathWhenNeitherHasOne() throws Exception { Request<?> amazonRequest = HttpClient.convertToRequest(new HttpRequest(HttpMethodName.GET)); amazonRequest.setEndpoint(new URI("https://hostname.without.a.trailing.slash")); amazonRequest.setResourcePath("resource/without/a/leading/slash/"); HTTPRequest googleRequest = new AmazonHttpRequestToGoogleHttpRequestAdaptor().convert(amazonRequest); assertEquals( "https://hostname.without.a.trailing.slash/resource/without/a/leading/slash/", googleRequest.getURL().toString()); }
@Test public void shouldPutContentIntoPayload() throws Exception { HttpRequest amazonRequest = new HttpRequest(HttpMethodName.POST); amazonRequest.setEndpoint(new URI("https://endpoint/")); amazonRequest.setContent(new ByteArrayInputStream("PAYLOAD".getBytes())); HTTPRequest googleRequest = new AmazonHttpRequestToGoogleHttpRequestAdaptor() .convert(HttpClient.convertToRequest(amazonRequest)); assertEquals("https://endpoint/", googleRequest.getURL().toString()); assertEquals("PAYLOAD", new String(googleRequest.getPayload())); }
@Test public void shouldIncludeParametersInRequestBodyForPostRequestIfNoContentSupplied() throws Exception { HttpRequest amazonRequest = new HttpRequest(HttpMethodName.POST); amazonRequest.setEndpoint(new URI("https://endpoint/")); amazonRequest.addParameter("key", "value"); HTTPRequest googleRequest = new AmazonHttpRequestToGoogleHttpRequestAdaptor() .convert(HttpClient.convertToRequest(amazonRequest)); assertEquals("https://endpoint/", googleRequest.getURL().toString()); assertEquals("key=value", new String(googleRequest.getPayload())); }
@Test public void shouldPutParamatersInQueryStringWhenContentAlsoSupplied() throws Exception { HttpRequest amazonRequest = new HttpRequest(HttpMethodName.POST); amazonRequest.setEndpoint(new URI("https://endpoint/")); amazonRequest.addParameter("key", "value"); amazonRequest.setContent(new ByteArrayInputStream("PAYLOAD".getBytes())); HTTPRequest googleRequest = new AmazonHttpRequestToGoogleHttpRequestAdaptor() .convert(HttpClient.convertToRequest(amazonRequest)); assertEquals("https://endpoint/?key=value", googleRequest.getURL().toString()); assertEquals("PAYLOAD", new String(googleRequest.getPayload())); }
@Test public void shouldConcatenateEndpointWithPathAndQueryString() throws Exception { Request<?> amazonRequest = HttpClient.convertToRequest(new HttpRequest(HttpMethodName.GET)); amazonRequest.setEndpoint(new URI("https://endpoint")); amazonRequest.setResourcePath("/resource/path/"); amazonRequest.addParameter("key1", "value1"); amazonRequest.addParameter("key2", "value2"); HTTPRequest googleRequest = new AmazonHttpRequestToGoogleHttpRequestAdaptor().convert(amazonRequest); assertEquals( "https://endpoint/resource/path/?key2=value2&key1=value1", googleRequest.getURL().toString()); }
private static Request<?> request(HttpMethodName method) throws URISyntaxException { HttpRequest request = new HttpRequest(method); request.setEndpoint(new URI("http://endpoint")); return HttpClient.convertToRequest(request); }