forked from wellington/wellington
/
http_test.go
161 lines (136 loc) · 3.42 KB
/
http_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package wellington
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
libsass "github.com/wellington/go-libsass"
)
func decResp(t *testing.T, r io.Reader) Response {
dec := json.NewDecoder(r)
var resp Response
err := dec.Decode(&resp)
if err != nil {
t.Errorf("decodeHTTPResponse: %s", err)
}
return resp
}
func TestFileHandler(t *testing.T) {
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("TempDir: %v", err)
}
e := "Hello world"
if err := ioutil.WriteFile(filepath.Join(tempDir, "foo.txt"),
[]byte(e), 0644); err != nil {
t.Fatalf("WriteFile: %v", err)
}
ts := httptest.NewServer(FileHandler(tempDir))
defer ts.Close()
get := func(suffix string) string {
res, err := http.Get(ts.URL + suffix)
if err != nil {
t.Fatalf("Get %s: %v", suffix, err)
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("ReadAll %s: %v", suffix, err)
}
res.Body.Close()
return string(b)
}
if s := get("/build/foo.txt"); e != s {
t.Fatalf("got %q want %q", s, e)
}
}
func TestHTTPHandler(t *testing.T) {
ctx := libsass.NewContext()
hh := http.HandlerFunc(HTTPHandler(ctx))
req, err := http.NewRequest("GET", "", nil)
req.Header.Set("Origin", "http://foo.com")
if err != nil {
t.Error(err)
}
w := httptest.NewRecorder()
hh.ServeHTTP(w, req)
if e := 200; w.Code != e {
t.Errorf("got: %d wanted: %d", w.Code, e)
}
resp := decResp(t, w.Body)
if e := "input is empty"; resp.Error != e {
t.Errorf("got: %s wanted: %s", resp, e)
}
req, err = http.NewRequest("GET", "",
bytes.NewBufferString(`div { p { color: red; } }`))
if err != nil {
t.Error(err)
}
w.Body.Reset()
hh.ServeHTTP(w, req)
if e := 200; w.Code != e {
t.Errorf("got: %d wanted: %d", w.Code, e)
}
e := `div p {
color: red; }
`
resp = decResp(t, w.Body)
if resp.Contents != e {
t.Errorf("got: %s wanted: %s", resp.Contents, e)
}
ehead := map[string][]string{
"Access-Control-Allow-Origin": []string{"http://foo.com"},
"Access-Control-Allow-Methods": []string{"POST, GET, OPTIONS, PUT, DELETE"},
"Access-Control-Allow-Headers": []string{"Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token"},
"Access-Control-Allow-Credentials": []string{"true"},
}
for i, h := range w.Header() {
if ehead[i][0] != h[0] {
t.Errorf("got:\n%q\nwanted:\n%q", h, ehead[i])
}
}
}
func TestHTTPHandler_error(t *testing.T) {
ctx := libsass.NewContext()
hh := http.HandlerFunc(HTTPHandler(ctx))
// nil causes panic, is this a problem?
req, err := http.NewRequest("GET", "",
bytes.NewBufferString(`div { p { color: darken(); } };`))
if err != nil {
t.Error(err)
}
w := httptest.NewRecorder()
hh.ServeHTTP(w, req)
if e := 200; w.Code != e {
t.Errorf("got: %d wanted: %d", w.Code, e)
}
e := `Error > stdin:1
required parameter $color is missing in call to function darken
div { p { color: darken(); } };
`
resp := decResp(t, w.Body)
if resp.Error != e {
t.Errorf("got:\n%s\nwanted:\n%s", resp.Error, e)
}
req, err = http.NewRequest("GET", "",
bytes.NewBufferString(`div { p { color: red; } }`))
if err != nil {
t.Error(err)
}
w.Body.Reset()
hh.ServeHTTP(w, req)
if e := 200; w.Code != e {
t.Errorf("got: %d wanted: %d", w.Code, e)
}
e = `div p {
color: red; }
`
resp = decResp(t, w.Body)
if resp.Contents != e {
t.Errorf("got:\n%s\nwanted:\n%s", resp.Contents, e)
}
// Second run shouldn't have an error in it
}