Skip to content

Commit

Permalink
increase code coverage
Browse files Browse the repository at this point in the history
resolve #149
  • Loading branch information
ysmood committed Sep 10, 2020
1 parent e6d6a87 commit 5d64df9
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: test
run: |
go generate
godev -m 95
godev -m 100
- uses: codecov/codecov-action@v1

Expand Down
4 changes: 0 additions & 4 deletions hijack.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,6 @@ func (p *Page) GetDownloadFile(pattern string, resourceType proto.NetworkResourc

ctx.Skip = true

ctx.OnError = func(e error) {
err = e
}

err = ctx.LoadResponse(client, true)
if err != nil {
return
Expand Down
120 changes: 117 additions & 3 deletions hijack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rod_test

import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -40,8 +41,13 @@ func (s *S) TestHijack() {
r := ctx.Request
r.Req().URL = r.Req().URL // override request url
r.Req().Header.Set("Test", "header") // override request header
r.SetBody([]byte("test")) // override request body
r.SetBody(123) // override request body
r.SetBody(r.Body()) // override request body

s.Equal(http.MethodPost, r.Method())
s.Equal(url+"/a", r.URL().String())

s.Equal(proto.NetworkResourceTypeXHR, ctx.Request.Type())
s.Contains(ctx.Request.Header("Origin"), url)
s.Len(ctx.Request.Headers(), 5)
Expand All @@ -62,9 +68,11 @@ func (s *S) TestHijack() {
ctx.Response.SetHeader("Set-Cookie", "key=val")

// override response body
ctx.Response.SetBody(utils.MustToJSON(map[string]string{
"text": ctx.Response.Body(),
}))
ctx.Response.SetBody([]byte("test"))
ctx.Response.SetBody(123)
ctx.Response.SetBody(map[string]string{
"text": "test",
})

s.Equal("{\"text\":\"test\"}", ctx.Response.Body())
})
Expand Down Expand Up @@ -155,6 +163,78 @@ func (s *S) TestHijackFailRequest() {
}()
}

func (s *S) TestHijackLoadResponseErr() {
ctx, cancel := context.WithCancel(s.browser.GetContext())
defer cancel()
p := s.page.Context(ctx, cancel)
router := p.HijackRequests()
defer router.MustStop()

wg := &sync.WaitGroup{}
wg.Add(1)

router.MustAdd("*", func(ctx *rod.Hijack) {
s.Error(ctx.LoadResponse(&http.Client{
Transport: &MockRoundTripper{err: errors.New("err")},
}, true))

s.Error(ctx.LoadResponse(&http.Client{
Transport: &MockRoundTripper{res: &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(&MockReader{err: errors.New("err")}),
}},
}, true))

wg.Done()
})

go router.Run()

go func() { _ = p.Navigate(srcFile("./fixtures/click.html")) }()

wg.Wait()
}

func (s *S) TestHijackResponseErr() {
url, mux, close := utils.Serve("")
defer close()

// to simulate a backend server
mux.HandleFunc("/", httpHTML(`ok`))

ctx, cancel := context.WithCancel(s.browser.GetContext())
defer cancel()
p := s.page.Context(ctx, cancel)
router := p.HijackRequests()
defer router.MustStop()

wg := &sync.WaitGroup{}
wg.Add(1)

router.MustAdd("*", func(ctx *rod.Hijack) {
ctx.OnError = func(err error) {
s.Error(err)
wg.Done()
}

ctx.MustLoadResponse()
s.mockClient.setCall(func(ctx context.Context, sessionID, method string, params interface{}) ([]byte, error) {
if method == (proto.FetchFulfillRequest{}).MethodName() {
return nil, errors.New("err")
}
return s.mockClient.principal.Call(ctx, sessionID, method, params)
})

})

go router.Run()

go func() { _ = p.Navigate(url) }()

wg.Wait()
s.mockClient.resetCall()
}

func (s *S) TestHandleAuth() {
url, mux, close := utils.Serve("")
defer close()
Expand All @@ -178,6 +258,21 @@ func (s *S) TestHandleAuth() {
page := s.browser.MustPage(url)
defer page.MustClose()
page.MustElementMatches("p", "ok")

func() {
wait := s.browser.HandleAuth("a", "b")
go func() { _, _ = s.browser.Page(url) }()

s.mockClient.setCall(func(ctx context.Context, sessionID, method string, params interface{}) ([]byte, error) {
if method == (proto.FetchContinueRequest{}).MethodName() {
return nil, errors.New("err")
}
return s.mockClient.principal.Call(ctx, sessionID, method, params)
})
defer s.mockClient.resetCall()

s.Error(wait())
}()
}

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

s.Equal(content, string(data))

waitErr := page.GetDownloadFile(url+"/d", "", &http.Client{
Transport: &MockRoundTripper{err: errors.New("err")},
})
page.MustElement("a").MustClick()
func() {
defer s.errorAt(1, nil)()
_, _, err := waitErr()
s.Error(err)
}()
_, _, err := waitErr()
s.Error(err)
}

func (s *S) TestGetDownloadFileFromDataURI() {
Expand Down Expand Up @@ -228,6 +335,13 @@ func (s *S) TestGetDownloadFileFromDataURI() {
page.MustElement("#b").MustClick()
data = wait()
s.Equal("test blob", string(data))

s.Panics(func() {
wait = page.MustGetDownloadFile("data:*")
page.MustElement("#b").MustClick()
defer s.errorAt(2, nil)()
data = wait()
})
}

func (s *S) TestGetDownloadFileWithHijack() {
Expand Down
17 changes: 17 additions & 0 deletions setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ func serveStatic() (string, func()) {
return u + "/", close
}

type MockRoundTripper struct {
res *http.Response
err error
}

func (mrt *MockRoundTripper) RoundTrip(*http.Request) (*http.Response, error) {
return mrt.res, mrt.err
}

type MockReader struct {
err error
}

func (mr *MockReader) Read(p []byte) (n int, err error) {
return 0, mr.err
}

type Call func(ctx context.Context, sessionID, method string, params interface{}) ([]byte, error)

var _ rod.Client = &MockClient{}
Expand Down

0 comments on commit 5d64df9

Please sign in to comment.