use of com.gargoylesoftware.htmlunit.attachment.AttachmentHandler in project htmlunit by HtmlUnit.
the class HtmlAnchor2Test method click_unexpectedPageAttachmentHandlerHandleContentType.
/**
* @throws Exception if the test fails
*/
@Test
public void click_unexpectedPageAttachmentHandlerHandleContentType() throws Exception {
final String html = "<html><head></head>\n" + "<body>\n" + " <a href='" + URL_SECOND + "' id='link'>link</a>\n" + "</body>\n" + "</html>";
getMockWebConnection().setResponse(URL_SECOND, "{name: \"Test\"};", MimeType.APPLICATION_JSON);
final LinkedList<Page> pages = new LinkedList<Page>();
getWebClient().setAttachmentHandler(new AttachmentHandler() {
@Override
public boolean isAttachment(final WebResponse response) {
return MimeType.APPLICATION_JSON.equalsIgnoreCase(response.getResponseHeaderValue(HttpHeader.CONTENT_TYPE));
}
@Override
public void handleAttachment(final Page page) {
pages.add(page);
}
});
try {
final HtmlPage page = loadPage(html);
assertEquals(1, getWebClient().getWebWindows().size());
page.getElementById("link").click();
final WebWindow newWindow = getWebClient().getWebWindows().get(getWebClient().getWebWindows().size() - 1);
assertTrue(newWindow.getEnclosedPage() instanceof UnexpectedPage);
assertEquals(1, pages.size());
} finally {
getWebClient().setAttachmentHandler(null);
}
}
use of com.gargoylesoftware.htmlunit.attachment.AttachmentHandler in project htmlunit by HtmlUnit.
the class HtmlAnchor2Test method click_unexpectedPageAttachmentHandlerDoesNotHandleContentType.
/**
* @throws Exception if the test fails
*/
@Test
public void click_unexpectedPageAttachmentHandlerDoesNotHandleContentType() throws Exception {
final String html = "<html><head></head>\n" + "<body>\n" + " <a href='" + URL_SECOND + "' id='link'>link</a>\n" + "</body>\n" + "</html>";
getMockWebConnection().setResponse(URL_SECOND, "{name: \"Test\"};", MimeType.APPLICATION_JSON);
final LinkedList<Page> pages = new LinkedList<Page>();
getWebClient().setAttachmentHandler(new AttachmentHandler() {
@Override
public void handleAttachment(final Page page) {
pages.add(page);
}
});
try {
final HtmlPage page = loadPage(html);
assertEquals(1, getWebClient().getWebWindows().size());
page.getElementById("link").click();
assertEquals(1, getWebClient().getWebWindows().size());
assertTrue(getWebClient().getCurrentWindow().getEnclosedPage() instanceof UnexpectedPage);
assertEquals(0, pages.size());
} finally {
getWebClient().setAttachmentHandler(null);
}
}
use of com.gargoylesoftware.htmlunit.attachment.AttachmentHandler in project htmlunit by HtmlUnit.
the class HtmlAnchor2Test method click_unexpectedPageAttachmentHandlerHandleResponseDoesNotHandleContentType.
/**
* @throws Exception if the test fails
*/
@Test
public void click_unexpectedPageAttachmentHandlerHandleResponseDoesNotHandleContentType() throws Exception {
final String html = "<html><head></head>\n" + "<body>\n" + " <a href='" + URL_SECOND + "' id='link'>link</a>\n" + "</body>\n" + "</html>";
getMockWebConnection().setResponse(URL_SECOND, "{name: \"Test\"};", MimeType.APPLICATION_JSON);
final LinkedList<WebResponse> pages = new LinkedList<WebResponse>();
getWebClient().setAttachmentHandler(new AttachmentHandler() {
@Override
public boolean handleAttachment(final WebResponse response) {
pages.add(response);
return true;
}
@Override
public void handleAttachment(final Page page) {
throw new IllegalAccessError("handleAttachment(Page) called");
}
});
try {
final HtmlPage page = loadPage(html);
assertEquals(1, getWebClient().getWebWindows().size());
page.getElementById("link").click();
assertEquals(1, getWebClient().getWebWindows().size());
assertTrue(page.getEnclosingWindow().getEnclosedPage() instanceof UnexpectedPage);
assertEquals(0, pages.size());
} finally {
getWebClient().setAttachmentHandler(null);
}
}
use of com.gargoylesoftware.htmlunit.attachment.AttachmentHandler in project htmlunit by HtmlUnit.
the class HtmlAnchor2Test method click_unexpectedPageAttachmentHandlerHandleResponseHandlesContentType.
/**
* @throws Exception if the test fails
*/
@Test
public void click_unexpectedPageAttachmentHandlerHandleResponseHandlesContentType() throws Exception {
final String html = "<html><head></head>\n" + "<body>\n" + " <a href='" + URL_SECOND + "' id='link'>link</a>\n" + "</body>\n" + "</html>";
getMockWebConnection().setResponse(URL_SECOND, "{name: \"Test\"};", MimeType.APPLICATION_JSON);
final LinkedList<WebResponse> pages = new LinkedList<WebResponse>();
getWebClient().setAttachmentHandler(new AttachmentHandler() {
@Override
public boolean isAttachment(final WebResponse response) {
return MimeType.APPLICATION_JSON.equalsIgnoreCase(response.getResponseHeaderValue(HttpHeader.CONTENT_TYPE));
}
@Override
public boolean handleAttachment(final WebResponse response) {
pages.add(response);
return true;
}
@Override
public void handleAttachment(final Page page) {
throw new IllegalAccessError("handleAttachment(Page) called");
}
});
try {
final HtmlPage page = loadPage(html);
assertEquals(1, getWebClient().getWebWindows().size());
page.getElementById("link").click();
assertEquals(1, getWebClient().getWebWindows().size());
assertTrue(page.getEnclosingWindow().getEnclosedPage() == page);
assertEquals(1, pages.size());
} finally {
getWebClient().setAttachmentHandler(null);
}
}
use of com.gargoylesoftware.htmlunit.attachment.AttachmentHandler in project htmlunit by HtmlUnit.
the class HtmlAnchor2Test method click_unexpectedPageDownloadAttributeAttachmentHandlerHandleResponse.
/**
* @throws Exception if the test fails
*/
@Test
public void click_unexpectedPageDownloadAttributeAttachmentHandlerHandleResponse() throws Exception {
final String html = "<html><head></head>\n" + "<body>\n" + " <a href='" + URL_SECOND + "' id='link' download='test.json'>link</a>\n" + "</body>\n" + "</html>";
getMockWebConnection().setResponse(URL_SECOND, "{name: \"Test\"};", MimeType.APPLICATION_JSON);
final LinkedList<WebResponse> pages = new LinkedList<WebResponse>();
getWebClient().setAttachmentHandler(new AttachmentHandler() {
@Override
public boolean handleAttachment(final WebResponse response) {
pages.add(response);
return true;
}
@Override
public void handleAttachment(final Page page) {
throw new IllegalAccessError("handleAttachment(Page) called");
}
});
try {
final HtmlPage page = loadPage(html);
assertEquals(1, getWebClient().getWebWindows().size());
page.getElementById("link").click();
assertEquals(1, getWebClient().getWebWindows().size());
assertTrue(page.getEnclosingWindow().getEnclosedPage() == page);
assertEquals(1, pages.size());
} finally {
getWebClient().setAttachmentHandler(null);
}
}
Aggregations