Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Łukasz Nowak
caddy
Commits
4c118549
Commit
4c118549
authored
May 09, 2015
by
jordi collell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added header match and a new failing test
parent
253c069b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
5 deletions
+58
-5
middleware/basicauth/basicauth.go
middleware/basicauth/basicauth.go
+0
-1
middleware/basicauth/basicauth_test.go
middleware/basicauth/basicauth_test.go
+58
-4
No files found.
middleware/basicauth/basicauth.go
View file @
4c118549
...
...
@@ -31,7 +31,6 @@ func (a BasicAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
// Check credentials
if
!
ok
||
username
!=
rule
.
Username
||
password
!=
rule
.
Password
{
w
.
Header
()
.
Set
(
"WWW-Authenticate"
,
"Basic"
)
w
.
WriteHeader
(
http
.
StatusUnauthorized
)
return
http
.
StatusUnauthorized
,
nil
}
...
...
middleware/basicauth/basicauth_test.go
View file @
4c118549
...
...
@@ -26,7 +26,6 @@ func TestBasicAuth(t *testing.T) {
}{
{
"/testing"
,
http
.
StatusUnauthorized
,
"ttest:test"
},
{
"/testing"
,
http
.
StatusOK
,
"test:ttest"
},
{
"/testing"
,
http
.
StatusUnauthorized
,
""
},
}
...
...
@@ -51,16 +50,71 @@ func TestBasicAuth(t *testing.T) {
t
.
Errorf
(
"Test %d: Expected Header '%d' but was '%d'"
,
i
,
test
.
result
,
result
)
}
if
result
==
http
.
StatusUnauthorized
{
headers
:=
rec
.
Header
()
if
val
,
ok
:=
headers
[
"Www-Authenticate"
];
ok
{
if
val
[
0
]
!=
"Basic"
{
t
.
Errorf
(
"Test %d, Www-Authenticate should be %s provided %s"
,
i
,
"Basic"
,
val
[
0
])
}
}
else
{
t
.
Errorf
(
"Test %d, should provide a header Www-Authenticate"
,
i
)
}
}
if
rec
.
Code
!=
test
.
result
{
t
.
Errorf
(
"Test %d: Expected Header '%d' but was '%d'"
,
i
,
test
.
result
,
rec
.
Code
)
}
}
func
TestMultipleOverlappingRules
(
t
*
testing
.
T
)
{
rw
:=
BasicAuth
{
Next
:
middleware
.
HandlerFunc
(
contentHandler
),
Rules
:
[]
Rule
{
{
Username
:
"t"
,
Password
:
"p1"
,
Resources
:
[]
string
{
"/t"
}},
{
Username
:
"t1"
,
Password
:
"p2"
,
Resources
:
[]
string
{
"/t/t"
}},
},
}
tests
:=
[]
struct
{
from
string
result
int
cred
string
}{
{
"/t"
,
http
.
StatusOK
,
"t:p1"
},
{
"/t/t"
,
http
.
StatusOK
,
"t:p1"
},
{
"/t/t"
,
http
.
StatusOK
,
"t1:p2"
},
}
for
i
,
test
:=
range
tests
{
req
,
err
:=
http
.
NewRequest
(
"GET"
,
test
.
from
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"Test %d: Could not create HTTP request %v"
,
i
,
err
)
}
auth
:=
"Basic "
+
base64
.
StdEncoding
.
EncodeToString
([]
byte
(
test
.
cred
))
req
.
Header
.
Set
(
"Authorization"
,
auth
)
rec
:=
httptest
.
NewRecorder
()
result
,
err
:=
rw
.
ServeHTTP
(
rec
,
req
)
if
err
!=
nil
{
t
.
Fatalf
(
"Test %d: Could not ServeHTTP %v"
,
i
,
err
)
}
if
result
!=
test
.
result
{
t
.
Errorf
(
"Test %d: Expected Header '%d' but was '%d'"
,
i
,
test
.
result
,
result
)
}
}
}
func
contentHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
fmt
.
Fprintf
(
w
,
r
.
URL
.
String
())
return
http
.
StatusOK
,
nil
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment