1
1
package html_test
2
2
3
3
import (
4
+ "fmt"
5
+ "io"
6
+ "net/http"
4
7
"strings"
5
8
"testing"
6
9
@@ -10,6 +13,7 @@ import (
10
13
. "github.com/gost-dom/browser/html"
11
14
. "github.com/gost-dom/browser/internal/testing/gomega-matchers"
12
15
"github.com/gost-dom/browser/internal/testing/gosttest"
16
+ "github.com/gost-dom/browser/internal/testing/htmltest"
13
17
. "github.com/gost-dom/browser/testing/gomega-matchers"
14
18
)
15
19
@@ -32,3 +36,52 @@ func (s *WindowTestSuite) TestDocumentWithDOCTYPE() {
32
36
func TestWindow (t * testing.T ) {
33
37
suite .Run (t , new (WindowTestSuite ))
34
38
}
39
+
40
+ type WindowNavigationTestSuite struct {
41
+ gosttest.GomegaSuite
42
+ win htmltest.WindowHelper
43
+ }
44
+
45
+ func (s * WindowNavigationTestSuite ) SetupTest () {
46
+ m := http .NewServeMux ()
47
+ m .HandleFunc ("GET /page-1" , func (w http.ResponseWriter , r * http.Request ) {
48
+ w .Write ([]byte (`<body>
49
+ <a id="link" href="/old-page-2">Link</a>
50
+ <form method="post" action="/old-page-2" id="form">
51
+ <input name="data" value="value" type="text" />
52
+ <button type="submit" id="submit">Submit</button>
53
+ </form>
54
+ </body>` ))
55
+ })
56
+ m .HandleFunc ("/old-page-2" , func (w http.ResponseWriter , r * http.Request ) {
57
+ u := r .URL
58
+ u .Path = "/new-page-2"
59
+ w .Header ().Add ("Location" , u .String ())
60
+ w .WriteHeader (301 )
61
+ })
62
+ m .HandleFunc ("/new-page-2" , func (w http.ResponseWriter , r * http.Request ) {
63
+ body , err := io .ReadAll (r .Body )
64
+ if err != nil {
65
+ w .WriteHeader (500 )
66
+ }
67
+ respBody := fmt .Sprintf (
68
+ `<body><h1>Page 2</h1>
69
+ <div id="method">%s</div>
70
+ <div id="request-body">%s</div>
71
+ </body>` , r .Method , string (body ),
72
+ )
73
+ w .Write ([]byte (respBody ))
74
+ })
75
+ s .win = htmltest .NewWindowHelper (s .T (), NewWindowFromHandler (m ))
76
+ s .win .Navigate ("https://example.com/page-1" )
77
+ }
78
+
79
+ func TestWindowNavigation (t * testing.T ) {
80
+ suite .Run (t , new (WindowNavigationTestSuite ))
81
+ }
82
+
83
+ func (s * WindowNavigationTestSuite ) TestRedirectGetRequests () {
84
+ s .Assert ().Equal ("/page-1" , s .win .Location ().Pathname ())
85
+ s .win .HTMLDocument ().GetHTMLElementById ("link" ).Click ()
86
+ s .Assert ().Equal ("https://example.com/new-page-2" , s .win .Location ().Href ())
87
+ }
0 commit comments