net.lightbody.bmp.filters.RequestFilter Java Examples

The following examples show how to use net.lightbody.bmp.filters.RequestFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultModule.java    From bromium with MIT License 6 votes vote down vote up
@CheckedProvides(IOURIProvider.class)
public ProxyDriverIntegrator getProxyDriverIntegrator(IOProvider<RequestFilter> requestFilterIOProvider,
                                                      IOProvider<ResponseFilter> responseFilterIOURIProvider,
                                                      WebDriverSupplier webDriverSupplier,
                                                      DriverServiceSupplier driverServiceSupplier,
                                                      @Named(PATH_TO_DRIVER) String pathToDriverExecutable,
                                                      @Named(SCREEN) String screen,
                                                      @Named(TIMEOUT) int timeout) throws IOException, URISyntaxException {
    return getProxyDriverIntegrator(requestFilterIOProvider.get(),
            webDriverSupplier,
            driverServiceSupplier,
            pathToDriverExecutable,
            screen,
            timeout,
            responseFilterIOURIProvider.get());
}
 
Example #2
Source File: DefaultModule.java    From bromium with MIT License 6 votes vote down vote up
private ProxyDriverIntegrator getProxyDriverIntegrator(RequestFilter recordRequestFilter,
                                                       WebDriverSupplier webDriverSupplier,
                                                       DriverServiceSupplier driverServiceSupplier,
                                                       @Named(PATH_TO_DRIVER) String pathToDriverExecutable,
                                                       @Named(SCREEN) String screen,
                                                       @Named(TIMEOUT) int timeout,
                                                       ResponseFilter responseFilter) throws IOException {
    BrowserMobProxy proxy = createBrowserMobProxy(timeout, recordRequestFilter, responseFilter);
    proxy.start(0);
    logger.info("Proxy running on port " + proxy.getPort());
    Proxy seleniumProxy = createSeleniumProxy(proxy);
    DesiredCapabilities desiredCapabilities = createDesiredCapabilities(seleniumProxy);
    DriverService driverService = driverServiceSupplier.getDriverService(pathToDriverExecutable, screen);
    WebDriver driver = webDriverSupplier.get(driverService, desiredCapabilities);

    return new ProxyDriverIntegrator(driver, proxy, driverService);
}
 
Example #3
Source File: DefaultModuleTest.java    From bromium with MIT License 6 votes vote down vote up
@Test
public void ifCommmandIsInvalidExceptionIsThrown() throws IOException {
    String command = "invalid";
    Map<String, Object> opts = new HashMap<>();
    opts.put(BROWSER, CHROME);
    opts.put(DRIVER, chromedriverFile.getAbsolutePath());
    opts.put(APPLICATION, configurationFile.getAbsolutePath());
    opts.put(URL, localhostUrl);
    opts.put(CASE, caseFile.getAbsolutePath());
    opts.put(SCREEN, screenString);
    opts.put(TIMEOUT, timeoutString);
    opts.put(PRECISION, precisionString);
    Module module = new DefaultModule(command, opts);
    Injector injector = Guice.createInjector(module);

    try {
        RequestFilter instance = injector.getInstance(
                Key.get(new TypeLiteral<IOProvider<RequestFilter>>() {})).get();
    } catch (ProvisionException e) {
        assertTrue(e.getCause() instanceof NoSuchCommandException);
    }
}
 
Example #4
Source File: DefaultModule.java    From bromium with MIT License 5 votes vote down vote up
@CheckedProvides(IOProvider.class)
public RequestFilter getRequestFilter(@Named(COMMAND) String command,
                                      IOProvider<List<EventDetector>> eventDetectorListProvider,
                                      Provider<RecordingState> recordingStateProvider,
                                      Provider<ReplayingState> replayingStateProvider,
                                      Provider<List<ConditionsUpdater>> conditionsUpdaters) throws IOException {
    switch (command) {
        case RECORD:
            return new RecordRequestFilter(recordingStateProvider.get(), eventDetectorListProvider.get());
        case REPLAY:
            return new ReplayRequestFilter(replayingStateProvider.get(), conditionsUpdaters.get());
        default:
            throw new NoSuchCommandException();
    }
}
 
Example #5
Source File: DefaultModule.java    From bromium with MIT License 5 votes vote down vote up
public BrowserMobProxy createBrowserMobProxy(int timeout, RequestFilter requestFilter, ResponseFilter responseFilter) {
    BrowserMobProxyServer proxy = new BrowserMobProxyServer();
    proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
    proxy.newHar("measurements");
    proxy.addRequestFilter(requestFilter);
    proxy.addResponseFilter(responseFilter);
    proxy.setIdleConnectionTimeout(timeout, TimeUnit.SECONDS);
    proxy.setRequestTimeout(timeout, TimeUnit.SECONDS);
    return proxy;
}
 
