-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathstatic_test.go
110 lines (87 loc) · 2.64 KB
/
static_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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package mango
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
)
func staticTestServer(env Env) (Status, Headers, Body) {
return 200, Headers{"Content-Type": []string{"text/html"}}, Body("<h1>Hello World!</h1>")
}
func TestStaticSuccess(t *testing.T) {
// Compile the stack
staticStack := new(Stack)
staticStack.Middleware(Static("./static"))
staticApp := staticStack.Compile(staticTestServer)
// Request against it
request, err := http.NewRequest("GET", "http://localhost:3000/static.html", nil)
status, headers, body := staticApp(Env{"mango.request": &Request{request}})
if err != nil {
t.Error(err)
}
if status != 200 {
t.Error("Expected status to equal 200, got:", status)
}
expected := "<h1>I'm a static test file</h1>\n"
if string(body) != expected {
t.Error("Expected body:", string(body), "to equal:", expected)
}
expected = "text/html"
got := headers.Get("Content-Type")
if got != expected {
t.Error("Expected Content-Type:", got, "to equal:", expected)
}
}
func TestStaticFail(t *testing.T) {
// Compile the stack
staticStack := new(Stack)
staticStack.Middleware(Static("./static"))
staticApp := staticStack.Compile(staticTestServer)
// Request against it
request, err := http.NewRequest("GET", "http://localhost:3000/not_a_file.html", nil)
status, _, body := staticApp(Env{"mango.request": &Request{request}})
if err != nil {
t.Error(err)
}
if status != 200 {
t.Error("Expected status to equal 200, got:", status)
}
expected := "<h1>Hello World!</h1>"
if string(body) != expected {
t.Error("Expected body:", string(body), "to equal:", expected)
}
}
func TestStaticBinaryFile(t *testing.T) {
// Compile the stack
staticStack := new(Stack)
staticStack.Middleware(Static("./static"))
staticApp := staticStack.Compile(staticTestServer)
// Request against it
request, err := http.NewRequest("GET", "http://localhost:3000/binary_file.png", nil)
status, _, body := staticApp(Env{"mango.request": &Request{request}})
if err != nil {
t.Error(err)
}
if status != 200 {
t.Error("Expected status to equal 200, got:", status)
}
expected, err := ioutil.ReadFile("./static/binary_file.png")
if err != nil {
t.Error(err)
}
if bytes.Compare([]byte(body), []byte(expected)) != 0 {
t.Error("Expected body to equal ./static/binary_file.png")
}
}
func BenchmarkStatic(b *testing.B) {
b.StopTimer()
staticStack := new(Stack)
staticStack.Middleware(Static("./static"))
staticApp := staticStack.Compile(staticTestServer)
request, _ := http.NewRequest("GET", "http://localhost:3000/static.html", nil)
b.StartTimer()
for i := 0; i < b.N; i++ {
staticApp(Env{"mango.request": &Request{request}})
}
b.StopTimer()
}