-
Notifications
You must be signed in to change notification settings - Fork 27
/
handler_test.go
62 lines (49 loc) · 1.33 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
59
60
61
62
package live
import (
"context"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
type Tester struct {
*BaseHandler
}
func TestHandler(t *testing.T) {
output := `<html _l00=""><head _l000=""></head><body _l001="" live-rendered="">test</body></html>`
h := &Tester{NewHandler()}
h.HandleRender(func(ctx context.Context, data *RenderContext) (io.Reader, error) {
return strings.NewReader(output), nil
})
e := NewHttpHandler(NewTestStore("test"), h)
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
ctx := httpContext(rr, req)
e.get(ctx, 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 := &Tester{NewHandler()}
e := NewHttpHandler(NewTestStore("test"), h)
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
ctx := httpContext(rr, req)
e.get(ctx, rr, req)
if rr.Code != http.StatusInternalServerError {
t.Errorf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusInternalServerError)
return
}
}