-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhijacker_test.go
128 lines (123 loc) · 2.65 KB
/
hijacker_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
124
125
126
127
128
package slackauth
import (
"bytes"
"context"
"errors"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_hijacker_Token(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
h := hijacker{
credsC: make(chan creds, 1),
}
go func() {
h.credsC <- creds{Token: "token"}
}()
token, err := h.Token(context.Background())
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if token != "token" {
t.Errorf("unexpected token: %v", token)
}
})
t.Run("cancelled context returns the cause", func(t *testing.T) {
h := hijacker{
credsC: make(chan creds, 1),
}
ctx, cancel := context.WithCancelCause(context.Background())
e := errors.New("cause")
cancel(e)
token, err := h.Token(ctx)
if err == nil {
t.Error("expected an error")
}
if token != "" {
t.Errorf("unexpected token: %v", token)
}
assert.ErrorIs(t, err, e)
})
t.Run("error in the credentials struct", func(t *testing.T) {
h := hijacker{
credsC: make(chan creds, 1),
}
e := errors.New("error")
go func() {
h.credsC <- creds{Err: e}
}()
token, err := h.Token(context.Background())
if err == nil {
t.Error("expected an error")
}
if token != "" {
t.Errorf("unexpected token: %v", token)
}
assert.ErrorIs(t, err, e)
})
}
func Test_extractToken(t *testing.T) {
type args struct {
r *http.Request
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "request with form data containing token",
args: args{
r: mkMultipartRequest(url.Values{paramToken: {"123"}}),
},
want: "123",
wantErr: false,
},
{
name: "request with form data without token",
args: args{
r: mkMultipartRequest(url.Values{"somefield": {"123"}}),
},
want: "",
wantErr: true,
},
{
name: "request without form data",
args: args{
r: httptest.NewRequest(http.MethodPost, "http://example.com", nil),
},
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := extractToken(tt.args.r)
if (err != nil) != tt.wantErr {
t.Errorf("extractToken() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("extractToken() = %v, want %v", got, tt.want)
}
})
}
}
func mkMultipartRequest(v url.Values) *http.Request {
buf := new(bytes.Buffer)
w := multipart.NewWriter(buf)
for k, vs := range v {
for _, v := range vs {
w.WriteField(k, v)
}
}
w.Close()
req := httptest.NewRequest(http.MethodPost, "http://example.com", buf)
req.Header.Set("Content-Type", w.FormDataContentType())
return req
}