Repository: datreeio/datree
Pull request #99 (2021-07-18 13:23:33)

fix: gzip requests with body


Patch:
diff --git a/pkg/httpClient/client.go b/pkg/httpClient/client.go
index a0814d4..ae8b417 100644
--- a/pkg/httpClient/client.go
+++ b/pkg/httpClient/client.go
@@ -2,6 +2,7 @@ package httpClient
 
 import (
 	"bytes"
+	"compress/gzip"
 	"encoding/json"
 	"fmt"
 	"io"
@@ -89,27 +90,47 @@ func (c *Client) Request(method string, resourceURI string, body interface{}, he
 }
 
 func (c *Client) createNewRequest(method string, url string, body interface{}, headers map[string]string) (*http.Request, error) {
-	bodyBuffer, err := c.parseBody(body)
-	if err != nil {
-		return nil, err
-	}
-
-	request, err := http.NewRequest(method, url, bodyBuffer)
-	if err != nil {
-		return nil, err
-	}
-	requestHeaders := c.mergeWithDefaultHeaders(headers)
-	c.addHeadersToRequest(request, requestHeaders)
+	var request *http.Request
+	var err error
 
 	if body != nil {
+		var encodedBuf bytes.Buffer
+		enc := json.NewEncoder(&encodedBuf)
+		enc.SetEscapeHTML(false)
+		err = enc.Encode(body)
+		if err != nil {
+			return nil, err
+		}
+
+		var gzippedBuf bytes.Buffer
+		gzipWriter := gzip.NewWriter(&gzippedBuf)
+		gzipWriter.Write(encodedBuf.Bytes())
+		if err = gzipWriter.Close(); err != nil {
+			return nil, err
+		}
+
+		request, err = http.NewRequest(method, url, &gzippedBuf)
+		if err != nil {
+			return nil, err
+		}
+
 		contentTypeHeader := c.getValueOfHeader(headers, "Content-Type")
 		if contentTypeHeader == "" {
 			request.Header.Set("Content-Type", defaultContentType)
 		} else {
 			request.Header.Set("Content-Type", contentTypeHeader)
 		}
+		request.Header.Set("Content-Encoding", "gzip")
+	} else {
+		request, err = http.NewRequest(method, url, nil)
+		if err != nil {
+			return nil, err
+		}
 	}
 
+	requestHeaders := c.mergeWithDefaultHeaders(headers)
+	c.addHeadersToRequest(request, requestHeaders)
+
 	return request, nil
 }
 


Test patch:
diff --git a/pkg/httpClient/client_test.go b/pkg/httpClient/client_test.go
index ae38083..848a32a 100644
--- a/pkg/httpClient/client_test.go
+++ b/pkg/httpClient/client_test.go
@@ -1,6 +1,8 @@
 package httpClient
 
 import (
+	"bytes"
+	"compress/gzip"
 	"io/ioutil"
 	"net/http"
 	"net/http/httptest"
@@ -79,6 +81,20 @@ func TestRequestWithBadUrl(t *testing.T) {
 	assert.NotNil(t, err)
 }
 
+func gunzipBody(body []byte) string {
+	buf := bytes.NewBuffer(body)
+	gzipReader, err := gzip.NewReader(buf)
+	if err != nil {
+		panic(err)
+	}
+	var gunzippedBuf bytes.Buffer
+	_, err = gunzippedBuf.ReadFrom(gzipReader)
+	if err != nil {
+		panic(err)
+	}
+	return string(gunzippedBuf.Bytes())
+}
+
 func createMockServer(t *testing.T, tc *testCase) *httptest.Server {
 	return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
 
@@ -86,7 +102,12 @@ func createMockServer(t *testing.T, tc *testCase) *httptest.Server {
 
 		body, _ := ioutil.ReadAll(req.Body)
 
-		assert.Equal(t, tc.expectedRequestBody, string(body))
+		if len(tc.expectedRequestBody) > 0 {
+			gunzippedBody := gunzipBody(body)
+			assert.Equal(t, tc.expectedRequestBody, gunzippedBody)
+		} else {
+			assert.Equal(t, tc.expectedRequestBody, string(body))
+		}
 
 		assert.Equal(t, tc.method, req.Method)