Skip to content

Commit

Permalink
Merge 8f54375 into 16dcda1
Browse files Browse the repository at this point in the history
  • Loading branch information
kpacha committed Jan 16, 2020
2 parents 16dcda1 + 8f54375 commit 523cd3a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
11 changes: 11 additions & 0 deletions proxy/request.go
Expand Up @@ -3,6 +3,7 @@ package proxy
import (
"bytes"
"io"
"io/ioutil"
"net/url"
)

Expand Down Expand Up @@ -57,6 +58,16 @@ func CloneRequest(r *Request) *Request {
clone := r.Clone()
clone.Headers = CloneRequestHeaders(r.Headers)
clone.Params = CloneRequestParams(r.Params)
if r.Body == nil {
return &clone
}
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
r.Body.Close()

r.Body = ioutil.NopCloser(bytes.NewReader(buf.Bytes()))
clone.Body = ioutil.NopCloser(buf)

return &clone
}

Expand Down
17 changes: 15 additions & 2 deletions proxy/request_test.go
@@ -1,6 +1,10 @@
package proxy

import "testing"
import (
"io/ioutil"
"strings"
"testing"
)

func TestRequestGeneratePath(t *testing.T) {
r := Request{
Expand Down Expand Up @@ -81,8 +85,9 @@ func TestRequest_Clone(t *testing.T) {
}

func TestCloneRequest(t *testing.T) {
body := `{"a":1,"b":2}`
r := Request{
Method: "GET",
Method: "POST",
Params: map[string]string{
"Supu": "42",
"Tupu": "false",
Expand All @@ -91,6 +96,7 @@ func TestCloneRequest(t *testing.T) {
Headers: map[string][]string{
"Content-Type": {"application/json"},
},
Body: ioutil.NopCloser(strings.NewReader(body)),
}
clone := CloneRequest(&r)

Expand Down Expand Up @@ -130,4 +136,11 @@ func TestCloneRequest(t *testing.T) {
if _, ok := clone.Params["Supu"]; !ok {
t.Error("the cloned instance shares its params with the original one")
}

rb, _ := ioutil.ReadAll(r.Body)
cb, _ := ioutil.ReadAll(clone.Body)

if string(cb) != string(rb) || body != string(rb) {
t.Errorf("unexpected bodies. original: %s, returned: %s", string(rb), string(cb))
}
}

0 comments on commit 523cd3a

Please sign in to comment.