Skip to content

Commit 488b962

Browse files
committed
Add test for parsing bearer tokens.
1 parent f9ef432 commit 488b962

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

webserver/webserver_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,35 @@ func TestParseWhip(t *testing.T) {
6161
}
6262
}
6363

64+
func TestGetBearerToken(t *testing.T) {
65+
a := []struct{ a, b string }{
66+
{"", ""},
67+
{"foo", ""},
68+
{"foo bar", ""},
69+
{" foo bar", ""},
70+
{"foo bar ", ""},
71+
{"Bearer", ""},
72+
{"Bearer ", ""},
73+
{"Bearer foo", "foo"},
74+
{"bearer foo", "foo"},
75+
{" Bearer foo", "foo"},
76+
{"Bearer foo ", "foo"},
77+
{" Bearer foo ", "foo"},
78+
{"Bearer foo bar", ""},
79+
}
80+
81+
for _, ab := range a {
82+
t.Run(ab.a, func(t *testing.T) {
83+
b := parseBearerToken(ab.a)
84+
if b != ab.b {
85+
t.Errorf("Bearer token %v, got %v, expected %v",
86+
ab.a, b, ab.b,
87+
)
88+
}
89+
})
90+
}
91+
}
92+
6493
func TestFormatICEServer(t *testing.T) {
6594
a := []struct {
6695
s webrtc.ICEServer

webserver/whip.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ func canPresent(perms []string) bool {
5252
return false
5353
}
5454

55-
func getBearerToken(r *http.Request) string {
56-
auth := r.Header.Get("Authorization")
55+
func parseBearerToken(auth string) string {
5756
auths := strings.Split(auth, ",")
5857
for _, a := range auths {
5958
a = strings.Trim(a, " \t")
@@ -178,7 +177,8 @@ func whipEndpointHandler(w http.ResponseWriter, r *http.Request) {
178177
return
179178
}
180179

181-
token := getBearerToken(r)
180+
token := parseBearerToken(r.Header.Get("Authorization"))
181+
182182
whip := "whip"
183183
creds := group.ClientCredentials{
184184
Username: &whip,
@@ -258,7 +258,7 @@ func whipResourceHandler(w http.ResponseWriter, r *http.Request) {
258258
}
259259

260260
if t := c.Token(); t != "" {
261-
token := getBearerToken(r)
261+
token := parseBearerToken(r.Header.Get("Authorization"))
262262
if token != t {
263263
http.Error(w, "Forbidden", http.StatusForbidden)
264264
return

0 commit comments

Comments
 (0)