From 5d64df9296600ed8d9d238fa052ddb1444bfd0d5 Mon Sep 17 00:00:00 2001 From: Yad Smood Date: Thu, 10 Sep 2020 15:16:53 +0900 Subject: [PATCH] increase code coverage resolve #149 --- .github/workflows/test.yml | 2 +- hijack.go | 4 -- hijack_test.go | 120 ++++++++++++++++++++++++++++++++++++- setup_test.go | 17 ++++++ 4 files changed, 135 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fa04688b..d37564c6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,7 +25,7 @@ jobs: - name: test run: | go generate - godev -m 95 + godev -m 100 - uses: codecov/codecov-action@v1 diff --git a/hijack.go b/hijack.go index 06f3a23f..0f5a12f4 100644 --- a/hijack.go +++ b/hijack.go @@ -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 diff --git a/hijack_test.go b/hijack_test.go index 02594881..63812e00 100644 --- a/hijack_test.go +++ b/hijack_test.go @@ -2,6 +2,7 @@ package rod_test import ( "context" + "errors" "fmt" "io/ioutil" "net/http" @@ -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) @@ -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()) }) @@ -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() @@ -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() { @@ -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() { @@ -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() { diff --git a/setup_test.go b/setup_test.go index d0fbc1c7..2b406a89 100644 --- a/setup_test.go +++ b/setup_test.go @@ -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{}