-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallable_test.go
More file actions
148 lines (125 loc) · 4.94 KB
/
callable_test.go
File metadata and controls
148 lines (125 loc) · 4.94 KB
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
package callable
import (
"context"
"errors"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func headerValues(res *http.Response, key string) []string {
return res.Header[http.CanonicalHeaderKey(key)]
}
type testRequest struct {
Who string `json:"who"`
}
type testResponse struct {
Greeting string `json:"greeting"`
}
func (r *testRequest) Handle(_ context.Context, call Call) (interface{}, error) {
if r.Who == "" {
return nil, Error(NotFound, "nobody to greet")
}
if r.Who == "error" {
return nil, errors.New("some error")
}
if call.IID != "" {
return testResponse{"IID: " + call.IID}, nil
}
return testResponse{"Hello " + r.Who + "!"}, nil
}
func TestCallable_CORS(t *testing.T) {
r := httptest.NewRequest(http.MethodOptions, "/", nil)
w := httptest.NewRecorder()
New(&testRequest{}).ServeHTTP(w, r)
res, body := response(w)
assert.Equal(t, 204, res.StatusCode)
vary := headerValues(res, "Vary")
assert.Contains(t, vary, "Origin")
assert.Contains(t, vary, "Access-Control-Request-Headers")
assert.Empty(t, headerValues(res, "Access-Control-Allow-Origin"))
assert.Empty(t, body)
}
func TestCallable_CORS_Origin(t *testing.T) {
r := httptest.NewRequest(http.MethodOptions, "/", nil)
r.Header.Set("Origin", "http://localhost")
w := httptest.NewRecorder()
New(&testRequest{}).ServeHTTP(w, r)
res, body := response(w)
assert.Equal(t, 204, res.StatusCode)
assert.ElementsMatch(t, []string{"Origin", "Access-Control-Request-Headers"}, headerValues(res, "Vary"))
assert.Equal(t, []string{"http://localhost"}, headerValues(res, "Access-Control-Allow-Origin"))
assert.Empty(t, body)
}
func TestCallable_BadContent(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, "/", nil)
w := httptest.NewRecorder()
New(&testRequest{}).ServeHTTP(w, r)
res, body := response(w)
assert.Equal(t, 400, res.StatusCode)
assert.JSONEq(t, `{"error":{"status":"INVALID_ARGUMENT","message":"missing content type"}}`, body)
}
func TestCallable_BadContent_TextPlain(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("text"))
r.Header.Set("Content-Type", "text/plain")
w := httptest.NewRecorder()
New(&testRequest{}).ServeHTTP(w, r)
res, body := response(w)
assert.Equal(t, 400, res.StatusCode)
assert.JSONEq(t, `{"error":{"status":"INVALID_ARGUMENT","message":"unsupported content type"}}`, body)
}
func TestCallable_BadContent_Charset(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("text"))
r.Header.Set("Content-Type", "application/json; charset=iso-8859-1")
w := httptest.NewRecorder()
New(&testRequest{}).ServeHTTP(w, r)
res, body := response(w)
assert.Equal(t, 400, res.StatusCode)
assert.JSONEq(t, `{"error":{"status":"INVALID_ARGUMENT","message":"unsupported encoding"}}`, body)
}
func TestCallable_Success(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"data":{"who":"World"}}`))
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
New(&testRequest{}).ServeHTTP(w, r)
res, body := response(w)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, "application/json; charset=utf-8", res.Header.Get("Content-Type"))
assert.Equal(t, []string{"Origin"}, headerValues(res, "Vary"))
assert.JSONEq(t, `{"data":{"greeting":"Hello World!"}}`, body)
}
func TestCallable_Error(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"data":{}}`))
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
New(&testRequest{}).ServeHTTP(w, r)
res, body := response(w)
assert.Equal(t, 404, res.StatusCode)
assert.Equal(t, "application/json; charset=utf-8", res.Header.Get("Content-Type"))
assert.Equal(t, []string{"Origin"}, headerValues(res, "Vary"))
assert.JSONEq(t, `{"error":{"status":"NOT_FOUND","message":"nobody to greet"}}`, body)
}
func TestCallable_Error_Internal(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"data":{"who":"error"}}`))
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
New(&testRequest{}).ServeHTTP(w, r)
res, body := response(w)
assert.Equal(t, 500, res.StatusCode)
assert.Equal(t, "application/json; charset=utf-8", res.Header.Get("Content-Type"))
assert.Equal(t, []string{"Origin"}, headerValues(res, "Vary"))
assert.JSONEq(t, `{"error":{"status":"INTERNAL","message":"some error"}}`, body)
}
func TestCallable_IID(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{"data":{"who":"World"}}`))
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Firebase-Instance-ID-Token", "42")
w := httptest.NewRecorder()
New(&testRequest{}).ServeHTTP(w, r)
res, body := response(w)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, "application/json; charset=utf-8", res.Header.Get("Content-Type"))
assert.Equal(t, []string{"Origin"}, headerValues(res, "Vary"))
assert.JSONEq(t, `{"data":{"greeting":"IID: 42"}}`, body)
}