github.com/uber/kraken@v0.1.4/lib/backend/httpbackend/http_test.go (about) 1 // Copyright (c) 2016-2019 Uber Technologies, Inc. 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 package httpbackend 15 16 import ( 17 "bytes" 18 "io" 19 "net/http" 20 "testing" 21 22 "github.com/uber/kraken/core" 23 "github.com/uber/kraken/lib/backend/backenderrors" 24 "github.com/uber/kraken/utils/memsize" 25 "github.com/uber/kraken/utils/randutil" 26 "github.com/uber/kraken/utils/testutil" 27 28 "github.com/pressly/chi" 29 "github.com/stretchr/testify/require" 30 ) 31 32 func TestClientFactory(t *testing.T) { 33 require := require.New(t) 34 35 config := Config{} 36 f := factory{} 37 _, err := f.Create(config, nil) 38 require.NoError(err) 39 } 40 41 func TestHttpDownloadSuccess(t *testing.T) { 42 require := require.New(t) 43 44 blob := randutil.Blob(32 * memsize.KB) 45 46 r := chi.NewRouter() 47 r.Get("/data/{blob}", func(w http.ResponseWriter, req *http.Request) { 48 _, err := io.Copy(w, bytes.NewReader(blob)) 49 require.NoError(err) 50 }) 51 addr, stop := testutil.StartServer(r) 52 defer stop() 53 54 config := Config{DownloadURL: "http://" + addr + "/data/%s"} 55 client, err := NewClient(config) 56 require.NoError(err) 57 58 var b bytes.Buffer 59 require.NoError(client.Download(core.NamespaceFixture(), "data", &b)) 60 require.Equal(blob, b.Bytes()) 61 } 62 63 func TestHttpDownloadFileNotFound(t *testing.T) { 64 require := require.New(t) 65 66 r := chi.NewRouter() 67 r.Get("/data/{blob}", func(w http.ResponseWriter, req *http.Request) { 68 w.WriteHeader(http.StatusNotFound) 69 w.Write([]byte("file not found")) 70 }) 71 addr, stop := testutil.StartServer(r) 72 defer stop() 73 74 config := Config{DownloadURL: "http://" + addr + "/data/%s"} 75 client, err := NewClient(config) 76 require.NoError(err) 77 78 var b bytes.Buffer 79 require.Equal(backenderrors.ErrBlobNotFound, client.Download(core.NamespaceFixture(), "data", &b)) 80 } 81 82 func TestDownloadMalformedURLThrowsError(t *testing.T) { 83 require := require.New(t) 84 85 // Empty router. 86 addr, stop := testutil.StartServer(chi.NewRouter()) 87 defer stop() 88 89 config := Config{DownloadURL: "http://" + addr + "/data"} 90 client, err := NewClient(config) 91 require.NoError(err) 92 93 var b bytes.Buffer 94 require.Error(client.Download(core.NamespaceFixture(), "data", &b)) 95 }