Skip to content

Commit

Permalink
Merge 3f6120d into 696db14
Browse files Browse the repository at this point in the history
  • Loading branch information
kpacha committed Dec 5, 2019
2 parents 696db14 + 3f6120d commit 51dd3cb
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
16 changes: 13 additions & 3 deletions proxy/http_response.go
@@ -1,6 +1,7 @@
package proxy

import (
"compress/gzip"
"context"
"io"
"net/http"
Expand Down Expand Up @@ -29,10 +30,19 @@ type HTTPResponseParserFactory func(HTTPResponseParserConfig) HTTPResponseParser
// DefaultHTTPResponseParserFactory is the default implementation of HTTPResponseParserFactory
func DefaultHTTPResponseParserFactory(cfg HTTPResponseParserConfig) HTTPResponseParser {
return func(ctx context.Context, resp *http.Response) (*Response, error) {
defer resp.Body.Close()

var reader io.ReadCloser
switch resp.Header.Get("Content-Encoding") {
case "gzip":
reader, _ = gzip.NewReader(resp.Body)
defer reader.Close()
default:
reader = resp.Body
}

var data map[string]interface{}
err := cfg.Decoder(resp.Body, &data)
resp.Body.Close()
if err != nil {
if err := cfg.Decoder(reader, &data); err != nil {
return nil, err
}

Expand Down
68 changes: 68 additions & 0 deletions proxy/http_response_test.go
@@ -1,11 +1,14 @@
package proxy

import (
"compress/gzip"
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/devopsfaith/krakend/encoding"
)

func TestNopHTTPResponseParser(t *testing.T) {
Expand Down Expand Up @@ -38,3 +41,68 @@ func TestNopHTTPResponseParser(t *testing.T) {
t.Error("unexpected result")
}
}

func TestDefaultHTTPResponseParser_gzipped(t *testing.T) {
w := httptest.NewRecorder()
handler := func(w http.ResponseWriter, r *http.Request) {
gzipWriter, _ := gzip.NewWriterLevel(w, gzip.BestSpeed)
defer gzipWriter.Close()

w.Header().Set("Vary", "Accept-Encoding")
w.Header().Set("Content-Encoding", "gzip")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
gzipWriter.Write([]byte(`{"msg":"some nice, interesting and long content"}`))
gzipWriter.Flush()
}
req, _ := http.NewRequest("GET", "/url", nil)
req.Header.Add("Accept-Encoding", "gzip")
handler(w, req)

result, err := DefaultHTTPResponseParserFactory(HTTPResponseParserConfig{
Decoder: encoding.JSONDecoder,
EntityFormatter: DefaultHTTPResponseParserConfig.EntityFormatter,
})(context.Background(), w.Result())

if err != nil {
t.Error(err)
}

if !result.IsComplete {
t.Error("unexpected result")
}
if len(result.Data) != 1 {
t.Error("unexpected result")
}
if m, ok := result.Data["msg"]; !ok || m != "some nice, interesting and long content" {
t.Error("unexpected result")
}
}

func TestDefaultHTTPResponseParser_plain(t *testing.T) {
w := httptest.NewRecorder()
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write([]byte(`{"msg":"some nice, interesting and long content"}`))
}
req, _ := http.NewRequest("GET", "/url", nil)
handler(w, req)

result, err := DefaultHTTPResponseParserFactory(HTTPResponseParserConfig{
Decoder: encoding.JSONDecoder,
EntityFormatter: DefaultHTTPResponseParserConfig.EntityFormatter,
})(context.Background(), w.Result())

if err != nil {
t.Error(err)
}

if !result.IsComplete {
t.Error("unexpected result")
}
if len(result.Data) != 1 {
t.Error("unexpected result")
}
if m, ok := result.Data["msg"]; !ok || m != "some nice, interesting and long content" {
t.Error("unexpected result")
}
}

0 comments on commit 51dd3cb

Please sign in to comment.