MCPcopy Index your code
hub / github.com/HtmlUnit/htmlunit / loadFilePage

Method loadFilePage

src/test/java/org/htmlunit/WebClientTest.java:747–790  ·  view source on GitHub ↗

Test loading a file page. @throws Exception if something goes wrong

()

Source from the content-addressed store, hash-verified

745 * @throws Exception if something goes wrong
746 */
747 @Test
748 public void loadFilePage() throws Exception {
749 // Create a real file to read.
750 // It could be useful to have existing files to test in a special location in filesystem.
751 // It will be really needed when we have to test binary files using the file protocol.
752
753 final String htmlContent = DOCTYPE_HTML + "<html><head><title>foo</title></head><body></body></html>";
754 final File currentDirectory = new File((new File("")).getAbsolutePath());
755 final File tmpFile = File.createTempFile("test", ".html", currentDirectory);
756 tmpFile.deleteOnExit();
757 final String encoding = (new OutputStreamWriter(new ByteArrayOutputStream())).getEncoding();
758 FileUtils.writeStringToFile(tmpFile, htmlContent, encoding);
759
760 // Test a normal file URL.
761
762 final WebClient client = getWebClient();
763 final URL url = new URL("file://" + tmpFile.getCanonicalPath());
764 final HtmlPage page = client.getPage(url);
765
766 assertEquals(htmlContent, page.getWebResponse().getContentAsString());
767 assertEquals(MimeType.TEXT_HTML, page.getWebResponse().getContentType());
768 assertEquals(200, page.getWebResponse().getStatusCode());
769 assertEquals("foo", page.getTitleText());
770
771 // Test a file URL with a query portion (needs to work for Dojo, for example).
772
773 final URL url2 = new URL(url + "?with=query");
774 final HtmlPage page2 = client.getPage(url2);
775
776 assertEquals(htmlContent, page2.getWebResponse().getContentAsString());
777 assertEquals(MimeType.TEXT_HTML, page2.getWebResponse().getContentType());
778 assertEquals(200, page2.getWebResponse().getStatusCode());
779 assertEquals("foo", page2.getTitleText());
780
781 // Test a file URL with a ref portion (needs to work for Dojo, for example).
782
783 final URL url3 = new URL(url + "#reference");
784 final HtmlPage page3 = client.getPage(url3);
785
786 assertEquals(htmlContent, page3.getWebResponse().getContentAsString());
787 assertEquals(MimeType.TEXT_HTML, page3.getWebResponse().getContentType());
788 assertEquals(200, page3.getWebResponse().getStatusCode());
789 assertEquals("foo", page3.getTitleText());
790 }
791
792 /**
793 * Test loading a file page with non ascii names.

Callers

nothing calls this directly

Calls 9

getPageMethod · 0.95
getTitleTextMethod · 0.95
getWebClientMethod · 0.65
getWebResponseMethod · 0.65
getEncodingMethod · 0.45
assertEqualsMethod · 0.45
getContentAsStringMethod · 0.45
getContentTypeMethod · 0.45
getStatusCodeMethod · 0.45

Tested by

no test coverage detected