Skip to content

Commit

Permalink
fix: disable uri normalization in ApacheHttpRequest (#804)
Browse files Browse the repository at this point in the history
* fix: disable uri normalization in ApacheHttpRequest

* test: add in-memory, local webserver for testing requested url

* fix: use try-with-resources on OutputStream

* fix: provide port of 0 to let InetSocketAddress pick a port
  • Loading branch information
chingor13 authored Aug 30, 2019
1 parent 41f6a2f commit 0e6d451
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ final class ApacheHttpRequest extends LowLevelHttpRequest {
// disable redirects as google-http-client handles redirects
this.requestConfig = RequestConfig.custom()
.setRedirectsEnabled(false)
.setNormalizeUri(false)
// TODO(chingor): configure in HttpClientBuilder when available
.setStaleConnectionCheckEnabled(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.LowLevelHttpResponse;
import com.google.api.client.util.ByteArrayStreamingContent;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.http.Header;
Expand Down Expand Up @@ -175,4 +182,33 @@ public void process(HttpRequest request, HttpContext context)
}
assertTrue("Expected to have called our test interceptor", interceptorCalled.get());
}

@Test
public void testNormalizedUrl() throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
server.createContext(
"/",
new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
byte[] response = httpExchange.getRequestURI().toString().getBytes();
httpExchange.sendResponseHeaders(200, response.length);
try (OutputStream out = httpExchange.getResponseBody()) {
out.write(response);
}
}
});
server.start();

ApacheHttpTransport transport = new ApacheHttpTransport();
GenericUrl testUrl = new GenericUrl("http://localhost/foo//bar");
testUrl.setPort(server.getAddress().getPort());
com.google.api.client.http.HttpResponse response =
transport
.createRequestFactory()
.buildGetRequest(testUrl)
.execute();
assertEquals(200, response.getStatusCode());
assertEquals("/foo//bar", response.parseAsString());
}
}

0 comments on commit 0e6d451

Please sign in to comment.