Skip to content

Commit

Permalink
added pageTitle parameter to PUT /proxy/[port]/har/pageRef API
Browse files Browse the repository at this point in the history
  • Loading branch information
bhdrk committed Mar 4, 2015
1 parent f5304fb commit c0ec9a1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -50,11 +50,13 @@ Once that is done, a new proxy will be available on the port returned. All you h
- GET /proxy - get a list of ports attached to `ProxyServer` instances managed by `ProxyManager`
- 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:
- initialPageRef - the string name of the first page ref that should be used in the HAR. Defaults to "Page 1".
- initialPageTitle - the title of first HAR page. Defaults to initialPageRef.
- captureHeaders - Boolean, capture headers
- captureContent - Boolean, capture content bodies
- captureBinaryContent - Boolean, capture binary content
- PUT /proxy/[port]/har/pageRef - starts a new page on the existing HAR. Supports the following parameters:
- 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.
- pageTitle - the title of new har page. Defaults to pageRef.
- DELETE /proxy/[port] - shuts down the proxy and closes the port
- GET /proxy/[port]/har - returns the JSON/HAR content representing all the HTTP traffic passed through the proxy
- GET /proxy/[port]/whitelist - Displays whitelisted items
Expand Down
Expand Up @@ -18,7 +18,12 @@ public HarPage() {
}

public HarPage(String id) {
this(id, "");
}

public HarPage(String id, String title) {
this.id = id;
this.title = title;
startedDateTime = new Date();
}

Expand Down
Expand Up @@ -233,24 +233,36 @@ public boolean checkCondition() {
}

public Har newHar(String initialPageRef) {
return newHar(initialPageRef, null);
}

public Har newHar(String initialPageRef, String initialPageTitle) {
pageCount.set(0); // this will be automatically incremented by newPage() below

Har oldHar = getHar();

Har har = new Har(new HarLog(CREATOR));
client.setHar(har);
newPage(initialPageRef);
newPage(initialPageRef, initialPageTitle);

return oldHar;
}

public void newPage(String pageRef) {
newPage(pageRef, null);
}

public void newPage(String pageRef, String pageTitle) {
if (pageRef == null) {
pageRef = "Page " + pageCount.get();
}

if (pageTitle == null) {
pageTitle = pageRef;
}

client.setHarPageRef(pageRef);
currentPage = new HarPage(pageRef);
currentPage = new HarPage(pageRef, pageTitle);
client.getHar().getLog().addPage(currentPage);

pageCount.incrementAndGet();
Expand Down
33 changes: 33 additions & 0 deletions browsermob-core/src/test/java/net/lightbody/bmp/proxy/HarTest.java
Expand Up @@ -391,6 +391,7 @@ public void testHarPagesPopulated() throws IOException {

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

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

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

@Test
public void testHarPageTitlePopulated() throws Exception {
proxy.newHar("testpage1", "Test Page 1");

HttpGet get = new HttpGet(getLocalServerHostnameAndPort() + "/a.txt");
IOUtils.toStringAndClose(client.execute(get).getEntity().getContent());

proxy.endPage();

proxy.newPage("testpage2", "Test Page 2");

IOUtils.toStringAndClose(client.execute(get).getEntity().getContent());
IOUtils.toStringAndClose(client.execute(get).getEntity().getContent());

proxy.endPage();

Har har = proxy.getHar();
Assert.assertNotNull("Har is null", har);
HarLog log = har.getLog();
Assert.assertNotNull("Log is null", log);

Assert.assertNotNull("har pages are null", log.getPages());
Assert.assertEquals("expected 2 har pages", 2, log.getPages().size());

HarPage page1 = log.getPages().get(0);
Assert.assertEquals("incorrect har page title", "Test Page 1", page1.getTitle());

HarPage page2 = log.getPages().get(1);
Assert.assertEquals("incorrect har page title", "Test Page 2", page2.getTitle());
}

@Test
public void testEntryFieldsPopulatedForHttp() throws IOException {
proxy.newHar("testEntryFieldsPopulatedForHttp");
Expand Down
Expand Up @@ -116,7 +116,8 @@ public Reply<?> newHar(@Named("port") int port, Request<String> request) {
}

String initialPageRef = request.param("initialPageRef");
Har oldHar = proxy.newHar(initialPageRef);
String initialPageTitle = request.param("initialPageTitle");
Har oldHar = proxy.newHar(initialPageRef, initialPageTitle);

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

String pageRef = request.param("pageRef");
proxy.newPage(pageRef);
String pageTitle = request.param("pageTitle");
proxy.newPage(pageRef, pageTitle);

return Reply.saying().ok();
}
Expand Down

0 comments on commit c0ec9a1

Please sign in to comment.