-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mockhttp_test.go
118 lines (113 loc) · 2.38 KB
/
mockhttp_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
package sdk
import (
"net/http"
"reflect"
"testing"
)
func Test_newObjectPath(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want objPath
}{
{
name: "projects",
args: args{"/projects"},
want: objPath{"/projects", false},
},
{
name: "trailing slash",
args: args{"/////"},
want: objPath{"", false},
},
{
name: "project",
args: args{"/projects/fooBar"},
want: objPath{"/projects/{project_id}", false},
},
{
name: "project's branches",
args: args{"/projects/fooBar/branches"},
want: objPath{"/projects/{project_id}/branches", false},
},
{
name: "project's branch",
args: args{"/projects/fooBar/branches/qux"},
want: objPath{"/projects/{project_id}/branches/{branch_id}", false},
},
{
name: "project branch's endpoints",
args: args{"/projects/fooBar/branches/qux/endpoints"},
want: objPath{"/projects/{project_id}/branches/{branch_id}/endpoints", false},
},
{
name: "project not found",
args: args{"/projects/notFound/branches/qux/endpoints"},
want: objPath{"/projects/{project_id}/branches/{branch_id}/endpoints", true},
},
{
name: "project shared",
args: args{"/projects/shared"},
want: objPath{"/projects/shared", false},
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
if got := parsePath(tt.args.s); got != tt.want {
t.Errorf("parsePath(%s) = %v, want %v", tt.args.s, got, tt.want)
}
},
)
}
}
func Test_authErrorResp(t *testing.T) {
type args struct {
req *http.Request
}
errResp := func(msg string) *http.Response {
o := Error{HTTPCode: http.StatusForbidden}
o.Message = msg
return o.httpResp()
}
tests := []struct {
name string
args args
want *http.Response
}{
{
name: "auth successful",
args: args{
req: &http.Request{
Header: http.Header{
"Authorization": []string{"Bearer validKey"},
},
},
},
want: nil,
},
{
name: "auth not successful",
args: args{
req: &http.Request{
Header: http.Header{
"Authorization": []string{"Bearer invalidApiKey"},
},
},
},
want: errResp("authorization failed"),
},
}
for _, tt := range tests {
t.Run(
tt.name, func(t *testing.T) {
if got := authErrorResp(tt.args.req); !reflect.DeepEqual(got, tt.want) {
t.Errorf("authErrorResp() = %v, want %v", got, tt.want)
}
},
)
}
}