forked from jfyne/live
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
58 lines (48 loc) · 1.24 KB
/
handler_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
package live
import (
"context"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestHandler(t *testing.T) {
output := `<html _l00=""><head _l000=""></head><body _l001="" live-rendered="">test</body></html>`
h, err := NewHandler(NewTestStore("test"))
if err != nil {
t.Fatal(err)
}
h.Render = func(ctx context.Context, data interface{}) (io.Reader, error) {
return strings.NewReader(output), nil
}
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
h.serveHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK)
return
}
if rr.Body.String() != output {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), output)
}
}
func TestHandlerErrorNoRenderer(t *testing.T) {
h, err := NewHandler(NewTestStore("test"))
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
h.serveHTTP(rr, req)
if rr.Code != http.StatusInternalServerError {
t.Errorf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusInternalServerError)
return
}
}