summaryrefslogtreecommitdiff
path: root/context_test.go
blob: 24b49e416f0b1c8b6dd7d95894222011b296d07d (plain)
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
162
163
164
package growel

import (
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

func TestContextPlain(t *testing.T) {
	w := httptest.NewRecorder()
	c := &Context{W: w}

	c.Plain(201, "ok")

	if w.Code != 201 {
		t.Fatalf("expected 201, got %d", w.Code)
	}
	if ct := w.Header().Get("Content-Type"); !strings.Contains(ct, "text/plain") {
		t.Fatalf("wrong content-type: %s", ct)
	}
	if w.Body.String() != "ok" {
		t.Fatalf("wrong body: %s", w.Body.String())
	}
}

func TestContextJSON(t *testing.T) {
	w := httptest.NewRecorder()
	c := &Context{W: w}

	c.JSON(200, map[string]string{"x": "y"})

	if w.Code != 200 {
		t.Fatalf("expected 200, got %d", w.Code)
	}
	if ct := w.Header().Get("Content-Type"); !strings.Contains(ct, "application/json") {
		t.Fatalf("unexpected content-type: %s", ct)
	}
	if !strings.Contains(w.Body.String(), `"x":"y"`) {
		t.Fatalf("wrong json: %s", w.Body.String())
	}
}

func TestContextError(t *testing.T) {
	w := httptest.NewRecorder()
	c := &Context{W: w}

	c.Error(http.StatusBadRequest, "bad input")

	if w.Code != 400 {
		t.Fatalf("expected 400, got %d", w.Code)
	}

	body := w.Body.String()
	if !strings.Contains(body, `"error":"Bad Request"`) {
		t.Fatalf("missing human readable status: %s", body)
	}
	if !strings.Contains(body, `"message":"bad input"`) {
		t.Fatalf("missing message: %s", body)
	}
	if !strings.Contains(body, `"status":"400"`) {
		t.Fatalf("missing status field: %s", body)
	}
}

func TestContextCookies(t *testing.T) {
	r := httptest.NewRequest("GET", "/", nil)
	r.AddCookie(&http.Cookie{Name: "a", Value: "1"})
	r.AddCookie(&http.Cookie{Name: "b", Value: "2"})

	c := &Context{R: r}

	cookies := c.Cookies()
	if len(cookies) != 2 {
		t.Fatalf("expected 2 cookies, got %d", len(cookies))
	}
}

func TestContextCookieFound(t *testing.T) {
	r := httptest.NewRequest("GET", "/", nil)
	r.AddCookie(&http.Cookie{Name: "token", Value: "xyz"})

	c := &Context{R: r}

	ck := c.Cookie("token")
	if ck == nil {
		t.Fatalf("expected cookie token, got nil")
	}
	if ck.Value != "xyz" {
		t.Fatalf("expected value xyz, got %s", ck.Value)
	}
}

func TestContextCookieNotFound(t *testing.T) {
	r := httptest.NewRequest("GET", "/", nil)
	r.AddCookie(&http.Cookie{Name: "session", Value: "abc"})

	c := &Context{R: r}

	ck := c.Cookie("missing")
	if ck != nil {
		t.Fatalf("expected nil, got %#v", ck)
	}
}

func TestContextSetCompleteCookie(t *testing.T) {
	w := httptest.NewRecorder()
	c := &Context{W: w}

	cookie := &http.Cookie{
		Name:  "x",
		Value: "y",
		Path:  "/",
	}

	c.SetCompleteCookie(cookie)

	res := w.Result()
	cs := res.Cookies()

	if len(cs) != 1 {
		t.Fatalf("expected 1 cookie, got %d", len(cs))
	}

	ck := cs[0]
	if ck.Name != "x" || ck.Value != "y" {
		t.Fatalf("unexpected cookie: %#v", ck)
	}
}

func TestContextSetCookie(t *testing.T) {
	w := httptest.NewRecorder()
	c := &Context{W: w}

	c.SetCookie("uid", "100")

	res := w.Result()
	cs := res.Cookies()

	if len(cs) != 1 {
		t.Fatalf("expected 1 cookie, got %d", len(cs))
	}

	ck := cs[0]

	if ck.Name != "uid" || ck.Value != "100" {
		t.Fatalf("unexpected cookie: %#v", ck)
	}
	if ck.Path != "/" {
		t.Fatalf("wrong Path: %s", ck.Path)
	}
	if ck.MaxAge != 3600 {
		t.Fatalf("wrong MaxAge: %d", ck.MaxAge)
	}
	if !ck.HttpOnly {
		t.Fatalf("HttpOnly should be true")
	}
	if !ck.Secure {
		t.Fatalf("Secure should be true")
	}
	if ck.SameSite != http.SameSiteLaxMode {
		t.Fatalf("wrong SameSite: %v", ck.SameSite)
	}
}