Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/public-api-server/pkg/auth/auth_test.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package auth
6
7
import (
8
"net/http"
9
"testing"
10
11
"github.com/stretchr/testify/require"
12
)
13
14
func TestBearerTokenFromHeaders(t *testing.T) {
15
type Scenario struct {
16
Name string
17
18
// Input
19
Header http.Header
20
21
// Output
22
Token string
23
Error error
24
}
25
26
for _, s := range []Scenario{
27
{
28
Name: "happy case",
29
Header: addToHeader(http.Header{}, "Authorization", "Bearer foo"),
30
Token: "foo",
31
},
32
{
33
Name: "leading and trailing spaces are trimmed",
34
Header: addToHeader(http.Header{}, "Authorization", " Bearer foo "),
35
Token: "foo",
36
},
37
{
38
Name: "anything after Bearer is extracted",
39
Header: addToHeader(http.Header{}, "Authorization", "Bearer foo bar"),
40
Token: "foo bar",
41
},
42
{
43
Name: "duplicate bearer",
44
Header: addToHeader(http.Header{}, "Authorization", "Bearer Bearer foo"),
45
Token: "Bearer foo",
46
},
47
{
48
Name: "missing Bearer prefix fails",
49
Header: addToHeader(http.Header{}, "Authorization", "foo"),
50
Error: NoAccessToken,
51
},
52
{
53
Name: "missing Authorization header fails",
54
Header: http.Header{},
55
Error: NoAccessToken,
56
},
57
} {
58
t.Run(s.Name, func(t *testing.T) {
59
token, err := BearerTokenFromHeaders(s.Header)
60
require.ErrorIs(t, err, s.Error)
61
require.Equal(t, s.Token, token)
62
})
63
}
64
}
65
66
func addToHeader(h http.Header, key, value string) http.Header {
67
h.Add(key, value)
68
return h
69
}
70
71