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
nexedi
caddy
Commits
40105094
Commit
40105094
authored
May 07, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Some tests and utilities for setup functions
parent
0dba8d40
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
229 additions
and
0 deletions
+229
-0
config/setup/basicauth_test.go
config/setup/basicauth_test.go
+100
-0
config/setup/controller_test.go
config/setup/controller_test.go
+15
-0
config/setup/gzip_test.go
config/setup/gzip_test.go
+29
-0
config/setup/rewrite_test.go
config/setup/rewrite_test.go
+85
-0
No files found.
config/setup/basicauth_test.go
0 → 100644
View file @
40105094
package
setup
import
(
"fmt"
"testing"
"github.com/mholt/caddy/middleware/basicauth"
)
func
TestBasicAuth
(
t
*
testing
.
T
)
{
c
:=
newTestController
(
`basicauth user pwd`
)
mid
,
err
:=
BasicAuth
(
c
)
if
err
!=
nil
{
t
.
Errorf
(
"Expected no errors, but got: %v"
,
err
)
}
if
mid
==
nil
{
t
.
Fatal
(
"Expected middleware, was nil instead"
)
}
handler
:=
mid
(
emptyNext
)
myHandler
,
ok
:=
handler
.
(
basicauth
.
BasicAuth
)
if
!
ok
{
t
.
Fatalf
(
"Expected handler to be type BasicAuth, got: %#v"
,
handler
)
}
if
!
sameNext
(
myHandler
.
Next
,
emptyNext
)
{
t
.
Error
(
"'Next' field of handler was not set properly"
)
}
}
func
TestBasicAuthParse
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
input
string
shouldErr
bool
expected
[]
basicauth
.
Rule
}{
{
`basicauth user pwd`
,
false
,
[]
basicauth
.
Rule
{
{
Username
:
"user"
,
Password
:
"pwd"
},
}},
{
`basicauth user pwd {
}`
,
false
,
[]
basicauth
.
Rule
{
{
Username
:
"user"
,
Password
:
"pwd"
},
}},
{
`basicauth user pwd {
/resource1
/resource2
}`
,
false
,
[]
basicauth
.
Rule
{
{
Username
:
"user"
,
Password
:
"pwd"
,
Resources
:
[]
string
{
"/resource1"
,
"/resource2"
}},
}},
{
`basicauth /resource user pwd`
,
false
,
[]
basicauth
.
Rule
{
{
Username
:
"user"
,
Password
:
"pwd"
,
Resources
:
[]
string
{
"/resource"
}},
}},
{
`basicauth /res1 user1 pwd1
basicauth /res2 user2 pwd2`
,
false
,
[]
basicauth
.
Rule
{
{
Username
:
"user1"
,
Password
:
"pwd1"
,
Resources
:
[]
string
{
"/res1"
}},
{
Username
:
"user2"
,
Password
:
"pwd2"
,
Resources
:
[]
string
{
"/res2"
}},
}},
{
`basicauth user`
,
true
,
[]
basicauth
.
Rule
{}},
{
`basicauth`
,
true
,
[]
basicauth
.
Rule
{}},
{
`basicauth /resource user pwd asdf`
,
true
,
[]
basicauth
.
Rule
{}},
}
for
i
,
test
:=
range
tests
{
c
:=
newTestController
(
test
.
input
)
actual
,
err
:=
basicAuthParse
(
c
)
if
err
==
nil
&&
test
.
shouldErr
{
t
.
Errorf
(
"Test %d didn't error, but it should have"
,
i
)
}
else
if
err
!=
nil
&&
!
test
.
shouldErr
{
t
.
Errorf
(
"Test %d errored, but it shouldn't have; got '%v'"
,
i
,
err
)
}
if
len
(
actual
)
!=
len
(
test
.
expected
)
{
t
.
Fatalf
(
"Test %d expected %d rules, but got %d"
,
i
,
len
(
test
.
expected
),
len
(
actual
))
}
for
j
,
expectedRule
:=
range
test
.
expected
{
actualRule
:=
actual
[
j
]
if
actualRule
.
Username
!=
expectedRule
.
Username
{
t
.
Errorf
(
"Test %d, rule %d: Expected username '%s', got '%s'"
,
i
,
j
,
expectedRule
.
Username
,
actualRule
.
Username
)
}
if
actualRule
.
Password
!=
expectedRule
.
Password
{
t
.
Errorf
(
"Test %d, rule %d: Expected password '%s', got '%s'"
,
i
,
j
,
expectedRule
.
Password
,
actualRule
.
Password
)
}
expectedRes
:=
fmt
.
Sprintf
(
"%v"
,
expectedRule
.
Resources
)
actualRes
:=
fmt
.
Sprintf
(
"%v"
,
actualRule
.
Resources
)
if
actualRes
!=
expectedRes
{
t
.
Errorf
(
"Test %d, rule %d: Expected resource list %s, but got %s"
,
i
,
j
,
expectedRes
,
actualRes
)
}
}
}
}
config/setup/controller_test.go
View file @
40105094
package
setup
import
(
"fmt"
"net/http"
"strings"
"github.com/mholt/caddy/config/parse"
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/server"
)
...
...
@@ -15,3 +18,15 @@ func newTestController(input string) *Controller {
Dispenser
:
parse
.
NewDispenser
(
"Testfile"
,
strings
.
NewReader
(
input
)),
}
}
// emptyNext is a no-op function that can be passed into
// middleware.Middleware functions so that the assignment
// to the Next field of the Handler can be tested.
var
emptyNext
=
middleware
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
return
0
,
nil
})
// sameNext does a pointer comparison between next1 and next2.
func
sameNext
(
next1
,
next2
middleware
.
Handler
)
bool
{
return
fmt
.
Sprintf
(
"%p"
,
next1
)
==
fmt
.
Sprintf
(
"%p"
,
next2
)
}
config/setup/gzip_test.go
0 → 100644
View file @
40105094
package
setup
import
(
"testing"
"github.com/mholt/caddy/middleware/gzip"
)
func
TestGzip
(
t
*
testing
.
T
)
{
c
:=
newTestController
(
`gzip`
)
mid
,
err
:=
Gzip
(
c
)
if
err
!=
nil
{
t
.
Errorf
(
"Expected no errors, but got: %v"
,
err
)
}
if
mid
==
nil
{
t
.
Fatal
(
"Expected middleware, was nil instead"
)
}
handler
:=
mid
(
emptyNext
)
myHandler
,
ok
:=
handler
.
(
gzip
.
Gzip
)
if
!
ok
{
t
.
Fatalf
(
"Expected handler to be type Gzip, got: %#v"
,
handler
)
}
if
!
sameNext
(
myHandler
.
Next
,
emptyNext
)
{
t
.
Error
(
"'Next' field of handler was not set properly"
)
}
}
config/setup/rewrite_test.go
0 → 100644
View file @
40105094
package
setup
import
(
"testing"
"github.com/mholt/caddy/middleware/rewrite"
)
func
TestRewrite
(
t
*
testing
.
T
)
{
c
:=
newTestController
(
`rewrite /from /to`
)
mid
,
err
:=
Rewrite
(
c
)
if
err
!=
nil
{
t
.
Errorf
(
"Expected no errors, but got: %v"
,
err
)
}
if
mid
==
nil
{
t
.
Fatal
(
"Expected middleware, was nil instead"
)
}
handler
:=
mid
(
emptyNext
)
myHandler
,
ok
:=
handler
.
(
rewrite
.
Rewrite
)
if
!
ok
{
t
.
Fatalf
(
"Expected handler to be type Rewrite, got: %#v"
,
handler
)
}
if
!
sameNext
(
myHandler
.
Next
,
emptyNext
)
{
t
.
Error
(
"'Next' field of handler was not set properly"
)
}
if
len
(
myHandler
.
Rules
)
!=
1
{
t
.
Errorf
(
"Expected handler to have %d rule, has %d instead"
,
1
,
len
(
myHandler
.
Rules
))
}
}
func
TestRewriteParse
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
input
string
shouldErr
bool
expected
[]
rewrite
.
Rule
}{
{
`rewrite /from /to`
,
false
,
[]
rewrite
.
Rule
{
{
From
:
"/from"
,
To
:
"/to"
},
}},
{
`rewrite /from /to
rewrite a b`
,
false
,
[]
rewrite
.
Rule
{
{
From
:
"/from"
,
To
:
"/to"
},
{
From
:
"a"
,
To
:
"b"
},
}},
{
`rewrite a`
,
true
,
[]
rewrite
.
Rule
{}},
{
`rewrite`
,
true
,
[]
rewrite
.
Rule
{}},
{
`rewrite a b c`
,
true
,
[]
rewrite
.
Rule
{
{
From
:
"a"
,
To
:
"b"
},
}},
}
for
i
,
test
:=
range
tests
{
c
:=
newTestController
(
test
.
input
)
actual
,
err
:=
rewriteParse
(
c
)
if
err
==
nil
&&
test
.
shouldErr
{
t
.
Errorf
(
"Test %d didn't error, but it should have"
,
i
)
}
else
if
err
!=
nil
&&
!
test
.
shouldErr
{
t
.
Errorf
(
"Test %d errored, but it shouldn't have; got '%v'"
,
i
,
err
)
}
if
len
(
actual
)
!=
len
(
test
.
expected
)
{
t
.
Fatalf
(
"Test %d expected %d rules, but got %d"
,
i
,
len
(
test
.
expected
),
len
(
actual
))
}
for
j
,
expectedRule
:=
range
test
.
expected
{
actualRule
:=
actual
[
j
]
if
actualRule
.
From
!=
expectedRule
.
From
{
t
.
Errorf
(
"Test %d, rule %d: Expected From=%s, got %s"
,
i
,
j
,
expectedRule
.
From
,
actualRule
.
From
)
}
if
actualRule
.
To
!=
expectedRule
.
To
{
t
.
Errorf
(
"Test %d, rule %d: Expected To=%s, got %s"
,
i
,
j
,
expectedRule
.
To
,
actualRule
.
To
)
}
}
}
}
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