github.com/suedadam/up@v0.1.12/http/static/static_test.go (about) 1 package static 2 3 import ( 4 "net/http/httptest" 5 "os" 6 "testing" 7 8 "github.com/tj/assert" 9 "github.com/apex/up" 10 "github.com/apex/up/config" 11 ) 12 13 func TestStatic_defaults(t *testing.T) { 14 os.Chdir("testdata") 15 defer os.Chdir("..") 16 17 c := &up.Config{} 18 assert.NoError(t, c.Default(), "default") 19 assert.NoError(t, c.Validate(), "validate") 20 test(t, c) 21 } 22 23 func TestStatic_dir(t *testing.T) { 24 c := &up.Config{ 25 Static: config.Static{ 26 Dir: "testdata", 27 }, 28 } 29 30 assert.NoError(t, c.Default(), "default") 31 assert.NoError(t, c.Validate(), "validate") 32 test(t, c) 33 } 34 35 func test(t *testing.T, c *up.Config) { 36 h := New(c) 37 38 t.Run("index.html", func(t *testing.T) { 39 res := httptest.NewRecorder() 40 req := httptest.NewRequest("GET", "/", nil) 41 42 h.ServeHTTP(res, req) 43 44 assert.Equal(t, 200, res.Code) 45 assert.Equal(t, "text/html; charset=utf-8", res.Header().Get("Content-Type")) 46 assert.Equal(t, "Index HTML\n", res.Body.String()) 47 }) 48 49 t.Run("file", func(t *testing.T) { 50 res := httptest.NewRecorder() 51 req := httptest.NewRequest("GET", "/style.css", nil) 52 53 h.ServeHTTP(res, req) 54 55 assert.Equal(t, 200, res.Code) 56 assert.Equal(t, "text/css; charset=utf-8", res.Header().Get("Content-Type")) 57 assert.Equal(t, "body { background: whatever }\n", res.Body.String()) 58 }) 59 60 t.Run("missing", func(t *testing.T) { 61 res := httptest.NewRecorder() 62 req := httptest.NewRequest("GET", "/notfound", nil) 63 64 h.ServeHTTP(res, req) 65 66 assert.Equal(t, 404, res.Code) 67 assert.Equal(t, "text/plain; charset=utf-8", res.Header().Get("Content-Type")) 68 assert.Equal(t, "404 page not found\n", res.Body.String()) 69 }) 70 71 t.Run("conditional get", func(t *testing.T) { 72 res := httptest.NewRecorder() 73 req := httptest.NewRequest("GET", "/style.css", nil) 74 req.Header.Set("If-Modified-Since", "Thu, 27 Jul 2030 03:30:31 GMT") 75 h.ServeHTTP(res, req) 76 assert.Equal(t, 304, res.Code) 77 assert.Equal(t, "", res.Header().Get("Content-Length")) 78 assert.Equal(t, "", res.Body.String()) 79 }) 80 }