Example #6
Source File: RecordingSimulatorModule.java    From bromium with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public RecordingSimulatorModule(Injector originalInjector) throws IOException {
    this.webDriverSupplier = spy(originalInjector.getInstance(WebDriverSupplier.class));
    this.recordingState = spy(originalInjector.getInstance(RecordingState.class));
    this.promptUtils = spy(originalInjector.getInstance(PromptUtils.class));
    this.recordRequestFilter = spy(originalInjector.getInstance(
            Key.get(new TypeLiteral<IOProvider<RequestFilter>>() {})).get());

    doAnswer(invocationOnMock -> {
        Object driver = invocationOnMock.callRealMethod();
        setWebDriver((WebDriver) driver);
        return driver;
    }).when(webDriverSupplier).get(any(), any());
}
 
Example #7
Source File: RecordingSimulatorModule.java    From bromium with MIT License 5 votes vote down vote up
@Override
protected void configure() {
    bind(WebDriverSupplier.class).toProvider(() -> webDriverSupplier);
    bind(PromptUtils.class).toProvider(() -> promptUtils);
    bind(RecordingState.class).toProvider(() -> recordingState);
    bind(RequestFilter.class).toProvider(() -> recordRequestFilter);
}
 
Example #8
Source File: RecordThroughTheRecordRequestFilterIT.java    From bromium with MIT License 5 votes vote down vote up
@Override
public void run(RecordingSimulatorModule recordingSimulatorModule) {
    RequestFilter recordRequestFilter = recordingSimulatorModule.getRecordRequestFilter();
    HttpMessageContents httpMessageContents = mock(HttpMessageContents.class);
    HttpMessageInfo httpMessageInfo = mock(HttpMessageInfo.class);

    for (Map<String, String> testCaseStep: expected) {
        String queryString = URLUtils.toQueryString(testCaseStep);
        HttpRequest httpRequest = mock(HttpRequest.class, RETURNS_DEEP_STUBS);
        when(httpRequest.getUri()).thenReturn(SUBMIT_EVENT_URL + queryString);
        when(httpRequest.getMethod()).thenReturn(HttpMethod.GET);
        when(httpRequest.headers().get(ACCEPT)).thenReturn(HTML);
        recordRequestFilter.filterRequest(httpRequest, httpMessageContents, httpMessageInfo);
    }
}
 
Example #9
Source File: RequestFilterRegistry.java    From bobcat with Apache License 2.0 5 votes vote down vote up
@Override
public HttpResponse filterRequest(
    HttpRequest httpRequest, HttpMessageContents httpMessageContents, HttpMessageInfo httpMessageInfo) {
  for (RequestFilter requestFilter : filters) {
    HttpResponse response = requestFilter.filterRequest(httpRequest, httpMessageContents, httpMessageInfo);
    if (response != null) {
      return response;
    }
  }
  return null;
}
 
Example #10
Source File: RecordingSimulatorModule.java    From bromium with MIT License 4 votes vote down vote up
public RequestFilter getRecordRequestFilter() {
    return recordRequestFilter;
}
 
Example #11
Source File: ProxyStepDefinitions.java    From IridiumApplicationTesting with MIT License 4 votes vote down vote up
/**
 * Apps like life express will often include AWSELB cookies from both the root "/" context and the
 * application "/life-express" context. Supplying both cookies means that requests are sent to a EC2
 * instance that didn't generate the initial session, and so the request fails. This step allows us to
 * remove these duplicated cookies from the request.
 *
 * @param url The regex that matches URLs that should have duplicate AWSELB cookies removed
 */
@When("^I (?:remove|delete) root AWSELB cookie from the request to the URL regex \"(.*?)\"$")
public void stripHeaders(final String url) {
	final Optional<ProxyDetails<?>> proxy =
		State.getFeatureStateForThread().getProxyInterface(BrowsermobProxyUtilsImpl.PROXY_NAME);

	/*
		Get the name of the thread running the test
	 */
	final String threadName = Thread.currentThread().getName();


	proxy
		.flatMap(ProxyDetails::getInterface)
		.map(BrowserMobProxy.class::cast)
		.ifPresent(x -> {
			x.addRequestFilter(new RequestFilter() {
				@Override
				public HttpResponse filterRequest(
					final HttpRequest request,
					final HttpMessageContents contents,
					final HttpMessageInfo messageInfo) {
					if (messageInfo.getOriginalRequest().getUri().matches(url)) {
						final Optional<String> cookies =
							Optional.ofNullable(request.headers().get("Cookie"));

					/*
						Only proceed if we have supplied some cookies
					 */
						if (cookies.isPresent()) {
						/*
							Find the root context cookie
						 */
							final WebDriver webDriver =
								State
									.getThreadDesiredCapabilityMap()
									.getWebDriverForThread(threadName, true);

							final Optional<Cookie> awselb =
								webDriver.manage().getCookies()
									.stream()
									.filter(x -> "AWSELB".equals(x.getName()))
									.filter(x -> "/".equals(x.getPath()))
									.findFirst();

						/*
							If we have a root context cookie,
							remove it from the request
						 */
							if (awselb.isPresent()) {

								LOGGER.info(
									"WEBAPPTESTER-INFO-0002: "
										+ "Removing AWSELB cookie with value {}",
									awselb.get().getValue());

								final String newCookie =
									cookies.get().replaceAll(awselb.get().getName()
											+ "="
											+ awselb.get().getValue() + ";"
											+ "( "
											+ "GMT=; "
											+ "\\d+-\\w+-\\d+=\\d+:\\d+:\\d+;"
											+ ")?",
										"");

								request.headers().set("Cookie", newCookie);
							}

							final int awsElbCookieCount = StringUtils.countMatches(
								request.headers().get("Cookie"),
								"AWSELB");

							if (awsElbCookieCount != 1) {
								LOGGER.info(
									"WEBAPPTESTER-INFO-0003: "
										+ "{} AWSELB cookies found",
									awsElbCookieCount);
							}
						}

					}

					return null;
				}
			});
		});
}
 
