github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/apiserver/static_test.go (about) 1 // Copyright © 2021 Kaleido, Inc. 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package apiserver 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 "net/http" 23 "net/http/httptest" 24 "os" 25 "testing" 26 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestStaticHosting(t *testing.T) { 31 sc := newStaticHandler(configDir, "index.html", "/config") 32 var handler http.HandlerFunc = sc.ServeHTTP 33 res := httptest.NewRecorder() 34 req := httptest.NewRequest("GET", "/config/firefly.core.yaml", nil) 35 handler(res, req) 36 assert.Equal(t, 200, res.Result().StatusCode) 37 b, err := ioutil.ReadAll(res.Body) 38 assert.NoError(t, err) 39 assert.NotEmpty(t, b) 40 41 } 42 43 func Test404UnRelablePath(t *testing.T) { 44 sc := newStaticHandler("test", "firefly.core.yaml", "test") 45 var handler http.HandlerFunc = sc.ServeHTTP 46 res := httptest.NewRecorder() 47 req := httptest.NewRequest("GET", "/test", nil) 48 handler(res, req) 49 assert.Equal(t, 404, res.Result().StatusCode) 50 } 51 52 func TestServeDefault(t *testing.T) { 53 sc := newStaticHandler(configDir, "firefly.core.yaml", "/config") 54 var handler http.HandlerFunc = sc.ServeHTTP 55 res := httptest.NewRecorder() 56 req := httptest.NewRequest("GET", "/config", nil) 57 handler(res, req) 58 assert.Equal(t, 200, res.Result().StatusCode) 59 b, err := ioutil.ReadAll(res.Body) 60 assert.NoError(t, err) 61 assert.NotEmpty(t, b) 62 } 63 64 func TestServeNotFoundServeDefault(t *testing.T) { 65 sc := newStaticHandler(configDir, "firefly.core.yaml", "/config") 66 var handler http.HandlerFunc = sc.ServeHTTP 67 res := httptest.NewRecorder() 68 req := httptest.NewRequest("GET", "/config/wrong", nil) 69 handler(res, req) 70 assert.Equal(t, 200, res.Result().StatusCode) 71 } 72 73 type fakeOS struct{} 74 75 func (f *fakeOS) Stat(name string) (os.FileInfo, error) { 76 return nil, fmt.Errorf("pop") 77 } 78 79 func TestStatError(t *testing.T) { 80 81 sc := &staticHandler{ 82 staticPath: "../../test", 83 urlPrefix: "/test", 84 indexPath: "firefly.core.yaml", 85 os: &fakeOS{}, 86 } 87 var handler http.HandlerFunc = sc.ServeHTTP 88 res := httptest.NewRecorder() 89 req := httptest.NewRequest("GET", "/test/config", nil) 90 handler(res, req) 91 assert.Equal(t, 500, res.Result().StatusCode) 92 b, err := ioutil.ReadAll(res.Body) 93 assert.NoError(t, err) 94 assert.Regexp(t, "FF10185", string(b)) 95 }