-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidator_test.go
71 lines (62 loc) · 1.64 KB
/
validator_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
// Copyright 2017 The Bulma Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bulma
import (
"net/http"
"net/http/httptest"
"testing"
)
var validatorTest = []struct {
validator Validator
code int
body string
}{
{
Auth(userTest.username, userTest.password),
http.StatusOK,
"success",
},
{
User{userTest.username: userTest.password},
http.StatusOK,
"success",
},
}
func TestValidator(t *testing.T) {
for _, tt := range validatorTest {
basicAuth := BasicAuth(Realm, handlerTest, tt.validator)
rec := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Error(err)
}
req.SetBasicAuth(userTest.username, userTest.password)
basicAuth.ServeHTTP(rec, req)
if rec.Code != tt.code {
t.Errorf("got status %d; want %d", rec.Code, http.StatusOK)
}
if got, want := rec.Body.String(), tt.body; got != want {
t.Errorf("rec.Body.String() got %q; want %q", got, want)
}
username, password, ok := req.BasicAuth()
if !ok && username != userTest.username && password != userTest.password {
t.Errorf(
"req.BasicAuth() got (%q/%q); want (%q/%q)",
username,
password,
userTest.username,
userTest.password,
)
}
req.Header.Del("Authorization")
rec = httptest.NewRecorder()
basicAuth.ServeHTTP(rec, req)
if rec.Code != http.StatusUnauthorized {
t.Errorf("got status %d; want %d", rec.Code, http.StatusOK)
}
if got, want := rec.Body.String(), "Unauthorized"; got != want {
t.Errorf("rec.Body.String() got %q; want %q", got, want)
}
}
}