Skip to content

Commit

Permalink
Merge pull request #334 from devopsfaith/noop_nil_body
Browse files Browse the repository at this point in the history
do not copy nil readers
  • Loading branch information
kpacha committed Mar 4, 2020
2 parents cff66ab + 2fa469f commit 1cbc06b
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
3 changes: 3 additions & 0 deletions router/gin/render.go
Expand Up @@ -132,6 +132,9 @@ func noopRender(c *gin.Context, response *proxy.Response) {
c.Writer.Header().Add(k, v)
}
}
if response.Io == nil {
return
}
io.Copy(c.Writer, response.Io)
}

Expand Down
49 changes: 48 additions & 1 deletion router/gin/render_test.go
Expand Up @@ -403,7 +403,7 @@ func TestRender_noop(t *testing.T) {
StatusCode: 200,
Headers: map[string][]string{
"Content-Type": {expectedHeader},
"Set-Cookie": []string{"test1=test1", "test2=test2"},
"Set-Cookie": {"test1=test1", "test2=test2"},
},
},
Io: bytes.NewBufferString(expectedContent),
Expand Down Expand Up @@ -453,6 +453,53 @@ func TestRender_noop(t *testing.T) {
}
}

func TestRender_noop_nilBody(t *testing.T) {
expectedContent := ""
expectedHeader := ""

p := func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) {
return &proxy.Response{IsComplete: true}, nil
}
endpoint := &config.EndpointConfig{
Timeout: time.Second,
CacheTTL: 6 * time.Hour,
QueryString: []string{"b"},
OutputEncoding: encoding.NOOP,
}

gin.SetMode(gin.TestMode)
server := gin.New()
server.GET("/_gin_endpoint/:param", EndpointHandler(endpoint, p))

req, _ := http.NewRequest("GET", "http://127.0.0.1:8080/_gin_endpoint/a?b=1", ioutil.NopCloser(&bytes.Buffer{}))
req.Header.Set("Content-Type", "application/json")

w := httptest.NewRecorder()
server.ServeHTTP(w, req)

defer w.Result().Body.Close()

body, ioerr := ioutil.ReadAll(w.Result().Body)
if ioerr != nil {
t.Error("reading response body:", ioerr)
return
}

content := string(body)
if w.Result().Header.Get("Content-Type") != expectedHeader {
t.Error("Content-Type error:", w.Result().Header.Get("Content-Type"))
}
if w.Result().Header.Get("X-Krakend") != "Version undefined" {
t.Error("X-Krakend error:", w.Result().Header.Get("X-Krakend"))
}
if w.Result().StatusCode != http.StatusOK {
t.Error("Unexpected status code:", w.Result().StatusCode)
}
if content != expectedContent {
t.Error("Unexpected body:", content, "expected:", expectedContent)
}
}

func TestRender_noop_nilResponse(t *testing.T) {
p := func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) {
return nil, nil
Expand Down
3 changes: 3 additions & 0 deletions router/mux/render.go
Expand Up @@ -106,5 +106,8 @@ func noopRender(w http.ResponseWriter, response *proxy.Response) {
}
w.WriteHeader(response.Metadata.StatusCode)

if response.Io == nil {
return
}
io.Copy(w, response.Io)
}
49 changes: 48 additions & 1 deletion router/mux/render_test.go
Expand Up @@ -235,7 +235,7 @@ func TestRender_noop(t *testing.T) {
StatusCode: 200,
Headers: map[string][]string{
"Content-Type": {expectedHeader},
"Set-Cookie": []string{"test1=test1", "test2=test2"},
"Set-Cookie": {"test1=test1", "test2=test2"},
},
},
Io: bytes.NewBufferString(expectedContent),
Expand Down Expand Up @@ -285,6 +285,53 @@ func TestRender_noop(t *testing.T) {
}
}

func TestRender_noop_nilBody(t *testing.T) {
expectedContent := ""
expectedHeader := ""

p := func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) {
return &proxy.Response{IsComplete: true}, nil
}
endpoint := &config.EndpointConfig{
Method: "GET",
Timeout: time.Second,
CacheTTL: 6 * time.Hour,
QueryString: []string{"b"},
OutputEncoding: encoding.NOOP,
}

router := http.NewServeMux()
router.Handle("/_mux_endpoint", EndpointHandler(endpoint, p))

req, _ := http.NewRequest("GET", "http://127.0.0.1:8080/_mux_endpoint?b=1", ioutil.NopCloser(&bytes.Buffer{}))
req.Header.Set("Content-Type", "application/json")

w := httptest.NewRecorder()
router.ServeHTTP(w, req)

defer w.Result().Body.Close()

body, ioerr := ioutil.ReadAll(w.Result().Body)
if ioerr != nil {
t.Error("reading response body:", ioerr)
return
}

content := string(body)
if w.Result().Header.Get("Content-Type") != expectedHeader {
t.Error("Content-Type error:", w.Result().Header.Get("Content-Type"))
}
if w.Result().Header.Get("X-Krakend") != "Version undefined" {
t.Error("X-Krakend error:", w.Result().Header.Get("X-Krakend"))
}
if w.Result().StatusCode != http.StatusOK {
t.Error("Unexpected status code:", w.Result().StatusCode)
}
if content != expectedContent {
t.Error("Unexpected body:", content, "expected:", expectedContent)
}
}

func TestRender_noop_nilResponse(t *testing.T) {
p := func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) {
return nil, nil
Expand Down

0 comments on commit 1cbc06b

Please sign in to comment.