Skip to content

Commit 5d64df9

Browse files
committed
increase code coverage
resolve #149
1 parent e6d6a87 commit 5d64df9

File tree

4 files changed

+135
-8
lines changed

4 files changed

+135
-8
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- name: test
2626
run: |
2727
go generate
28-
godev -m 95
28+
godev -m 100
2929
3030
- uses: codecov/codecov-action@v1
3131

hijack.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,6 @@ func (p *Page) GetDownloadFile(pattern string, resourceType proto.NetworkResourc
395395

396396
ctx.Skip = true
397397

398-
ctx.OnError = func(e error) {
399-
err = e
400-
}
401-
402398
err = ctx.LoadResponse(client, true)
403399
if err != nil {
404400
return

hijack_test.go

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package rod_test
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io/ioutil"
78
"net/http"
@@ -40,8 +41,13 @@ func (s *S) TestHijack() {
4041
r := ctx.Request
4142
r.Req().URL = r.Req().URL // override request url
4243
r.Req().Header.Set("Test", "header") // override request header
44+
r.SetBody([]byte("test")) // override request body
45+
r.SetBody(123) // override request body
4346
r.SetBody(r.Body()) // override request body
4447

48+
s.Equal(http.MethodPost, r.Method())
49+
s.Equal(url+"/a", r.URL().String())
50+
4551
s.Equal(proto.NetworkResourceTypeXHR, ctx.Request.Type())
4652
s.Contains(ctx.Request.Header("Origin"), url)
4753
s.Len(ctx.Request.Headers(), 5)
@@ -62,9 +68,11 @@ func (s *S) TestHijack() {
6268
ctx.Response.SetHeader("Set-Cookie", "key=val")
6369

6470
// override response body
65-
ctx.Response.SetBody(utils.MustToJSON(map[string]string{
66-
"text": ctx.Response.Body(),
67-
}))
71+
ctx.Response.SetBody([]byte("test"))
72+
ctx.Response.SetBody(123)
73+
ctx.Response.SetBody(map[string]string{
74+
"text": "test",
75+
})
6876

6977
s.Equal("{\"text\":\"test\"}", ctx.Response.Body())
7078
})
@@ -155,6 +163,78 @@ func (s *S) TestHijackFailRequest() {
155163
}()
156164
}
157165

166+
func (s *S) TestHijackLoadResponseErr() {
167+
ctx, cancel := context.WithCancel(s.browser.GetContext())
168+
defer cancel()
169+
p := s.page.Context(ctx, cancel)
170+
router := p.HijackRequests()
171+
defer router.MustStop()
172+
173+
wg := &sync.WaitGroup{}
174+
wg.Add(1)
175+
176+
router.MustAdd("*", func(ctx *rod.Hijack) {
177+
s.Error(ctx.LoadResponse(&http.Client{
178+
Transport: &MockRoundTripper{err: errors.New("err")},
179+
}, true))
180+
181+
s.Error(ctx.LoadResponse(&http.Client{
182+
Transport: &MockRoundTripper{res: &http.Response{
183+
StatusCode: 200,
184+
Body: ioutil.NopCloser(&MockReader{err: errors.New("err")}),
185+
}},
186+
}, true))
187+
188+
wg.Done()
189+
})
190+
191+
go router.Run()
192+
193+
go func() { _ = p.Navigate(srcFile("./fixtures/click.html")) }()
194+
195+
wg.Wait()
196+
}
197+
198+
func (s *S) TestHijackResponseErr() {
199+
url, mux, close := utils.Serve("")
200+
defer close()
201+
202+
// to simulate a backend server
203+
mux.HandleFunc("/", httpHTML(`ok`))
204+
205+
ctx, cancel := context.WithCancel(s.browser.GetContext())
206+
defer cancel()
207+
p := s.page.Context(ctx, cancel)
208+
router := p.HijackRequests()
209+
defer router.MustStop()
210+
211+
wg := &sync.WaitGroup{}
212+
wg.Add(1)
213+
214+
router.MustAdd("*", func(ctx *rod.Hijack) {
215+
ctx.OnError = func(err error) {
216+
s.Error(err)
217+
wg.Done()
218+
}
219+
220+
ctx.MustLoadResponse()
221+
s.mockClient.setCall(func(ctx context.Context, sessionID, method string, params interface{}) ([]byte, error) {
222+
if method == (proto.FetchFulfillRequest{}).MethodName() {
223+
return nil, errors.New("err")
224+
}
225+
return s.mockClient.principal.Call(ctx, sessionID, method, params)
226+
})
227+
228+
})
229+
230+
go router.Run()
231+
232+
go func() { _ = p.Navigate(url) }()
233+
234+
wg.Wait()
235+
s.mockClient.resetCall()
236+
}
237+
158238
func (s *S) TestHandleAuth() {
159239
url, mux, close := utils.Serve("")
160240
defer close()
@@ -178,6 +258,21 @@ func (s *S) TestHandleAuth() {
178258
page := s.browser.MustPage(url)
179259
defer page.MustClose()
180260
page.MustElementMatches("p", "ok")
261+
262+
func() {
263+
wait := s.browser.HandleAuth("a", "b")
264+
go func() { _, _ = s.browser.Page(url) }()
265+
266+
s.mockClient.setCall(func(ctx context.Context, sessionID, method string, params interface{}) ([]byte, error) {
267+
if method == (proto.FetchContinueRequest{}).MethodName() {
268+
return nil, errors.New("err")
269+
}
270+
return s.mockClient.principal.Call(ctx, sessionID, method, params)
271+
})
272+
defer s.mockClient.resetCall()
273+
274+
s.Error(wait())
275+
}()
181276
}
182277

183278
func (s *S) TestGetDownloadFile() {
@@ -198,6 +293,18 @@ func (s *S) TestGetDownloadFile() {
198293
data := wait()
199294

200295
s.Equal(content, string(data))
296+
297+
waitErr := page.GetDownloadFile(url+"/d", "", &http.Client{
298+
Transport: &MockRoundTripper{err: errors.New("err")},
299+
})
300+
page.MustElement("a").MustClick()
301+
func() {
302+
defer s.errorAt(1, nil)()
303+
_, _, err := waitErr()
304+
s.Error(err)
305+
}()
306+
_, _, err := waitErr()
307+
s.Error(err)
201308
}
202309

203310
func (s *S) TestGetDownloadFileFromDataURI() {
@@ -228,6 +335,13 @@ func (s *S) TestGetDownloadFileFromDataURI() {
228335
page.MustElement("#b").MustClick()
229336
data = wait()
230337
s.Equal("test blob", string(data))
338+
339+
s.Panics(func() {
340+
wait = page.MustGetDownloadFile("data:*")
341+
page.MustElement("#b").MustClick()
342+
defer s.errorAt(2, nil)()
343+
data = wait()
344+
})
231345
}
232346

233347
func (s *S) TestGetDownloadFileWithHijack() {

setup_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,23 @@ func serveStatic() (string, func()) {
113113
return u + "/", close
114114
}
115115

116+
type MockRoundTripper struct {
117+
res *http.Response
118+
err error
119+
}
120+
121+
func (mrt *MockRoundTripper) RoundTrip(*http.Request) (*http.Response, error) {
122+
return mrt.res, mrt.err
123+
}
124+
125+
type MockReader struct {
126+
err error
127+
}
128+
129+
func (mr *MockReader) Read(p []byte) (n int, err error) {
130+
return 0, mr.err
131+
}
132+
116133
type Call func(ctx context.Context, sessionID, method string, params interface{}) ([]byte, error)
117134

118135
var _ rod.Client = &MockClient{}

0 commit comments

Comments
 (0)