github.com/orangenpresse/up@v0.6.0/http/static/static_test.go (about) 1 package static 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/httptest" 7 "os" 8 "testing" 9 10 "github.com/apex/up" 11 "github.com/apex/up/config" 12 "github.com/tj/assert" 13 ) 14 15 func TestStatic_defaults(t *testing.T) { 16 os.Chdir("testdata/static") 17 defer os.Chdir("../..") 18 19 c := &up.Config{Name: "app", Type: "static"} 20 assert.NoError(t, c.Default(), "default") 21 assert.NoError(t, c.Validate(), "validate") 22 test(t, c) 23 } 24 25 func TestStatic_dir(t *testing.T) { 26 c := &up.Config{ 27 Name: "app", 28 Type: "static", 29 Static: config.Static{ 30 Dir: "testdata/static", 31 }, 32 } 33 34 assert.NoError(t, c.Default(), "default") 35 assert.NoError(t, c.Validate(), "validate") 36 test(t, c) 37 } 38 39 func test(t *testing.T, c *up.Config) { 40 h := New(c) 41 42 t.Run("index.html", func(t *testing.T) { 43 res := httptest.NewRecorder() 44 req := httptest.NewRequest("GET", "/", nil) 45 46 h.ServeHTTP(res, req) 47 48 assert.Equal(t, 200, res.Code) 49 assert.Equal(t, "text/html; charset=utf-8", res.Header().Get("Content-Type")) 50 assert.Equal(t, "Index HTML\n", res.Body.String()) 51 }) 52 53 t.Run("file", func(t *testing.T) { 54 res := httptest.NewRecorder() 55 req := httptest.NewRequest("GET", "/style.css", nil) 56 57 h.ServeHTTP(res, req) 58 59 assert.Equal(t, 200, res.Code) 60 assert.Equal(t, "text/css; charset=utf-8", res.Header().Get("Content-Type")) 61 assert.Equal(t, "body { background: whatever }\n", res.Body.String()) 62 }) 63 64 t.Run("missing", func(t *testing.T) { 65 res := httptest.NewRecorder() 66 req := httptest.NewRequest("GET", "/notfound", nil) 67 68 h.ServeHTTP(res, req) 69 70 assert.Equal(t, 404, res.Code) 71 assert.Equal(t, "text/plain; charset=utf-8", res.Header().Get("Content-Type")) 72 assert.Equal(t, "404 page not found\n", res.Body.String()) 73 }) 74 75 t.Run("conditional get", func(t *testing.T) { 76 res := httptest.NewRecorder() 77 req := httptest.NewRequest("GET", "/style.css", nil) 78 req.Header.Set("If-Modified-Since", "Thu, 27 Jul 2030 03:30:31 GMT") 79 h.ServeHTTP(res, req) 80 assert.Equal(t, 304, res.Code) 81 assert.Equal(t, "", res.Header().Get("Content-Length")) 82 assert.Equal(t, "", res.Body.String()) 83 }) 84 } 85 86 func TestStatic_dynamic(t *testing.T) { 87 c := &up.Config{ 88 Name: "app", 89 Static: config.Static{ 90 Dir: "testdata/dynamic/public", 91 }, 92 } 93 94 assert.NoError(t, c.Default(), "default") 95 assert.NoError(t, c.Validate(), "validate") 96 97 h := NewDynamic(c, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 98 fmt.Fprintln(w, ":)") 99 })) 100 101 t.Run("file", func(t *testing.T) { 102 res := httptest.NewRecorder() 103 req := httptest.NewRequest("GET", "/css/style.css", nil) 104 105 h.ServeHTTP(res, req) 106 107 assert.Equal(t, 200, res.Code) 108 assert.Equal(t, "text/css; charset=utf-8", res.Header().Get("Content-Type")) 109 assert.Equal(t, "body { background: whatever }\n", res.Body.String()) 110 }) 111 112 t.Run("missing", func(t *testing.T) { 113 res := httptest.NewRecorder() 114 req := httptest.NewRequest("GET", "/notfound", nil) 115 116 h.ServeHTTP(res, req) 117 118 assert.Equal(t, 200, res.Code) 119 assert.Equal(t, "text/plain; charset=utf-8", res.Header().Get("Content-Type")) 120 assert.Equal(t, ":)\n", res.Body.String()) 121 }) 122 } 123 124 func TestStatic_dynamicPrefix(t *testing.T) { 125 c := &up.Config{ 126 Name: "app", 127 Static: config.Static{ 128 Dir: "testdata/dynamic/public", 129 Prefix: "/public", 130 }, 131 } 132 133 assert.NoError(t, c.Default(), "default") 134 assert.NoError(t, c.Validate(), "validate") 135 136 h := NewDynamic(c, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 137 fmt.Fprintln(w, ":)") 138 })) 139 140 t.Run("/", func(t *testing.T) { 141 res := httptest.NewRecorder() 142 req := httptest.NewRequest("GET", "/index.html", nil) 143 144 h.ServeHTTP(res, req) 145 146 assert.Equal(t, 200, res.Code) 147 assert.Equal(t, ":)\n", res.Body.String()) 148 }) 149 150 t.Run("file", func(t *testing.T) { 151 res := httptest.NewRecorder() 152 req := httptest.NewRequest("GET", "/public/css/style.css", nil) 153 154 h.ServeHTTP(res, req) 155 156 assert.Equal(t, 200, res.Code) 157 assert.Equal(t, "text/css; charset=utf-8", res.Header().Get("Content-Type")) 158 assert.Equal(t, "body { background: whatever }\n", res.Body.String()) 159 }) 160 161 t.Run("missing", func(t *testing.T) { 162 res := httptest.NewRecorder() 163 req := httptest.NewRequest("GET", "/public/notfound", nil) 164 165 h.ServeHTTP(res, req) 166 167 assert.Equal(t, 200, res.Code) 168 assert.Equal(t, ":)\n", res.Body.String()) 169 }) 170 } 171 172 func TestNormalizePrefix(t *testing.T) { 173 assert.Equal(t, `/public/`, normalizePrefix(`public`)) 174 assert.Equal(t, `/public/`, normalizePrefix(`public/`)) 175 assert.Equal(t, `/public/`, normalizePrefix(`/public`)) 176 assert.Equal(t, `/public/`, normalizePrefix(`/public/`)) 177 }