Search in sources :

Example 1 with AttachmentHandler

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);
    }
}
Also used : WebResponse(com.gargoylesoftware.htmlunit.WebResponse) UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) AttachmentHandler(com.gargoylesoftware.htmlunit.attachment.AttachmentHandler) UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) Page(com.gargoylesoftware.htmlunit.Page) LinkedList(java.util.LinkedList) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Test(org.junit.Test)

Example 2 with AttachmentHandler

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);
    }
}
Also used : UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) AttachmentHandler(com.gargoylesoftware.htmlunit.attachment.AttachmentHandler) UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) Page(com.gargoylesoftware.htmlunit.Page) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 3 with AttachmentHandler

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);
    }
}
Also used : WebResponse(com.gargoylesoftware.htmlunit.WebResponse) UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) AttachmentHandler(com.gargoylesoftware.htmlunit.attachment.AttachmentHandler) UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) Page(com.gargoylesoftware.htmlunit.Page) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 4 with AttachmentHandler

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);
    }
}
Also used : WebResponse(com.gargoylesoftware.htmlunit.WebResponse) AttachmentHandler(com.gargoylesoftware.htmlunit.attachment.AttachmentHandler) UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) Page(com.gargoylesoftware.htmlunit.Page) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 5 with AttachmentHandler

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);
    }
}
Also used : WebResponse(com.gargoylesoftware.htmlunit.WebResponse) AttachmentHandler(com.gargoylesoftware.htmlunit.attachment.AttachmentHandler) UnexpectedPage(com.gargoylesoftware.htmlunit.UnexpectedPage) Page(com.gargoylesoftware.htmlunit.Page) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

Page (com.gargoylesoftware.htmlunit.Page)6 UnexpectedPage (com.gargoylesoftware.htmlunit.UnexpectedPage)6 AttachmentHandler (com.gargoylesoftware.htmlunit.attachment.AttachmentHandler)6 LinkedList (java.util.LinkedList)6 Test (org.junit.Test)6 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)4 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)2