Skip to content

Commit

Permalink
Add testcase for POST redirect with original method and body
Browse files Browse the repository at this point in the history
  • Loading branch information
liguangbo committed Jul 26, 2022
1 parent f6fb976 commit b996aaa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
25 changes: 25 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"reflect"
"strconv"
"strings"
"sync"
"testing"
"time"
)
Expand Down Expand Up @@ -787,3 +788,27 @@ func TestHostURLForGH318AndGH407(t *testing.T) {
assertNil(t, err)
assertNotNil(t, resp)
}

func TestPostRedirectWithBody(t *testing.T) {
ts := createPostServer(t)
defer ts.Close()

targetURL, _ := url.Parse(ts.URL)
t.Log("ts.URL:", ts.URL)
t.Log("targetURL.Host:", targetURL.Host)

c := dc()
wg := sync.WaitGroup{}
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
resp, err := c.R().
SetBody([]byte(strconv.Itoa(newRnd().Int()))).
Post(targetURL.String() + "/redirect-with-body")
assertNil(t, err)
assertNotNil(t, resp)
}()
}
wg.Wait()
}
14 changes: 14 additions & 0 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -291,6 +292,19 @@ func createPostServer(t *testing.T) *httptest.Server {
w.Header().Set(hdrLocationKey, "/login")
w.WriteHeader(http.StatusTemporaryRedirect)
}

if r.URL.Path == "/redirect-with-body" {
body, _ := ioutil.ReadAll(r.Body)
query := url.Values{}
query.Add("body", string(body))
w.Header().Set(hdrLocationKey, "/redirected-with-body?"+query.Encode())
w.WriteHeader(http.StatusTemporaryRedirect)
}
if r.URL.Path == "/redirected-with-body" {
body, _ := ioutil.ReadAll(r.Body)
assertEqual(t, r.URL.Query().Get("body"), string(body))
w.WriteHeader(http.StatusOK)
}
}
})

Expand Down

0 comments on commit b996aaa

Please sign in to comment.