Example #12
Source File: BrowserMobProxy.java    From AndroidHttpCapture with MIT License 2 votes vote down vote up
/**
 * Adds a new RequestFilter that can be used to examine and manipulate the request before sending it to the server.
 *
 * @param filter filter instance
 */
void addRequestFilter(RequestFilter filter);
 
Example #13
Source File: BrowserMobProxyServer.java    From CapturePacket with MIT License 2 votes vote down vote up
/**
 * <b>Note:</b> The current implementation of this method forces a maximum request size of 2 MiB. To adjust the maximum request size, or
 * to disable aggregation (which disallows access to the {@link net.lightbody.bmp.util.HttpMessageContents}), you may add the filter source
 * directly: <code>addFirstHttpFilterFactory(new RequestFilterAdapter.FilterSource(filter, bufferSizeInBytes));</code>
 */
@Override
public void addRequestFilter(RequestFilter filter) {
    addFirstHttpFilterFactory(new RequestFilterAdapter.FilterSource(filter));
}
 
Example #14
Source File: BrowserMobProxyServer.java    From AndroidHttpCapture with MIT License 2 votes vote down vote up
/**
 * <b>Note:</b> The current implementation of this method forces a maximum request size of 2 MiB. To adjust the maximum request size, or
 * to disable aggregation (which disallows access to the {@link net.lightbody.bmp.util.HttpMessageContents}), you may add the filter source
 * directly: <code>addFirstHttpFilterFactory(new RequestFilterAdapter.FilterSource(filter, bufferSizeInBytes));</code>
 */
@Override
public void addRequestFilter(RequestFilter filter) {
    addFirstHttpFilterFactory(new RequestFilterAdapter.FilterSource(filter));
}
 
Example #15
Source File: BrowserMobProxy.java    From Dream-Catcher with MIT License 2 votes vote down vote up
/**
 * Adds a new RequestFilter that can be used to examine and manipulate the request before sending it to the server.
 *
 * @param filter filter instance
 */
void addRequestFilter(RequestFilter filter);
 
Example #16
Source File: BrowserMobProxyServer.java    From Dream-Catcher with MIT License 2 votes vote down vote up
/**
 * <b>Note:</b> The current implementation of this method forces a maximum request size of 2 MiB. To adjust the maximum request size, or
 * to disable aggregation (which disallows access to the {@link net.lightbody.bmp.util.HttpMessageContents}), you may add the filter source
 * directly: <code>addFirstHttpFilterFactory(new RequestFilterAdapter.FilterSource(filter, bufferSizeInBytes));</code>
 */
@Override
public void addRequestFilter(RequestFilter filter) {
    addFirstHttpFilterFactory(new RequestFilterAdapter.FilterSource(filter));
}
 
Example #17
Source File: RequestFilterRegistry.java    From bobcat with Apache License 2.0 2 votes vote down vote up
/**
 * Unregister filter (it won't get anymore events)
 *
 * @param filter filter to be removed to the Set of filters.
 */
public void remove(RequestFilter filter) {
  filters.remove(filter);
}
 
Example #18
Source File: RequestFilterRegistry.java    From bobcat with Apache License 2.0 2 votes vote down vote up
/**
 * Register new filter
 *
 * @param filter filter to be added to the Set of filters.
 */
public void add(RequestFilter filter) {
  filters.add(filter);
}
 
Example #19
Source File: BrowserMobProxy.java    From CapturePacket with MIT License 2 votes vote down vote up
/**
 * Adds a new RequestFilter that can be used to examine and manipulate the request before sending it to the server.
 *
 * @param filter filter instance
 */
void addRequestFilter(RequestFilter filter);