-
Notifications
You must be signed in to change notification settings - Fork 6
/
static_files_test.go
63 lines (51 loc) · 1.36 KB
/
static_files_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) 2016, Janoš Guljaš <janos@resenje.org>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package web
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"testing"
)
func TestNewStaticFilesHandler(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping on windows")
}
dir := t.TempDir()
content := "file content"
f, err := os.CreateTemp(dir, "")
if err != nil {
t.Error(err)
}
_, err = f.WriteString(content)
if err != nil {
t.Error(err)
}
_, fn := filepath.Split(f.Name())
f.Close()
r := httptest.NewRequest("", "/static/"+fn, nil)
w := httptest.NewRecorder()
NewStaticFilesHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Error("unexpected call to handler")
}), "/static", http.Dir(dir)).ServeHTTP(w, r)
body := w.Body.String()
if body != content {
t.Errorf("expected content %q, got %q", content, body)
}
}
func TestNewStaticFilesHandlerMissingFile(t *testing.T) {
dir := t.TempDir()
called := false
r := httptest.NewRequest("", "/static/no-file", nil)
w := httptest.NewRecorder()
NewStaticFilesHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
}), "/static", http.Dir(dir)).ServeHTTP(w, r)
if !called {
t.Error("handler was not called")
}
}