@@ -66,6 +66,17 @@ func newTestServer() *httptest.Server {
6666 ` ))
6767 })
6868
69+ mux .HandleFunc ("/xml" , func (w http.ResponseWriter , r * http.Request ) {
70+ w .Header ().Set ("Content-Type" , "application/xml" )
71+ w .Write ([]byte (`<?xml version="1.0" encoding="UTF-8"?>
72+ <page>
73+ <title>Test Page</title>
74+ <paragraph type="description">This is a test page</paragraph>
75+ <paragraph type="description">This is a test paragraph</paragraph>
76+ </page>
77+ ` ))
78+ })
79+
6980 mux .HandleFunc ("/login" , func (w http.ResponseWriter , r * http.Request ) {
7081 if r .Method == "POST" {
7182 w .Header ().Set ("Content-Type" , "text/html" )
@@ -1118,7 +1129,7 @@ func TestHTMLElement(t *testing.T) {
11181129 }
11191130}
11201131
1121- func TestCollectorOnXML (t * testing.T ) {
1132+ func TestCollectorOnXMLWithHtml (t * testing.T ) {
11221133 ts := newTestServer ()
11231134 defer ts .Close ()
11241135
@@ -1162,6 +1173,50 @@ func TestCollectorOnXML(t *testing.T) {
11621173 }
11631174}
11641175
1176+ func TestCollectorOnXMLWithXML (t * testing.T ) {
1177+ ts := newTestServer ()
1178+ defer ts .Close ()
1179+
1180+ c := NewCollector ()
1181+
1182+ titleCallbackCalled := false
1183+ paragraphCallbackCount := 0
1184+
1185+ c .OnXML ("//page/title" , func (e * XMLElement ) {
1186+ titleCallbackCalled = true
1187+ if e .Text != "Test Page" {
1188+ t .Error ("Title element text does not match, got" , e .Text )
1189+ }
1190+ })
1191+
1192+ c .OnXML ("//page/paragraph" , func (e * XMLElement ) {
1193+ paragraphCallbackCount ++
1194+ if e .Attr ("type" ) != "description" {
1195+ t .Error ("Failed to get paragraph's type attribute" )
1196+ }
1197+ })
1198+
1199+ c .OnXML ("/page" , func (e * XMLElement ) {
1200+ if e .ChildAttr ("paragraph" , "type" ) != "description" {
1201+ t .Error ("Invalid type value" )
1202+ }
1203+ classes := e .ChildAttrs ("paragraph" , "type" )
1204+ if len (classes ) != 2 {
1205+ t .Error ("Invalid type values" )
1206+ }
1207+ })
1208+
1209+ c .Visit (ts .URL + "/xml" )
1210+
1211+ if ! titleCallbackCalled {
1212+ t .Error ("Failed to call OnXML callback for <title> tag" )
1213+ }
1214+
1215+ if paragraphCallbackCount != 2 {
1216+ t .Error ("Failed to find all <paragraph> tags" )
1217+ }
1218+ }
1219+
11651220func TestCollectorVisitWithTrace (t * testing.T ) {
11661221 ts := newTestServer ()
11671222 defer ts .Close ()
0 commit comments