Commit ed907d33 authored by Yong Tang's avatar Yong Tang Committed by Miek Gieben

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: default avatarYong Tang <yong.tang.github@outlook.com>
parent 50d47a55
......@@ -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)
}
......
......@@ -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() {
......
......@@ -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)
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment