Skip to content

add GetBody in hijack LoadResponse request for http2 retry #1128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
loadresponse getbody test
  • Loading branch information
pyneda committed Oct 21, 2024
commit c77c27b4bf430f65e1cd784447c3c49bc6533b8d
4 changes: 1 addition & 3 deletions hijack.go
Original file line number Diff line number Diff line change
@@ -223,8 +223,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 {
req := h.Request.req

if req.Body != nil {
if req.Body != nil && req.GetBody == nil {
bodyBytes, err := io.ReadAll(req.Body)
if err != nil {
return err
@@ -237,7 +236,6 @@ func (h *Hijack) LoadResponse(client *http.Client, loadBody bool) error {
return io.NopCloser(bytes.NewBuffer(bodyBytes)), nil
}
}

res, err := client.Do(req)
if err != nil {
return err
53 changes: 53 additions & 0 deletions hijack_test.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import (
"io"
"mime"
"net/http"
"strings"
"sync"
"testing"
"time"
@@ -380,3 +381,55 @@ func TestHandleAuth(t *testing.T) {
wait2()
page2.MustClose()
}

func TestHijackWithRedirectAndLoadResponseGetBody(t *testing.T) {
g := setup(t)

redirectCount := 0
s := g.Serve()
s.Mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
g.Eq(r.Method, http.MethodPost)
body, err := io.ReadAll(r.Body)
g.Eq(err, nil)
g.Eq("test", string(body))

if redirectCount < 3 {
redirectCount++
w.Header().Set("Location", s.URL("/test"))
w.WriteHeader(http.StatusTemporaryRedirect)
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})

router := g.page.HijackRequests()
defer router.MustStop()

router.MustAdd(s.URL("/test"), func(ctx *rod.Hijack) {
ctx.Request.Req().Body = io.NopCloser(strings.NewReader("test"))
ctx.Request.Req().Method = http.MethodPost
ctx.Request.SetBody([]byte("test"))

err := ctx.LoadResponse(http.DefaultClient, true)
g.Eq(err, nil)

body, err := ctx.Request.Req().GetBody()
g.Eq(err, nil)
bodyBytes, err := io.ReadAll(body)

g.Eq(err, nil)
g.Eq("test", string(bodyBytes))

})

go router.Run()

g.page.MustNavigate(s.URL("/test"))

content := g.page.MustElement("body").MustText()
g.Eq(content, "OK")
g.Eq(redirectCount, 3)

}