github.com/blend/go-sdk@v1.20220411.3/web/static_file_server_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package web 9 10 import ( 11 "bytes" 12 "fmt" 13 "net/http" 14 "testing" 15 16 "github.com/blend/go-sdk/assert" 17 "github.com/blend/go-sdk/uuid" 18 "github.com/blend/go-sdk/webutil" 19 ) 20 21 func TestStaticFileserver(t *testing.T) { 22 assert := assert.New(t) 23 24 cfs := NewStaticFileServer( 25 OptStaticFileServerSearchPaths(http.Dir("testdata")), 26 ) 27 buffer := bytes.NewBuffer(nil) 28 res := webutil.NewMockResponse(buffer) 29 req := webutil.NewMockRequest("GET", "/test_file.html") 30 result := cfs.Action(NewCtx(res, req, OptCtxRouteParams(RouteParameters{ 31 RouteTokenFilepath: "test_file.html", 32 }))) 33 34 assert.Nil(result) 35 assert.NotEmpty(buffer.Bytes()) 36 } 37 38 func TestStaticFileserverHeaders(t *testing.T) { 39 assert := assert.New(t) 40 41 cfs := NewStaticFileServer( 42 OptStaticFileServerSearchPaths(http.Dir("testdata")), 43 OptStaticFileServerHeaders(http.Header{"buzz": []string{"fuzz"}}), 44 ) 45 cfs.AddHeader("foo", "bar") 46 assert.NotEmpty(cfs.Headers) 47 48 buffer := new(bytes.Buffer) 49 res := webutil.NewMockResponse(buffer) 50 req := webutil.NewMockRequest("GET", "/test_file.html") 51 result := cfs.Action(NewCtx(res, req, OptCtxRouteParams(RouteParameters{ 52 RouteTokenFilepath: "test_file.html", 53 }))) 54 55 assert.Nil(result) 56 assert.NotEmpty(buffer.Bytes()) 57 58 assert.Equal("bar", res.Header().Get("foo"), "the header should be set on the response") 59 assert.Equal("fuzz", res.Header().Get("buzz"), "the header should be set on the response") 60 } 61 62 func TestStaticFileserverRewriteRule(t *testing.T) { 63 assert := assert.New(t) 64 65 cfs := NewStaticFileServer( 66 OptStaticFileServerSearchPaths(http.Dir("testdata")), 67 ) 68 assert.Nil(cfs.AddRewriteRule(RegexpAssetCacheFiles, func(path string, parts ...string) string { 69 return fmt.Sprintf("%s.%s", parts[1], parts[3]) 70 })) 71 72 buffer := new(bytes.Buffer) 73 res := webutil.NewMockResponse(buffer) 74 req := webutil.NewMockRequest("GET", "/test_file.123123123.html") 75 result := cfs.Action(NewCtx(res, req, OptCtxRouteParams(RouteParameters{ 76 RouteTokenFilepath: "test_file.123123123.html", 77 }))) 78 79 assert.Nil(result) 80 assert.NotEmpty(buffer.Bytes(), "we should still have reached the file") 81 } 82 83 func TestStaticFileserverNotFound(t *testing.T) { 84 assert := assert.New(t) 85 86 cfs := NewStaticFileServer( 87 OptStaticFileServerSearchPaths(http.Dir("testdata")), 88 ) 89 buffer := new(bytes.Buffer) 90 res := webutil.NewMockResponse(buffer) 91 req := webutil.NewMockRequest("GET", "/"+uuid.V4().String()) 92 result := cfs.Action(NewCtx(res, req, OptCtxRouteParams(RouteParameters{ 93 RouteTokenFilepath: req.URL.Path, 94 }))) 95 96 assert.Nil(result) 97 assert.Equal(http.StatusNotFound, res.StatusCode()) 98 assert.NotEmpty(buffer.Bytes()) 99 } 100 101 func TestStaticFileserverNotFoundDefaultProvider(t *testing.T) { 102 assert := assert.New(t) 103 104 cfs := NewStaticFileServer( 105 OptStaticFileServerSearchPaths(http.Dir("testdata")), 106 ) 107 buffer := new(bytes.Buffer) 108 res := webutil.NewMockResponse(buffer) 109 req := webutil.NewMockRequest("GET", "/"+uuid.V4().String()) 110 result := cfs.Action(NewCtx(res, req, 111 OptCtxRouteParams(RouteParameters{RouteTokenFilepath: req.URL.Path}), 112 OptCtxDefaultProvider(JSON), 113 )) 114 115 assert.NotNil(result) 116 typed, ok := result.(*JSONResult) 117 assert.True(ok) 118 assert.NotNil(typed) 119 assert.Equal(http.StatusNotFound, typed.StatusCode) 120 } 121 122 func TestStaticFileserverLive(t *testing.T) { 123 assert := assert.New(t) 124 125 cfs := NewStaticFileServer( 126 OptStaticFileServerSearchPaths(http.Dir("testdata")), 127 ) 128 cfs.CacheDisabled = true 129 buffer := new(bytes.Buffer) 130 res := webutil.NewMockResponse(buffer) 131 req := webutil.NewMockRequest("GET", "/test_file.html") 132 result := cfs.Action(NewCtx(res, req, OptCtxRouteParams(RouteParameters{ 133 RouteTokenFilepath: req.URL.Path, 134 }))) 135 136 assert.Nil(result) 137 assert.Equal(http.StatusOK, res.StatusCode()) 138 assert.NotEmpty(buffer.Bytes()) 139 } 140 141 func TestStaticFileserverLiveNotFound(t *testing.T) { 142 assert := assert.New(t) 143 144 cfs := NewStaticFileServer( 145 OptStaticFileServerSearchPaths(http.Dir("testdata")), 146 OptStaticFileServerCacheDisabled(true), 147 ) 148 buffer := new(bytes.Buffer) 149 res := webutil.NewMockResponse(buffer) 150 req := webutil.NewMockRequest("GET", "/"+uuid.V4().String()) 151 result := cfs.Action(NewCtx(res, req, OptCtxRouteParams(RouteParameters{ 152 RouteTokenFilepath: req.URL.Path, 153 }))) 154 155 assert.Nil(result) 156 assert.Equal(http.StatusNotFound, res.StatusCode()) 157 assert.NotEmpty(buffer.Bytes()) 158 } 159 160 func TestStaticFileserverCachedNotFoundOnRoot(t *testing.T) { 161 assert := assert.New(t) 162 163 cfs := NewStaticFileServer( 164 OptStaticFileServerSearchPaths(http.Dir("testdata")), 165 OptStaticFileServerCacheDisabled(false), 166 ) 167 buffer := new(bytes.Buffer) 168 res := webutil.NewMockResponse(buffer) 169 req := webutil.NewMockRequest("GET", "/") 170 r := NewCtx(res, req, OptCtxRouteParams(RouteParameters{ 171 RouteTokenFilepath: req.URL.Path, 172 })) 173 r.DefaultProvider = Text 174 result := cfs.Action(r) 175 176 assert.NotNil(result) 177 assert.Nil(result.Render(r)) 178 assert.Equal(http.StatusNotFound, res.StatusCode()) 179 assert.NotEmpty(buffer.Bytes()) 180 } 181 182 func TestStaticFileserverLiveNotFoundOnRoot(t *testing.T) { 183 assert := assert.New(t) 184 185 cfs := NewStaticFileServer( 186 OptStaticFileServerSearchPaths(http.Dir("testdata")), 187 OptStaticFileServerCacheDisabled(true), 188 ) 189 buffer := new(bytes.Buffer) 190 res := webutil.NewMockResponse(buffer) 191 req := webutil.NewMockRequest("GET", "/") 192 r := NewCtx(res, req, OptCtxRouteParams(RouteParameters{ 193 RouteTokenFilepath: req.URL.Path, 194 })) 195 r.DefaultProvider = Text 196 result := cfs.Action(r) 197 198 assert.NotNil(result) 199 assert.Nil(result.Render(r)) 200 assert.Equal(http.StatusNotFound, res.StatusCode()) 201 assert.NotEmpty(buffer.Bytes()) 202 } 203 204 func TestStaticFileserverAddsETag(t *testing.T) { 205 assert := assert.New(t) 206 207 cfs := NewStaticFileServer( 208 OptStaticFileServerSearchPaths(http.Dir("testdata")), 209 OptStaticFileServerCacheDisabled(false), 210 ) 211 buffer := new(bytes.Buffer) 212 res := webutil.NewMockResponse(buffer) 213 req := webutil.NewMockRequest("GET", "/test_file.html") 214 result := cfs.Action(NewCtx(res, req, OptCtxRouteParams(RouteParameters{ 215 RouteTokenFilepath: req.URL.Path, 216 }))) 217 218 assert.Nil(result) 219 assert.Equal(http.StatusOK, res.StatusCode()) 220 assert.NotEmpty(buffer.Bytes()) 221 assert.NotEmpty(res.Header().Get(webutil.HeaderETag)) 222 }