Skip to content

Commit c0ec9a1

Browse files
committed
added pageTitle parameter to PUT /proxy/[port]/har/pageRef API
1 parent f5304fb commit c0ec9a1

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ Once that is done, a new proxy will be available on the port returned. All you h
5050
- GET /proxy - get a list of ports attached to `ProxyServer` instances managed by `ProxyManager`
5151
- PUT /proxy/[port]/har - creates a new HAR attached to the proxy and returns the HAR content if there was a previous HAR. Supports the following parameters:
5252
- initialPageRef - the string name of the first page ref that should be used in the HAR. Defaults to "Page 1".
53+
- initialPageTitle - the title of first HAR page. Defaults to initialPageRef.
5354
- captureHeaders - Boolean, capture headers
5455
- captureContent - Boolean, capture content bodies
5556
- captureBinaryContent - Boolean, capture binary content
5657
- PUT /proxy/[port]/har/pageRef - starts a new page on the existing HAR. Supports the following parameters:
5758
- pageRef - the string name of the first page ref that should be used in the HAR. Defaults to "Page N" where N is the next page number.
59+
- pageTitle - the title of new har page. Defaults to pageRef.
5860
- DELETE /proxy/[port] - shuts down the proxy and closes the port
5961
- GET /proxy/[port]/har - returns the JSON/HAR content representing all the HTTP traffic passed through the proxy
6062
- GET /proxy/[port]/whitelist - Displays whitelisted items

browsermob-core/src/main/java/net/lightbody/bmp/core/har/HarPage.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ public HarPage() {
1818
}
1919

2020
public HarPage(String id) {
21+
this(id, "");
22+
}
23+
24+
public HarPage(String id, String title) {
2125
this.id = id;
26+
this.title = title;
2227
startedDateTime = new Date();
2328
}
2429

browsermob-core/src/main/java/net/lightbody/bmp/proxy/ProxyServer.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,24 +233,36 @@ public boolean checkCondition() {
233233
}
234234

235235
public Har newHar(String initialPageRef) {
236+
return newHar(initialPageRef, null);
237+
}
238+
239+
public Har newHar(String initialPageRef, String initialPageTitle) {
236240
pageCount.set(0); // this will be automatically incremented by newPage() below
237241

238242
Har oldHar = getHar();
239243

240244
Har har = new Har(new HarLog(CREATOR));
241245
client.setHar(har);
242-
newPage(initialPageRef);
246+
newPage(initialPageRef, initialPageTitle);
243247

244248
return oldHar;
245249
}
246250

247251
public void newPage(String pageRef) {
252+
newPage(pageRef, null);
253+
}
254+
255+
public void newPage(String pageRef, String pageTitle) {
248256
if (pageRef == null) {
249257
pageRef = "Page " + pageCount.get();
250258
}
251259

260+
if (pageTitle == null) {
261+
pageTitle = pageRef;
262+
}
263+
252264
client.setHarPageRef(pageRef);
253-
currentPage = new HarPage(pageRef);
265+
currentPage = new HarPage(pageRef, pageTitle);
254266
client.getHar().getLog().addPage(currentPage);
255267

256268
pageCount.incrementAndGet();

browsermob-core/src/test/java/net/lightbody/bmp/proxy/HarTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ public void testHarPagesPopulated() throws IOException {
391391

392392
HarPage page1 = log.getPages().get(0);
393393
Assert.assertEquals("incorrect har page id", "testpage1", page1.getId());
394+
Assert.assertEquals("incorrect har page title", page1.getId(), page1.getTitle());
394395
Assert.assertNotNull("har page timings are null", page1.getPageTimings());
395396

396397
HarPageTimings timings1 = page1.getPageTimings();
@@ -399,12 +400,44 @@ public void testHarPagesPopulated() throws IOException {
399400

400401
HarPage page2 = log.getPages().get(1);
401402
Assert.assertEquals("incorrect har page id", "testpage2", page2.getId());
403+
Assert.assertEquals("incorrect har page id", page2.getId(), page2.getTitle());
402404
Assert.assertNotNull("har page timings are null", page2.getPageTimings());
403405
HarPageTimings timings2 = page2.getPageTimings();
404406
Assert.assertNotNull("har page onLoad timing is null", timings2.getOnLoad());
405407
Assert.assertNotEquals("har page onLoad timing should be greater than 0", timings2.getOnLoad().longValue(), 0L);
406408
}
407409

410+
@Test
411+
public void testHarPageTitlePopulated() throws Exception {
412+
proxy.newHar("testpage1", "Test Page 1");
413+
414+
HttpGet get = new HttpGet(getLocalServerHostnameAndPort() + "/a.txt");
415+
IOUtils.toStringAndClose(client.execute(get).getEntity().getContent());
416+
417+
proxy.endPage();
418+
419+
proxy.newPage("testpage2", "Test Page 2");
420+
421+
IOUtils.toStringAndClose(client.execute(get).getEntity().getContent());
422+
IOUtils.toStringAndClose(client.execute(get).getEntity().getContent());
423+
424+
proxy.endPage();
425+
426+
Har har = proxy.getHar();
427+
Assert.assertNotNull("Har is null", har);
428+
HarLog log = har.getLog();
429+
Assert.assertNotNull("Log is null", log);
430+
431+
Assert.assertNotNull("har pages are null", log.getPages());
432+
Assert.assertEquals("expected 2 har pages", 2, log.getPages().size());
433+
434+
HarPage page1 = log.getPages().get(0);
435+
Assert.assertEquals("incorrect har page title", "Test Page 1", page1.getTitle());
436+
437+
HarPage page2 = log.getPages().get(1);
438+
Assert.assertEquals("incorrect har page title", "Test Page 2", page2.getTitle());
439+
}
440+
408441
@Test
409442
public void testEntryFieldsPopulatedForHttp() throws IOException {
410443
proxy.newHar("testEntryFieldsPopulatedForHttp");

browsermob-rest/src/main/java/net/lightbody/bmp/proxy/bricks/ProxyResource.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ public Reply<?> newHar(@Named("port") int port, Request<String> request) {
116116
}
117117

118118
String initialPageRef = request.param("initialPageRef");
119-
Har oldHar = proxy.newHar(initialPageRef);
119+
String initialPageTitle = request.param("initialPageTitle");
120+
Har oldHar = proxy.newHar(initialPageRef, initialPageTitle);
120121

121122
String captureHeaders = request.param("captureHeaders");
122123
String captureContent = request.param("captureContent");
@@ -141,7 +142,8 @@ public Reply<?> setPage(@Named("port") int port, Request<String> request) {
141142
}
142143

143144
String pageRef = request.param("pageRef");
144-
proxy.newPage(pageRef);
145+
String pageTitle = request.param("pageTitle");
146+
proxy.newPage(pageRef, pageTitle);
145147

146148
return Reply.saying().ok();
147149
}

0 commit comments

Comments
 (0)