Skip to content

Commit f519c45

Browse files
committed
Added test demonstrating short-circuiting with Javascript filters
1 parent 154fe9b commit f519c45

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

browsermob-rest/src/test/groovy/net/lightbody/bmp/proxy/FilterTest.groovy

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ import org.mockserver.matchers.Times
1414
import org.mockserver.model.Header
1515

1616
import static org.hamcrest.Matchers.endsWith
17+
import static org.hamcrest.Matchers.greaterThanOrEqualTo
1718
import static org.junit.Assert.assertEquals
1819
import static org.junit.Assert.assertThat
1920
import static org.junit.Assert.assertTrue
21+
import static org.junit.Assert.fail
22+
import static org.junit.Assume.assumeThat
2023
import static org.mockito.Matchers.any
2124
import static org.mockito.Mockito.mock
2225
import static org.mockito.Mockito.never
@@ -237,6 +240,49 @@ class FilterTest extends ProxyResourceTest {
237240
verify(mockProxy, never()).addResponseFilter(any(ResponseFilter))
238241
}
239242

243+
@Test
244+
void testCanShortCircuitRequestWithJavascript() {
245+
def javaVersion = System.getProperty("java.specification.version") as double
246+
assumeThat("Skipping Nashorn-dependent test on Java 1.7", javaVersion, greaterThanOrEqualTo(1.8d))
247+
248+
final String requestFilterJavaScript =
249+
'''
250+
// "import" classes
251+
var DefaultFullHttpResponse = Java.type('io.netty.handler.codec.http.DefaultFullHttpResponse');
252+
var HttpResponseStatus = Java.type('io.netty.handler.codec.http.HttpResponseStatus');
253+
var HttpObjectUtil = Java.type('net.lightbody.bmp.util.HttpObjectUtil');
254+
255+
// create a new DefaultFullHttpResponse that will short-circuit the request
256+
var shortCircuitRequest = new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.PAYMENT_REQUIRED);
257+
258+
// use the convenient HttpObjectUtil.replaceTextHttpEntityBody() method to set the entity body
259+
var responseBody = 'You have to pay the troll toll to get into this Proxy\\'s soul';
260+
HttpObjectUtil.replaceTextHttpEntityBody(shortCircuitRequest, responseBody);
261+
262+
// return the short-circuit FullHttpResponse
263+
shortCircuitRequest;
264+
'''
265+
266+
Request<String> mockRestRequest = createMockRestRequestWithEntity(requestFilterJavaScript)
267+
268+
proxyResource.addRequestFilter(proxyPort, mockRestRequest)
269+
270+
HTTPBuilder http = getHttpBuilder()
271+
272+
http.request(Method.GET, ContentType.TEXT_PLAIN) { req ->
273+
uri.path = "/testShortCircuit"
274+
275+
response.success = { resp, reader ->
276+
fail("Expected short-circuit response to return an HTTP 402 Payment Required")
277+
}
278+
279+
response.failure = { resp, reader ->
280+
assertEquals("Expected short-circuit response to return an HTTP 402 Payment Required", 402, resp.status)
281+
assertEquals("Expected short-circuit response to contain body text set in Javascript", "You have to pay the troll toll to get into this Proxy's soul", reader.text)
282+
}
283+
}
284+
}
285+
240286
@Override
241287
String[] getArgs() {
242288
return ["--use-littleproxy", "true"]

0 commit comments

Comments
 (0)