go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/config/helpers_test.go (about) 1 // Copyright 2023 The LUCI Authors. 2 // 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 package config 16 17 import ( 18 "bytes" 19 "context" 20 "fmt" 21 "net/http" 22 "net/http/httptest" 23 "testing" 24 25 "github.com/klauspost/compress/gzip" 26 27 . "github.com/smartystreets/goconvey/convey" 28 . "go.chromium.org/luci/common/testing/assertions" 29 ) 30 31 func TestHelpers(t *testing.T) { 32 t.Parallel() 33 34 Convey("DownloadConfigFromSignedURL", t, func() { 35 ctx := context.Background() 36 37 Convey("nil client", func() { 38 content, err := DownloadConfigFromSignedURL(ctx, nil, "") 39 So(content, ShouldBeNil) 40 So(err, ShouldErrLike, "http client is nil") 41 }) 42 43 Convey("empty signed url", func() { 44 content, err := DownloadConfigFromSignedURL(ctx, http.DefaultClient, "") 45 So(content, ShouldBeNil) 46 So(err, ShouldErrLike, "empty signedURL") 47 }) 48 49 Convey("ok - compressed response", func(c C) { 50 fakeSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 51 c.So(r.Header.Get("Accept-Encoding"), ShouldEqual, "gzip") 52 buf := &bytes.Buffer{} 53 gw := gzip.NewWriter(buf) 54 _, err := gw.Write([]byte("config content")) 55 c.So(err, ShouldBeNil) 56 c.So(gw.Close(), ShouldBeNil) 57 w.Header().Set("Content-Encoding", "gzip") 58 _, err = w.Write(buf.Bytes()) 59 c.So(err, ShouldBeNil) 60 })) 61 defer fakeSrv.Close() 62 63 content, err := DownloadConfigFromSignedURL(ctx, http.DefaultClient, fakeSrv.URL) 64 So(err, ShouldBeNil) 65 So(content, ShouldEqual, []byte("config content")) 66 }) 67 68 Convey("ok - uncompressed response", func(c C) { 69 fakeSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 70 c.So(r.Header.Get("Accept-Encoding"), ShouldEqual, "gzip") 71 fmt.Fprint(w, "config content") 72 })) 73 defer fakeSrv.Close() 74 75 content, err := DownloadConfigFromSignedURL(ctx, http.DefaultClient, fakeSrv.URL) 76 So(err, ShouldBeNil) 77 So(content, ShouldEqual, []byte("config content")) 78 }) 79 80 Convey("http error", func() { 81 fakeSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 82 w.WriteHeader(http.StatusInternalServerError) 83 fmt.Fprint(w, "internal error") 84 })) 85 defer fakeSrv.Close() 86 87 content, err := DownloadConfigFromSignedURL(ctx, http.DefaultClient, fakeSrv.URL) 88 So(content, ShouldBeNil) 89 So(err, ShouldErrLike, `failed to download file, got http response code: 500, body: "internal error"`) 90 }) 91 }) 92 }