Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,9 @@ func Example_hijack_requests() {
// Not calling this will require you to mock the entire response.
// This can be done with the SetXxx (Status, Header, Body) functions on the
// ctx.Response struct.
_ = ctx.LoadResponse(http.DefaultClient, true)
_ = ctx.LoadResponse(http.DefaultClient, func(res *http.Response) bool {
return true
})

// Here we append some code to every js file.
// The code will update the document title to "hi"
Expand Down
9 changes: 7 additions & 2 deletions hijack.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (h *Hijack) ContinueRequest(cq *proto.FetchContinueRequest) {
}

// LoadResponse will send request to the real destination and load the response as default response to override.
func (h *Hijack) LoadResponse(client *http.Client, loadBody bool) error {
func (h *Hijack) LoadResponse(client *http.Client, shouldLoadBody func(*http.Response) bool) error {
res, err := client.Do(h.Request.req)
if err != nil {
return err
Expand All @@ -238,7 +238,7 @@ func (h *Hijack) LoadResponse(client *http.Client, loadBody bool) error {
}
}

if loadBody {
if shouldLoadBody(res) {
b, err := io.ReadAll(res.Body)
if err != nil {
return err
Expand All @@ -255,6 +255,11 @@ type HijackRequest struct {
req *http.Request
}

// Event associated to the request.
func (ctx *HijackRequest) Event() *proto.FetchRequestPaused {
return ctx.event
}

// Type of the resource.
func (ctx *HijackRequest) Type() proto.NetworkResourceType {
return ctx.event.ResourceType
Expand Down
8 changes: 6 additions & 2 deletions hijack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,18 @@ func TestHijackLoadResponseErr(t *testing.T) {
router.MustAdd("http://localhost/a", func(ctx *rod.Hijack) {
g.Err(ctx.LoadResponse(&http.Client{
Transport: &MockRoundTripper{err: errors.New("err")},
}, true))
}, func(*http.Response) bool {
return true
}))

g.Err(ctx.LoadResponse(&http.Client{
Transport: &MockRoundTripper{res: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(&MockReader{err: errors.New("err")}),
}},
}, true))
}, func(*http.Response) bool {
return true
}))

wg.Done()

Expand Down
4 changes: 3 additions & 1 deletion must.go
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,9 @@ func (r *HijackRouter) MustStop() {

// MustLoadResponse is similar to [Hijack.LoadResponse].
func (h *Hijack) MustLoadResponse() {
h.browser.e(h.LoadResponse(http.DefaultClient, true))
h.browser.e(h.LoadResponse(http.DefaultClient, func(res *http.Response) bool {
return true
}))
}

// MustEqual is similar to [Element.Equal].
Expand Down