Merge pull request #4165 from tariq1890/master

Setting DisableCompression to true to disable unwanted httpclient transformation
pull/3438/merge
Matthew Fisher 7 years ago committed by GitHub
commit ad38044f7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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
}

@ -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)
}
}

Binary file not shown.
Loading…
Cancel
Save