-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_mojito_test.go
58 lines (45 loc) · 1.36 KB
/
context_mojito_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 mojito
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/go-mojito/mojito/pkg/router"
)
func Test_MojitoContext_Status(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
resp := httptest.NewRecorder()
router, _ := router.NewContextFromStdlib(resp, req)
ctx := &mojitoContext{router}
ctx.Status(http.StatusTeapot)
if resp.Code != http.StatusTeapot {
t.Errorf("Expected status code %d, got %d", http.StatusTeapot, resp.Code)
}
}
func Test_MojitoContext_JSON(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
resp := httptest.NewRecorder()
router, _ := router.NewContextFromStdlib(resp, req)
ctx := &mojitoContext{router}
body := map[string]string{"hello": "world"}
err := ctx.JSON(body)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if resp.Body.String() != `{"hello":"world"}` {
t.Errorf("Expected response body %s, got %s", `{"hello":"world"}`, resp.Body.String())
}
}
func Test_MojitoContext_String(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
resp := httptest.NewRecorder()
router, _ := router.NewContextFromStdlib(resp, req)
ctx := &mojitoContext{router}
body := "hello world"
err := ctx.String(body)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if resp.Body.String() != body {
t.Errorf("Expected response body %s, got %s", body, resp.Body.String())
}
}