github.com/wangchanggan/helm@v0.0.0-20211020154240-11b1b7d5406d/pkg/getter/httpgetter_test.go (about) 1 /* 2 Copyright The Helm Authors. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package getter 17 18 import ( 19 "io" 20 "net/http" 21 "net/http/httptest" 22 "os" 23 "path/filepath" 24 "strconv" 25 "testing" 26 ) 27 28 type TestFileHandler struct{} 29 30 func (h *TestFileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 31 HandleClient(w, r) 32 } 33 34 func TestHTTPGetter(t *testing.T) { 35 g, err := newHTTPGetter("http://example.com", "", "", "") 36 if err != nil { 37 t.Fatal(err) 38 } 39 40 if _, ok := g.(*HttpGetter); !ok { 41 t.Fatal("Expected newHTTPGetter to produce an HttpGetter") 42 } 43 44 // Test with SSL: 45 cd := "../../testdata" 46 join := filepath.Join 47 ca, pub, priv := join(cd, "ca.pem"), join(cd, "crt.pem"), join(cd, "key.pem") 48 g, err = newHTTPGetter("https://example.com/", pub, priv, ca) 49 if err != nil { 50 t.Fatal(err) 51 } 52 if hg, ok := g.(*HttpGetter); !ok { 53 t.Fatal("Expected newHTTPGetter to produce an HttpGetter") 54 } else if hg.client == http.DefaultClient { 55 t.Fatal("Expected newHTTPGetter to return a non-default HTTP client") 56 } 57 58 // Test with SSL, caFile only 59 g, err = newHTTPGetter("https://example.com/", "", "", ca) 60 if err != nil { 61 t.Fatal(err) 62 } 63 if hg, ok := g.(*HttpGetter); !ok { 64 t.Fatal("Expected newHTTPGetter to produce an HttpGetter") 65 } else if hg.client == http.DefaultClient { 66 t.Fatal("Expected newHTTPGetter to return a non-default HTTP client") 67 } 68 } 69 70 func HandleClient(writer http.ResponseWriter, request *http.Request) { 71 f, _ := os.Open("testdata/sssd-0.1.0.tgz") 72 defer f.Close() 73 74 b := make([]byte, 512) 75 f.Read(b) 76 //Get the file size 77 FileStat, _ := f.Stat() 78 FileSize := strconv.FormatInt(FileStat.Size(), 10) 79 80 //Simulating improper header values from bitbucket 81 writer.Header().Set("Content-Type", "application/x-tar") 82 writer.Header().Set("Content-Encoding", "gzip") 83 writer.Header().Set("Content-Length", FileSize) 84 85 f.Seek(0, 0) 86 io.Copy(writer, f) 87 return 88 } 89 90 func TestHTTPGetterTarDownload(t *testing.T) { 91 h := &TestFileHandler{} 92 server := httptest.NewServer(h) 93 defer server.Close() 94 95 g, err := newHTTPGetter(server.URL, "", "", "") 96 if err != nil { 97 t.Fatal(err) 98 } 99 100 data, _ := g.Get(server.URL) 101 mimeType := http.DetectContentType(data.Bytes()) 102 103 expectedMimeType := "application/x-gzip" 104 if mimeType != expectedMimeType { 105 t.Fatalf("Expected response with MIME type %s, but got %s", expectedMimeType, mimeType) 106 } 107 }