-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint_test.go
123 lines (102 loc) · 2.95 KB
/
endpoint_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
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
package dispatch
import (
"context"
"errors"
"log"
"strings"
"testing"
)
func TestEndpoints(t *testing.T) {
api := API{}
api.AddEndpoint("GET/test", testEndpointHandler)
api.AddEndpoint("PUT/test", testEndpointHandler)
api.AddEndpoint("DELETE/tests", testEndpointHandler)
api.AddEndpoint("DELETE/test/{id}", testEndpointHandler)
api.AddEndpoint("GET/apiErrorTest", testAPIErrors)
methods := api.GetMethodsForPath("/test")
if (methods[0] != "GET" && methods[0] != "PUT") || (methods[1] != "GET" && methods[1] != "PUT") {
t.Errorf("Expected GET, PUT, got %s\n", strings.Join(methods, ", "))
}
ctx := context.Background()
_, err := api.Call(ctx, "GET", "/test", []byte("{\"foo\": \"hello\", \"Var2\": 42}"))
if err != nil {
t.Error(err)
}
_, err = api.Call(ctx, "GET", "/test", []byte("{\"Var1\":"))
if !strings.Contains(err.Error(), "unexpected") {
t.Error(err)
}
_, err = api.Call(ctx, "POST", "/none", nil)
if !strings.Contains(err.Error(), "not found") {
t.Error(err)
}
_, err = api.Call(ctx, "GET", "/test", []byte("{\"foo\": \"PANIC\", \"Var2\": 42}"))
if err.Error() != "PANICKING" {
t.Error(err)
}
_, err = api.Call(ctx, "GET", "/apiErrorTest", nil)
if err.Error() != "I'm a teapot" {
t.Error(err)
}
}
func TestEndpointWithContext(t *testing.T) {
api := API{}
api.AddEndpoint("GET/user/{foo}", testPathVarHandler)
ctx := context.Background()
result, err := api.Call(ctx, "GET", "/user/abcde", []byte("{}"))
if result != "abcde" || err != nil {
t.Error(result)
t.Error(err)
}
}
func TestEndpointBadHandler(t *testing.T) {
api := API{}
api.AddEndpoint("GET/test", testBadHandler)
ctx := context.Background()
_, err := api.Call(ctx, "GET", "/test", []byte("{\"foo\": \"TestAdmin\"}"))
if err == nil {
t.Error("Should have failed!")
}
}
func TestMiddleware(t *testing.T) {
api := API{}
api.AddEndpoint("GET/test/{TestVar}", testEndpointHandler, middlewareHook)
ctx := context.Background()
_, err := api.Call(ctx, "GET", "/test/TestVar", []byte("{}"))
if err != nil {
t.Error(err)
}
// Since the TestVar path variable is not "TestVar", the middleware should fail
_, err = api.Call(ctx, "GET", "/test/none", []byte("{}"))
if err == nil || err.Error() != "ERROR" {
t.Error(err)
}
}
type testInputType struct {
Var1 string `json:"foo"`
Var2 int
}
func testEndpointHandler(in testInputType) error {
if in.Var1 == "PANIC" {
return errors.New("PANICKING")
}
return nil
}
func testBadHandler(in1, in2 testInputType) (interface{}, error) {
return "OK", nil
}
func testPathVarHandler(ctx context.Context, in1 testInputType) (interface{}, error) {
pathVars := ContextPathVars(ctx)
log.Println(pathVars)
return pathVars["foo"], nil
}
func middlewareHook(input *EndpointInput) (*EndpointInput, error) {
log.Println(string(input.Input))
if ContextPathVars(input.Ctx)["TestVar"] != "TestVar" {
return nil, errors.New("ERROR")
}
return input, nil
}
func testAPIErrors() *APIError {
return NewAPIError(418, "I'm a teapot")
}