@Test public void unspecifiedEncoding() throws Exception { String HEX_BYTES = "E29C93"; byte[] bytes = BaseEncoding.base16().decode(HEX_BYTES); String text = new String(bytes, StandardCharsets.UTF_8); System.out.format("text: %s%n", text); String url = "http: HarRequest request = BmpTests.buildHarRequest(HttpMethod.GET, url, ImmutableList.of()); MediaType contentType = MediaType.PLAIN_TEXT_UTF_8.withoutParameters(); System.out.format("content-type: %s%n", contentType); HarContent responseContent = BmpTests.buildHarContent(text, contentType); List<HarHeader> responseHeaders = BmpTests.buildHarHeaders(HttpHeaders.CONTENT_TYPE, contentType.toString()); HarResponse response = BmpTests.buildHarResponse(200, responseHeaders, responseContent); HarEntry entry = BmpTests.buildHarEntry(request, response); List<HarEntry> entries = ImmutableList.of(entry); BmpResponseManufacturer responseManufacturer = BmpTests.createManufacturer(entries, Collections.emptyList()); BrowsermobVhsConfig config = BrowsermobVhsConfig.builder(responseManufacturer) .scratchDirProvider(ScratchDirProvider.under(temporaryFolder.getRoot().toPath())) .responseListener(newLoggingResponseListener()) .build(); VirtualHarServer server = new BrowsermobVirtualHarServer(config); Multimap<URI, byte[]> responses; try (VirtualHarServerControl ctrl = server.start()) { ApacheRawClient<byte[]> client = new ApacheRawClient<byte[]>() { @Override protected byte[] transform(URI requestUrl, org.apache.http.HttpResponse response) throws IOException { return EntityUtils.toByteArray(response.getEntity()); } }; responses = client.collectResponses(Collections.singleton(URI.create(url)), ctrl.getSocketAddress()); } assertEquals("num responses", 1, responses.size()); byte[] actual = responses.values().iterator().next(); System.out.format("expecting %s, actual = %s%n", HEX_BYTES, BaseEncoding.base16().encode(actual)); assertArrayEquals("response data", bytes, actual); }
@Override public VirtualHarServerControl start() throws IOException { List<Closeable> closeables = new ArrayList<>(); Scratch scratch = config.scratchDirProvider.createScratchDir(); closeables.add(scratch); CertificateAndKeySource certificateAndKeySource; BrowserUpProxy proxy; Path scratchPath = scratch.getRoot(); try { certificateAndKeySource = config.certificateAndKeySourceFactory.produce(config, scratchPath); TlsEndpoint httpsInterceptionServer = config.tlsEndpointFactory.produce(config, scratchPath); closeables.add(httpsInterceptionServer); TrustSource trustSource = httpsInterceptionServer.getTrustSource(); proxy = startProxy(config.bmpResponseManufacturer.withFreshState(), httpsInterceptionServer.getSocketAddress(), certificateAndKeySource, trustSource); } catch (RuntimeException | IOException e) { closeAll(closeables, true); throw e; } return new BrowsermobVhsControl(proxy, closeables); }
BrowsermobVirtualHarServer implements VirtualHarServer { @Override public VirtualHarServerControl start() throws IOException { List<Closeable> closeables = new ArrayList<>(); Scratch scratch = config.scratchDirProvider.createScratchDir(); closeables.add(scratch); CertificateAndKeySource certificateAndKeySource; BrowserUpProxy proxy; Path scratchPath = scratch.getRoot(); try { certificateAndKeySource = config.certificateAndKeySourceFactory.produce(config, scratchPath); TlsEndpoint httpsInterceptionServer = config.tlsEndpointFactory.produce(config, scratchPath); closeables.add(httpsInterceptionServer); TrustSource trustSource = httpsInterceptionServer.getTrustSource(); proxy = startProxy(config.bmpResponseManufacturer.withFreshState(), httpsInterceptionServer.getSocketAddress(), certificateAndKeySource, trustSource); } catch (RuntimeException | IOException e) { closeAll(closeables, true); throw e; } return new BrowsermobVhsControl(proxy, closeables); } }
BrowsermobVirtualHarServer implements VirtualHarServer { @Override public VirtualHarServerControl start() throws IOException { List<Closeable> closeables = new ArrayList<>(); Scratch scratch = config.scratchDirProvider.createScratchDir(); closeables.add(scratch); CertificateAndKeySource certificateAndKeySource; BrowserUpProxy proxy; Path scratchPath = scratch.getRoot(); try { certificateAndKeySource = config.certificateAndKeySourceFactory.produce(config, scratchPath); TlsEndpoint httpsInterceptionServer = config.tlsEndpointFactory.produce(config, scratchPath); closeables.add(httpsInterceptionServer); TrustSource trustSource = httpsInterceptionServer.getTrustSource(); proxy = startProxy(config.bmpResponseManufacturer.withFreshState(), httpsInterceptionServer.getSocketAddress(), certificateAndKeySource, trustSource); } catch (RuntimeException | IOException e) { closeAll(closeables, true); throw e; } return new BrowsermobVhsControl(proxy, closeables); } BrowsermobVirtualHarServer(BrowsermobVhsConfig config); }
BrowsermobVirtualHarServer implements VirtualHarServer { @Override public VirtualHarServerControl start() throws IOException { List<Closeable> closeables = new ArrayList<>(); Scratch scratch = config.scratchDirProvider.createScratchDir(); closeables.add(scratch); CertificateAndKeySource certificateAndKeySource; BrowserUpProxy proxy; Path scratchPath = scratch.getRoot(); try { certificateAndKeySource = config.certificateAndKeySourceFactory.produce(config, scratchPath); TlsEndpoint httpsInterceptionServer = config.tlsEndpointFactory.produce(config, scratchPath); closeables.add(httpsInterceptionServer); TrustSource trustSource = httpsInterceptionServer.getTrustSource(); proxy = startProxy(config.bmpResponseManufacturer.withFreshState(), httpsInterceptionServer.getSocketAddress(), certificateAndKeySource, trustSource); } catch (RuntimeException | IOException e) { closeAll(closeables, true); throw e; } return new BrowsermobVhsControl(proxy, closeables); } BrowsermobVirtualHarServer(BrowsermobVhsConfig config); @Override VirtualHarServerControl start(); }
BrowsermobVirtualHarServer implements VirtualHarServer { @Override public VirtualHarServerControl start() throws IOException { List<Closeable> closeables = new ArrayList<>(); Scratch scratch = config.scratchDirProvider.createScratchDir(); closeables.add(scratch); CertificateAndKeySource certificateAndKeySource; BrowserUpProxy proxy; Path scratchPath = scratch.getRoot(); try { certificateAndKeySource = config.certificateAndKeySourceFactory.produce(config, scratchPath); TlsEndpoint httpsInterceptionServer = config.tlsEndpointFactory.produce(config, scratchPath); closeables.add(httpsInterceptionServer); TrustSource trustSource = httpsInterceptionServer.getTrustSource(); proxy = startProxy(config.bmpResponseManufacturer.withFreshState(), httpsInterceptionServer.getSocketAddress(), certificateAndKeySource, trustSource); } catch (RuntimeException | IOException e) { closeAll(closeables, true); throw e; } return new BrowsermobVhsControl(proxy, closeables); } BrowsermobVirtualHarServer(BrowsermobVhsConfig config); @Override VirtualHarServerControl start();  }