Fix proxy upstream parser issue and add test cases (#263)

This fix tries to fix 261 where proxy upstream parser is not
able to parse upstream correctly.

Several test cases have also been added to cover the changes
and prevent regression in the future.

This fix fixes 261.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang 2016-09-16 23:49:35 -07:00 committed by Miek Gieben
parent 50d47a55a2
commit ed907d3327
3 changed files with 80 additions and 3 deletions

View file

@ -15,7 +15,7 @@ func init() {
}
func setup(c *caddy.Controller) error {
upstreams, err := NewStaticUpstreams(c.Dispenser)
upstreams, err := NewStaticUpstreams(&c.Dispenser)
if err != nil {
return middleware.Error("proxy", err)
}

View file

@ -44,7 +44,7 @@ type Options struct {
// NewStaticUpstreams parses the configuration input and sets up
// static upstreams for the proxy middleware.
func NewStaticUpstreams(c caddyfile.Dispenser) ([]Upstream, error) {
func NewStaticUpstreams(c *caddyfile.Dispenser) ([]Upstream, error) {
var upstreams []Upstream
for c.Next() {
upstream := &staticUpstream{
@ -126,7 +126,7 @@ func (u *staticUpstream) Options() Options {
return u.options
}
func parseBlock(c caddyfile.Dispenser, u *staticUpstream) error {
func parseBlock(c *caddyfile.Dispenser, u *staticUpstream) error {
switch c.Val() {
case "policy":
if !c.NextArg() {

View file

@ -3,6 +3,8 @@ package proxy
import (
"testing"
"time"
"github.com/mholt/caddy"
)
func TestHealthCheck(t *testing.T) {
@ -75,3 +77,78 @@ func TestAllowedPaths(t *testing.T) {
}
}
}
func TestProxyParse(t *testing.T) {
tests := []struct {
inputUpstreams string
shouldErr bool
}{
{
`proxy . 8.8.8.8:53`,
false,
},
{
`
proxy . 8.8.8.8:53 {
policy round_robin
}`,
false,
},
{
`
proxy . 8.8.8.8:53 {
fail_timeout 5s
}`,
false,
},
{
`
proxy . 8.8.8.8:53 {
max_fails 10
}`,
false,
},
{
`
proxy . 8.8.8.8:53 {
health_check /health:8080
}`,
false,
},
{
`
proxy . 8.8.8.8:53 {
without without
}`,
false,
},
{
`
proxy . 8.8.8.8:53 {
except miek.nl example.org
}`,
false,
},
{
`
proxy . 8.8.8.8:53 {
spray
}`,
false,
},
{
`
proxy . 8.8.8.8:53 {
error_option
}`,
true,
},
}
for i, test := range tests {
c := caddy.NewTestController("dns", test.inputUpstreams)
_, err := NewStaticUpstreams(&c.Dispenser)
if (err != nil) != test.shouldErr {
t.Errorf("Test %d expected no error, got %v", i+1, err)
}
}
}