Skip to content

Commit 6e878d0

Browse files
committed
Add passing headers example
1 parent 7198af6 commit 6e878d0

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.github.kklisura.cdt.examples;
2+
3+
import com.github.kklisura.cdt.launch.ChromeLauncher;
4+
import com.github.kklisura.cdt.protocol.commands.Network;
5+
import com.github.kklisura.cdt.protocol.commands.Page;
6+
import com.github.kklisura.cdt.protocol.types.network.RequestPattern;
7+
import com.github.kklisura.cdt.services.ChromeDevToolsService;
8+
import com.github.kklisura.cdt.services.ChromeService;
9+
import com.github.kklisura.cdt.services.types.ChromeTab;
10+
import java.util.Collections;
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
/**
15+
* This is an example on how to pass header to a intercepted requests. A 'Custom-Header' with
16+
* 'Value' is passed on every request.
17+
*
18+
* @author Kenan Klisura
19+
*/
20+
public class PassingCustomHeadersToRequests {
21+
public static void main(String[] args) {
22+
// Create chrome launcher.
23+
final ChromeLauncher launcher = new ChromeLauncher();
24+
25+
// Launch chrome either as headless (true) or regular (false).
26+
final ChromeService chromeService = launcher.launch(false);
27+
28+
// Create empty tab ie about:blank.
29+
final ChromeTab tab = chromeService.createTab();
30+
31+
// Get DevTools service to this tab
32+
final ChromeDevToolsService devToolsService = chromeService.createDevToolsService(tab);
33+
34+
// Get individual commands
35+
final Page page = devToolsService.getPage();
36+
final Network network = devToolsService.getNetwork();
37+
38+
network.onRequestIntercepted(
39+
event -> {
40+
String interceptionId = event.getInterceptionId();
41+
42+
System.out.printf(
43+
"Adding header to %s request%s", event.getRequest().getUrl(), System.lineSeparator());
44+
45+
Map<String, Object> headers = new HashMap<>();
46+
headers.put("Custom-Header", "Value");
47+
48+
network.continueInterceptedRequest(
49+
interceptionId, null, null, null, null, null, headers, null);
50+
});
51+
52+
RequestPattern interceptionRequestPattern = new RequestPattern();
53+
network.setRequestInterception(Collections.singletonList(interceptionRequestPattern));
54+
network.enable();
55+
56+
// Enable page events.
57+
page.enable();
58+
59+
// Navigate to github.com.
60+
page.navigate("http://github.com");
61+
62+
devToolsService.waitUntilClosed();
63+
}
64+
}

0 commit comments

Comments
 (0)