Skip to content

Commit a97819b

Browse files
committed
work: XHR ResponseURL working
Reflects the URL of the request cauring a redirect.
1 parent ca495cd commit a97819b

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

internal/html/xml_http_request.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (req *xmlHttpRequest) Status() int { return req.status }
159159
// be in JS wrapper layer
160160
func (req *xmlHttpRequest) StatusText() string { return http.StatusText(req.status) }
161161

162-
func (req *xmlHttpRequest) ResponseURL() string { return req.url }
162+
func (req *xmlHttpRequest) ResponseURL() string { return req.res.Request.URL.String() }
163163

164164
func (req *xmlHttpRequest) Response() string { return req.ResponseText() }
165165

internal/html/xml_http_request_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
. "github.com/gost-dom/browser/internal/html"
1616
. "github.com/gost-dom/browser/internal/testing/gomega-matchers"
1717
"github.com/gost-dom/browser/internal/testing/gosttest"
18+
"github.com/stretchr/testify/assert"
1819
"github.com/stretchr/testify/suite"
1920

2021
"github.com/onsi/gomega/types"
@@ -218,6 +219,23 @@ func (s *XMLHTTPRequestTestSuite) TestCookieVisibility() {
218219
).To(HaveLines("x-test-1: value1", "x-test-2: value2", "content-type: text/plain"))
219220
}
220221

222+
func TestXMLHTTPRequestRedirect(t *testing.T) {
223+
m := http.NewServeMux()
224+
m.Handle("GET /redirect", http.RedirectHandler("/redirect-temp", 301))
225+
m.Handle("GET /redirect-temp", http.RedirectHandler("/redirected-url", 301))
226+
m.HandleFunc("GET /redirected-url", func(w http.ResponseWriter, r *http.Request) {
227+
w.Write([]byte("Handled"))
228+
})
229+
xhr := NewXmlHttpRequest(
230+
stubBrowsingContext{client: gosthttp.NewHttpClientFromHandler(m)},
231+
clock.New(),
232+
)
233+
xhr.Open("GET", "https://example.com/redirect", RequestOptionAsync(false))
234+
xhr.Send()
235+
236+
assert.Equal(t, "https://example.com/redirected-url", xhr.ResponseURL())
237+
}
238+
221239
func HaveLines(expected ...string) types.GomegaMatcher {
222240
return WithTransform(func(s string) []string {
223241
lines := strings.Split(s, "\r\n")

0 commit comments

Comments
 (0)