Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a test to answer responses with content or to redirect. #224

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions src/test/java/org/littleshoot/proxy/ProxyWebUiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package org.littleshoot.proxy;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpHeaders.Names;
import io.netty.handler.codec.http.HttpHeaders.Values;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;

import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.littleshoot.proxy.impl.DefaultHttpProxyServer;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class ProxyWebUiTest {

private static final String REDIRECT_PATH = "/";

private HttpFiltersAdapter answerRedirectFilter(HttpRequest originalRequest) {
return new HttpFiltersAdapter(originalRequest) {
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
HttpResponse answer = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FOUND);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing to note here is that this method will return a new HTTP response when the client sends the HttpRequest, then return a new HttpResponse when the client sends the first HttpContent (if the request is a POST, for example). If the request is a GET, I believe Netty still sends an EmptyLastHttpContent through the pipeline after the HttpRequest, so this filter method would still return a duplicate HttpResponse.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is very simplified to show the trick. I use it with an UI in
my proxy. Three lists, some error/message pages and some actions which
needs to load modified content after redirect. It's like block/unblock,
delete/undelete, empty trash, rebuild index, such things... My use case
is a caching proxy for offline use (without an internet connection). The
search form uses the GET method. I haven't tried a POST request.

Am 31.07.2015 um 17:19 schrieb Jason Hoetger:

In src/test/java/org/littleshoot/proxy/ProxyWebUiTest.java
#224 (comment):

+import org.junit.Test;
+import org.littleshoot.proxy.impl.DefaultHttpProxyServer;
+import org.openqa.selenium.WebDriver;
+import org.openqa.selenium.firefox.FirefoxDriver;
+import org.openqa.selenium.remote.DesiredCapabilities;
+
+public class ProxyWebUiTest {
+

  • private static final String REDIRECT_PATH = "/";
  • private HttpFiltersAdapter answerRedirectFilter(HttpRequest originalRequest) {
  •    return new HttpFiltersAdapter(originalRequest) {
    
  •        @Override
    
  •        public HttpResponse clientToProxyRequest(HttpObject httpObject) {
    
  •            HttpResponse answer = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FOUND);
    

One thing to note here is that this method will return a new HTTP
response when the client sends the HttpRequest, then return a new
HttpResponse when the client sends the first HttpContent (if the request
is a POST, for example). If the request is a GET, I believe Netty still
sends an EmptyLastHttpContent through the pipeline after the
HttpRequest, so this filter method would still return a duplicate
HttpResponse.


Reply to this email directly or view it on GitHub
https://github.com/adamfisk/LittleProxy/pull/224/files#r35983395.

HttpHeaders.setHeader(answer, HttpHeaders.Names.LOCATION, REDIRECT_PATH);
HttpHeaders.setHeader(answer, Names.CONNECTION, Values.CLOSE);
return answer;
}
};
}

private HttpFiltersAdapter answerContentFilter(HttpRequest originalRequest) {
return new HttpFiltersAdapter(originalRequest) {
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
ByteBuf buffer = Unpooled.wrappedBuffer("CONTENT".getBytes());
HttpResponse answer = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buffer);
HttpHeaders.setContentLength(answer, buffer.readableBytes());
return answer;
}
};
}

@Test
public void testRedirectWithWebDriver() throws Exception {
HttpProxyServer proxyServer = null;
WebDriver driver = null;
try {

HttpFiltersSourceAdapter filtersSource = new HttpFiltersSourceAdapter() {
@Override
public HttpFilters filterRequest(HttpRequest originalRequest) {
if (REDIRECT_PATH.equals(originalRequest.getUri())) {
return answerContentFilter(originalRequest);
} else {
return answerRedirectFilter(originalRequest);
}
}

};
proxyServer = DefaultHttpProxyServer.bootstrap()//
.withFiltersSource(filtersSource)//
.withPort(0)//
.start();

DesiredCapabilities capability = DesiredCapabilities.htmlUnit();
driver = new FirefoxDriver(capability);
driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);

String urlStr = String.format("http://localhost:%d/somewhere", proxyServer.getListenAddress().getPort());
driver.get(urlStr);
String source = driver.getPageSource();

assertThat(source, containsString("CONTENT"));

} finally {
if (driver != null) {
driver.close();
}
if (proxyServer != null) {
proxyServer.abort();
}
}
}

}