From 3eaa1bfd3b040cb644c7974d32047dc70037cf95 Mon Sep 17 00:00:00 2001 From: tariq1890 Date: Sat, 2 Jun 2018 21:26:50 -0700 Subject: [PATCH] Setting DisableCompression to true to disable unwanted httpclient tranformations --- pkg/getter/httpgetter.go | 14 ++++---- pkg/getter/httpgetter_test.go | 53 +++++++++++++++++++++++++++-- pkg/getter/testdata/sssd-0.1.0.tgz | Bin 0 -> 2736 bytes 3 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 pkg/getter/testdata/sssd-0.1.0.tgz diff --git a/pkg/getter/httpgetter.go b/pkg/getter/httpgetter.go index 990cb8cc5..1df6b0f62 100644 --- a/pkg/getter/httpgetter.go +++ b/pkg/getter/httpgetter.go @@ -81,19 +81,17 @@ func newHTTPGetter(URL, CertFile, KeyFile, CAFile string) (Getter, error) { // NewHTTPGetter constructs a valid http/https client as HttpGetter func NewHTTPGetter(URL, CertFile, KeyFile, CAFile string) (*HttpGetter, error) { var client HttpGetter + tr := &http.Transport{ + DisableCompression: true, + } if (CertFile != "" && KeyFile != "") || CAFile != "" { tlsConf, err := tlsutil.NewTLSConfig(URL, CertFile, KeyFile, CAFile) if err != nil { return &client, fmt.Errorf("can't create TLS config: %s", err.Error()) } - client.client = &http.Client{ - Transport: &http.Transport{ - TLSClientConfig: tlsConf, - Proxy: http.ProxyFromEnvironment, - }, - } - } else { - client.client = http.DefaultClient + tr.TLSClientConfig = tlsConf + tr.Proxy = http.ProxyFromEnvironment } + client.client = &http.Client{Transport: tr} return &client, nil } diff --git a/pkg/getter/httpgetter_test.go b/pkg/getter/httpgetter_test.go index ec730f401..fa4863de8 100644 --- a/pkg/getter/httpgetter_test.go +++ b/pkg/getter/httpgetter_test.go @@ -16,21 +16,29 @@ limitations under the License. package getter import ( + "io" "net/http" + "net/http/httptest" + "os" "path/filepath" + "strconv" "testing" ) +type TestFileHandler struct{} + +func (h *TestFileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + HandleClient(w, r) +} + func TestHTTPGetter(t *testing.T) { g, err := newHTTPGetter("http://example.com", "", "", "") if err != nil { t.Fatal(err) } - if hg, ok := g.(*HttpGetter); !ok { + if _, ok := g.(*HttpGetter); !ok { t.Fatal("Expected newHTTPGetter to produce an HttpGetter") - } else if hg.client != http.DefaultClient { - t.Fatal("Expected newHTTPGetter to return a default HTTP client.") } // Test with SSL: @@ -58,3 +66,42 @@ func TestHTTPGetter(t *testing.T) { t.Fatal("Expected newHTTPGetter to return a non-default HTTP client") } } + +func HandleClient(writer http.ResponseWriter, request *http.Request) { + f, _ := os.Open("testdata/sssd-0.1.0.tgz") + defer f.Close() + + b := make([]byte, 512) + f.Read(b) + //Get the file size + FileStat, _ := f.Stat() + FileSize := strconv.FormatInt(FileStat.Size(), 10) + + //Simulating improper header values from bitbucket + writer.Header().Set("Content-Type", "application/x-tar") + writer.Header().Set("Content-Encoding", "gzip") + writer.Header().Set("Content-Length", FileSize) + + f.Seek(0, 0) + io.Copy(writer, f) + return +} + +func TestHTTPGetterTarDownload(t *testing.T) { + h := &TestFileHandler{} + server := httptest.NewServer(h) + defer server.Close() + + g, err := newHTTPGetter(server.URL, "", "", "") + if err != nil { + t.Fatal(err) + } + + data, _ := g.Get(server.URL) + mimeType := http.DetectContentType(data.Bytes()) + + expectedMimeType := "application/x-gzip" + if mimeType != expectedMimeType { + t.Fatalf("Expected response with MIME type %s, but got %s", expectedMimeType, mimeType) + } +} diff --git a/pkg/getter/testdata/sssd-0.1.0.tgz b/pkg/getter/testdata/sssd-0.1.0.tgz new file mode 100644 index 0000000000000000000000000000000000000000..2f34fece6a1fd4de1852a3057f7a373e3f3070ad GIT binary patch literal 2736 zcmV;h3QzSPiwG0|00000|0w_~VMtOiV@ORlOnEsqVl!4SWK%V1T2nbTPgYhoO;>Dc zVQyr3R8em|NM&qo0PH&dZyUF-{aJqn=X9@0XP@oZ>6ZiYfFSA6Vr?4CZHJ*499hTn zY0*iVq;un!=KbvtlFna~yriGC=?0iT*mn{|QlyR&Nm*;N_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