Skip to content

Commit ca495cd

Browse files
committed
work: Handle too many redirects
This was implicitly handled before, but now we define a specific error type; as well as verify the concrete error type.
1 parent a5b947b commit ca495cd

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

html/window.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/gost-dom/browser/url"
1717
)
1818

19+
var ErrTooManyRedirects = errors.New("Too many redirects")
20+
1921
type ScriptHost interface {
2022
NewContext(window Window) ScriptContext
2123
Close()
@@ -110,7 +112,7 @@ func newWindow(windowOptions ...WindowOption) *window {
110112

111113
func (w *window) checkRedirect(req *http.Request, via []*http.Request) error {
112114
if len(via) > 9 {
113-
return errors.New("Too many redirects")
115+
return ErrTooManyRedirects
114116
}
115117
w.history.ReplaceState(EMPTY_STATE, req.URL.String())
116118
return nil

html/window_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/stretchr/testify/suite"
1111

1212
"github.com/gost-dom/browser/dom"
13+
"github.com/gost-dom/browser/html"
1314
. "github.com/gost-dom/browser/html"
1415
. "github.com/gost-dom/browser/internal/testing/gomega-matchers"
1516
"github.com/gost-dom/browser/internal/testing/gosttest"
@@ -72,6 +73,10 @@ func (s *WindowNavigationTestSuite) SetupTest() {
7273
)
7374
w.Write([]byte(respBody))
7475
})
76+
m.HandleFunc("/infinite-redirects", func(w http.ResponseWriter, r *http.Request) {
77+
http.Redirect(w, r, "/infinite-redirects", 301)
78+
})
79+
7580
s.win = htmltest.NewWindowHelper(s.T(), NewWindowFromHandler(m))
7681
s.win.Navigate("https://example.com/page-1")
7782
}
@@ -85,3 +90,8 @@ func (s *WindowNavigationTestSuite) TestRedirectGetRequests() {
8590
s.win.HTMLDocument().GetHTMLElementById("link").Click()
8691
s.Assert().Equal("https://example.com/new-page-2", s.win.Location().Href())
8792
}
93+
94+
func (s *WindowNavigationTestSuite) TestInfinteRedirects() {
95+
err := s.win.Navigate("/infinite-redirects")
96+
s.Assert().ErrorIs(err, html.ErrTooManyRedirects, "Error is too many redirects")
97+
}

0 commit comments

Comments
 (0)