-
Notifications
You must be signed in to change notification settings - Fork 780
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
ganskef
wants to merge
1
commit into
adamfisk:master
Choose a base branch
from
ganskef:proxy_web_ui_test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
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(); | ||
} | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: