1 38 package com.gargoylesoftware.htmlunit.xml; 39 40 import com.gargoylesoftware.htmlunit.MockWebConnection; 41 import com.gargoylesoftware.htmlunit.Page; 42 import com.gargoylesoftware.htmlunit.WebClient; 43 import com.gargoylesoftware.htmlunit.WebTestCase; 44 45 51 public class XmlPageTest extends WebTestCase { 52 56 public XmlPageTest( final String name ) { 57 super(name); 58 } 59 60 61 65 public void testValidDocument() throws Exception { 66 final WebClient client = new WebClient(); 67 final MockWebConnection webConnection = new MockWebConnection( client ); 68 69 final String content 70 = "<?xml version=\"1.0\"?>" 71 + "<foo>\n" 72 + " <foofoo name='first'>something</foofoo>\n" 73 + " <foofoo name='second'>something else</foofoo>\n" 74 + "</foo>"; 75 76 webConnection.setDefaultResponse(content, 200, "OK", "text/xml"); 77 client.setWebConnection( webConnection ); 78 79 final Page page = client.getPage(URL_FIRST); 80 assertEquals(URL_FIRST.toExternalForm(), page.getWebResponse().getUrl().toExternalForm()); 81 assertEquals("OK", page.getWebResponse().getStatusMessage()); 82 assertEquals(200, page.getWebResponse().getStatusCode()); 83 assertEquals("text/xml", page.getWebResponse().getContentType()); 84 85 assertInstanceOf(page, XmlPage.class); 86 final XmlPage xmlPage = (XmlPage) page; 87 assertEquals(content, xmlPage.getContent()); 88 assertNotNull(xmlPage.getXmlDocument()); 89 assertEquals("foo", xmlPage.getXmlDocument().getFirstChild().getNodeName()); 90 } 91 92 93 97 public void testInvalidDocument() throws Exception { 98 final WebClient client = new WebClient(); 99 final MockWebConnection webConnection = new MockWebConnection( client ); 100 101 final String content 102 = "<?xml version=\"1.0\"?>" 103 + "<foo>\n" 104 + " <foofoo invalid\n" 105 + " <foofoo name='first'>something</foofoo>\n" 106 + " <foofoo name='second'>something else</foofoo>\n" 107 + "</foo>"; 108 109 webConnection.setDefaultResponse(content, 200, "OK", "text/xml"); 110 client.setWebConnection( webConnection ); 111 112 final Page page = client.getPage(URL_FIRST); 113 assertEquals(URL_FIRST.toExternalForm(), page.getWebResponse().getUrl().toExternalForm()); 114 assertEquals("OK", page.getWebResponse().getStatusMessage()); 115 assertEquals(200, page.getWebResponse().getStatusCode()); 116 assertEquals("text/xml", page.getWebResponse().getContentType()); 117 118 assertInstanceOf(page, XmlPage.class); 119 final XmlPage xmlPage = (XmlPage) page; 120 assertEquals(content, xmlPage.getContent()); 121 assertNull(xmlPage.getXmlDocument()); 122 } 123 } 124 | Popular Tags |