github.com/hhsnopek/up@v0.1.1/handler/handler_test.go (about) 1 package handler 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "os" 7 "testing" 8 "time" 9 10 "github.com/tj/assert" 11 ) 12 13 // TODO: tests integration of each middleware 14 // TODO: tests for POST etc 15 // TODO: tests for larger response bodies 16 17 func TestNode(t *testing.T) { 18 os.Chdir("testdata/node") 19 defer os.Chdir("../..") 20 21 h, err := New() 22 assert.NoError(t, err) 23 24 res := httptest.NewRecorder() 25 req := httptest.NewRequest("GET", "/", nil) 26 27 h.ServeHTTP(res, req) 28 29 actual := res.Header() 30 assert.NotEmpty(t, actual.Get("Date"), "date") 31 actual.Del("Date") 32 33 expected := make(http.Header) 34 expected.Add("X-Powered-By", "up") 35 expected.Add("X-Foo", "bar") 36 expected.Add("Content-Length", "11") 37 expected.Add("Content-Type", "text/plain; charset=utf-8") 38 assert.Equal(t, expected, actual) 39 } 40 41 func TestStatic(t *testing.T) { 42 os.Chdir("testdata/static") 43 defer os.Chdir("../..") 44 45 h, err := New() 46 assert.NoError(t, err) 47 48 res := httptest.NewRecorder() 49 req := httptest.NewRequest("GET", "/", nil) 50 51 start := time.Now() 52 h.ServeHTTP(res, req) 53 t.Logf("latency = %s", time.Since(start)) 54 55 actual := res.Header() 56 assert.NotEmpty(t, actual.Get("Last-Modified"), "last-modified") 57 actual.Del("Last-Modified") 58 59 expected := make(http.Header) 60 expected.Add("X-Powered-By", "up") 61 expected.Add("Content-Length", "12") 62 expected.Add("Content-Type", "text/html; charset=utf-8") 63 expected.Add("Accept-Ranges", "bytes") 64 65 assert.Equal(t, expected, actual) 66 } 67 68 func TestHandler_conditionalGet(t *testing.T) { 69 os.Chdir("testdata/static") 70 defer os.Chdir("../..") 71 72 h, err := New() 73 assert.NoError(t, err) 74 75 res := httptest.NewRecorder() 76 req := httptest.NewRequest("GET", "/style.css", nil) 77 req.Header.Set("If-Modified-Since", "Thu, 27 Jul 2030 03:30:31 GMT") 78 h.ServeHTTP(res, req) 79 assert.Equal(t, 304, res.Code) 80 assert.Equal(t, "", res.Header().Get("Content-Length")) 81 assert.Equal(t, "", res.Body.String()) 82 } 83 84 func TestHandler_rewrite(t *testing.T) { 85 os.Chdir("testdata/static-rewrites") 86 defer os.Chdir("../..") 87 88 h, err := New() 89 assert.NoError(t, err) 90 91 res := httptest.NewRecorder() 92 req := httptest.NewRequest("GET", "/docs/ping/guides/alerts", nil) 93 h.ServeHTTP(res, req) 94 assert.Equal(t, 200, res.Code) 95 assert.Equal(t, "14", res.Header().Get("Content-Length")) 96 assert.Equal(t, "Alerting docs\n", res.Body.String()) 97 } 98 99 func TestHandler_redirect(t *testing.T) { 100 os.Chdir("testdata/static-redirects") 101 defer os.Chdir("../..") 102 103 h, err := New() 104 assert.NoError(t, err) 105 106 res := httptest.NewRecorder() 107 req := httptest.NewRequest("GET", "/docs/ping/guides/alerts/", nil) 108 h.ServeHTTP(res, req) 109 assert.Equal(t, "/help/ping/alerts", res.Header().Get("Location")) 110 assert.Equal(t, 302, res.Code) 111 assert.Equal(t, "Found\n", res.Body.String()) 112 } 113 114 func TestHandler_spa(t *testing.T) { 115 os.Chdir("testdata/spa") 116 defer os.Chdir("../..") 117 118 h, err := New() 119 assert.NoError(t, err) 120 121 t.Run("index", func(t *testing.T) { 122 res := httptest.NewRecorder() 123 req := httptest.NewRequest("GET", "/", nil) 124 h.ServeHTTP(res, req) 125 assert.Equal(t, 200, res.Code) 126 assert.Equal(t, "Index\n", res.Body.String()) 127 }) 128 129 t.Run("redirect", func(t *testing.T) { 130 res := httptest.NewRecorder() 131 req := httptest.NewRequest("GET", "/index.html", nil) 132 h.ServeHTTP(res, req) 133 assert.Equal(t, 301, res.Code) 134 }) 135 136 t.Run("file does not exist", func(t *testing.T) { 137 res := httptest.NewRecorder() 138 req := httptest.NewRequest("GET", "/something/here", nil) 139 h.ServeHTTP(res, req) 140 assert.Equal(t, 200, res.Code) 141 assert.Equal(t, "Index\n", res.Body.String()) 142 }) 143 144 t.Run("file exists", func(t *testing.T) { 145 res := httptest.NewRecorder() 146 req := httptest.NewRequest("GET", "/app.js", nil) 147 h.ServeHTTP(res, req) 148 assert.Equal(t, 200, res.Code) 149 assert.Equal(t, "app js\n", res.Body.String()) 150 }) 151 152 t.Run("file exists nested", func(t *testing.T) { 153 res := httptest.NewRecorder() 154 req := httptest.NewRequest("GET", "/css/bar.css", nil) 155 h.ServeHTTP(res, req) 156 assert.Equal(t, 200, res.Code) 157 assert.Equal(t, "bar css\n", res.Body.String()) 158 }) 159 } 160 161 func BenchmarkHandler(b *testing.B) { 162 b.ReportAllocs() 163 164 b.Run("static server", func(b *testing.B) { 165 os.Chdir("testdata/basic") 166 167 h, err := New() 168 assert.NoError(b, err) 169 170 b.ResetTimer() 171 b.SetParallelism(80) 172 173 b.RunParallel(func(pb *testing.PB) { 174 for pb.Next() { 175 res := httptest.NewRecorder() 176 req := httptest.NewRequest("GET", "/", nil) 177 h.ServeHTTP(res, req) 178 } 179 }) 180 }) 181 182 b.Run("node server relay", func(b *testing.B) { 183 os.Chdir("testdata/basic") 184 185 h, err := New() 186 assert.NoError(b, err) 187 188 b.ResetTimer() 189 b.SetParallelism(80) 190 191 b.RunParallel(func(pb *testing.PB) { 192 for pb.Next() { 193 res := httptest.NewRecorder() 194 req := httptest.NewRequest("GET", "/", nil) 195 h.ServeHTTP(res, req) 196 } 197 }) 198 }) 199 }