Repository: datreeio/datree
Pull request #100 (2021-07-19 08:17:35)

staging


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/cmd/test/main.go b/cmd/test/main.go
index bd33e6d..8ab7b4e 100644
--- a/cmd/test/main.go
+++ b/cmd/test/main.go
@@ -70,13 +70,13 @@ type TestCommandContext struct {
 
 func New(ctx *TestCommandContext) *cobra.Command {
 	testCommand := &cobra.Command{
-		Use:   "test",
-		Short: "Execute static analysis for pattern",
-		Long:  "Execute static analysis for pattern. Input should be glob or `-` for stdin",
+		Use:   "test <pattern>",
+		Short: "Execute static analysis for given <pattern>",
+		Long:  "Execute static analysis for given <pattern>. Input should be glob or `-` for stdin",
 		Args: func(cmd *cobra.Command, args []string) error {
 			if len(args) < 1 {
-				errMessage := "requires at least 1 arg"
-				fmt.Println(errMessage)
+				errMessage := "Requires at least 1 arg\n"
+				cmd.Usage()
 				return fmt.Errorf(errMessage)
 			}
 			return nil
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)