@@ -139,6 +139,21 @@ func newTestServer() *httptest.Server {
139139 ` ))
140140 })
141141
142+ mux .HandleFunc ("/base_relative" , func (w http.ResponseWriter , r * http.Request ) {
143+ w .Header ().Set ("Content-Type" , "text/html" )
144+ w .Write ([]byte (`<!DOCTYPE html>
145+ <html>
146+ <head>
147+ <title>Test Page</title>
148+ <base href="/foobar/" />
149+ </head>
150+ <body>
151+ <a href="z">link</a>
152+ </body>
153+ </html>
154+ ` ))
155+ })
156+
142157 mux .HandleFunc ("/large_binary" , func (w http.ResponseWriter , r * http.Request ) {
143158 w .Header ().Set ("Content-Type" , "application/octet-stream" )
144159 ww := bufio .NewWriter (w )
@@ -767,6 +782,31 @@ func TestBaseTag(t *testing.T) {
767782 c2 .Visit (ts .URL + "/base" )
768783}
769784
785+ func TestBaseTagRelative (t * testing.T ) {
786+ ts := newTestServer ()
787+ defer ts .Close ()
788+
789+ c := NewCollector ()
790+ c .OnHTML ("a[href]" , func (e * HTMLElement ) {
791+ u := e .Request .AbsoluteURL (e .Attr ("href" ))
792+ expected := ts .URL + "/foobar/z"
793+ if u != expected {
794+ t .Errorf ("Invalid <base /> tag handling in OnHTML: expected %q, got %q" , expected , u )
795+ }
796+ })
797+ c .Visit (ts .URL + "/base_relative" )
798+
799+ c2 := NewCollector ()
800+ c2 .OnXML ("//a" , func (e * XMLElement ) {
801+ u := e .Request .AbsoluteURL (e .Attr ("href" ))
802+ expected := ts .URL + "/foobar/z"
803+ if u != expected {
804+ t .Errorf ("Invalid <base /> tag handling in OnXML: expected %q, got %q" , expected , u )
805+ }
806+ })
807+ c2 .Visit (ts .URL + "/base_relative" )
808+ }
809+
770810func TestCollectorCookies (t * testing.T ) {
771811 ts := newTestServer ()
772812 defer ts .Close ()
0 commit comments