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
881da313
Commit
881da313
authored
Jun 03, 2016
by
Marc Guasch
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tests for upstream directive
parent
1bdbf9d6
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
122 additions
and
0 deletions
+122
-0
caddy/setup/proxy_test.go
caddy/setup/proxy_test.go
+122
-0
No files found.
caddy/setup/proxy_test.go
0 → 100644
View file @
881da313
package
setup
import
(
"reflect"
"testing"
"github.com/mholt/caddy/middleware/proxy"
)
func
TestUpstream
(
t
*
testing
.
T
)
{
for
i
,
test
:=
range
[]
struct
{
input
string
shouldErr
bool
expectedHosts
map
[
string
]
struct
{}
}{
// test #0 test usual to destination still works normally
{
"proxy / localhost:80"
,
false
,
map
[
string
]
struct
{}{
"http://localhost:80"
:
struct
{}{},
},
},
// test #1 test usual to destination with port range
{
"proxy / localhost:8080-8082"
,
false
,
map
[
string
]
struct
{}{
"http://localhost:8080"
:
struct
{}{},
"http://localhost:8081"
:
struct
{}{},
"http://localhost:8082"
:
struct
{}{},
},
},
// test #2 test upstream directive
{
"proxy / {
\n
upstream localhost:8080
\n
}"
,
false
,
map
[
string
]
struct
{}{
"http://localhost:8080"
:
struct
{}{},
},
},
// test #3 test upstream directive with port range
{
"proxy / {
\n
upstream localhost:8080-8081
\n
}"
,
false
,
map
[
string
]
struct
{}{
"http://localhost:8080"
:
struct
{}{},
"http://localhost:8081"
:
struct
{}{},
},
},
// test #4 test to destination with upstream directive
{
"proxy / localhost:8080 {
\n
upstream localhost:8081-8082
\n
}"
,
false
,
map
[
string
]
struct
{}{
"http://localhost:8080"
:
struct
{}{},
"http://localhost:8081"
:
struct
{}{},
"http://localhost:8082"
:
struct
{}{},
},
},
// test #5 test with unix sockets
{
"proxy / localhost:8080 {
\n
upstream unix:/var/foo
\n
}"
,
false
,
map
[
string
]
struct
{}{
"http://localhost:8080"
:
struct
{}{},
"unix:/var/foo"
:
struct
{}{},
},
},
// test #6 test fail on malformed port range
{
"proxy / localhost:8090-8080"
,
true
,
nil
,
},
// test #7 test fail on malformed port range 2
{
"proxy / {
\n
upstream localhost:80-A
\n
}"
,
true
,
nil
,
},
// test #8 test upstreams without ports work correctly
{
"proxy / http://localhost {
\n
upstream testendpoint
\n
}"
,
false
,
map
[
string
]
struct
{}{
"http://localhost"
:
struct
{}{},
"http://testendpoint"
:
struct
{}{},
},
},
}
{
receivedFunc
,
err
:=
Proxy
(
NewTestController
(
test
.
input
))
if
err
!=
nil
&&
!
test
.
shouldErr
{
t
.
Errorf
(
"Test case #%d received an error of %v"
,
i
,
err
)
}
else
if
test
.
shouldErr
{
continue
}
upstreams
:=
receivedFunc
(
nil
)
.
(
proxy
.
Proxy
)
.
Upstreams
for
_
,
upstream
:=
range
upstreams
{
val
:=
reflect
.
ValueOf
(
upstream
)
.
Elem
()
hosts
:=
val
.
FieldByName
(
"Hosts"
)
.
Interface
()
.
(
proxy
.
HostPool
)
if
len
(
hosts
)
!=
len
(
test
.
expectedHosts
)
{
t
.
Errorf
(
"Test case #%d expected %d hosts but received %d"
,
i
,
len
(
test
.
expectedHosts
),
len
(
hosts
))
}
else
{
for
_
,
host
:=
range
hosts
{
if
_
,
found
:=
test
.
expectedHosts
[
host
.
Name
];
!
found
{
t
.
Errorf
(
"Test case #%d has an unexpected host %s"
,
i
,
host
.
Name
)
}
}
}
}
}
}
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