mirror of
https://github.com/coredns/coredns.git
synced 2025-10-28 00:34:24 -04:00
Various cleanups and fixes (#88)
Add port number to health check. Add tests the rewrite middleware. Fixes #36
This commit is contained in:
@@ -22,9 +22,9 @@ proxy from to... {
|
|||||||
policy random | least_conn | round_robin
|
policy random | least_conn | round_robin
|
||||||
fail_timeout duration
|
fail_timeout duration
|
||||||
max_fails integer
|
max_fails integer
|
||||||
health_check path [duration]
|
health_check path:port [duration]
|
||||||
except ignored_names...
|
except ignored_names...
|
||||||
preset
|
tcp
|
||||||
}
|
}
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
@@ -33,8 +33,10 @@ proxy from to... {
|
|||||||
* `policy` is the load balancing policy to use; applies only with multiple backends. May be one of random, least_conn, or round_robin. Default is random.
|
* `policy` is the load balancing policy to use; applies only with multiple backends. May be one of random, least_conn, or round_robin. Default is random.
|
||||||
* `fail_timeout` specifies how long to consider a backend as down after it has failed. While it is down, requests will not be routed to that backend. A backend is "down" if Caddy fails to communicate with it. The default value is 10 seconds ("10s").
|
* `fail_timeout` specifies how long to consider a backend as down after it has failed. While it is down, requests will not be routed to that backend. A backend is "down" if Caddy fails to communicate with it. The default value is 10 seconds ("10s").
|
||||||
* `max_fails` is the number of failures within fail_timeout that are needed before considering a backend to be down. If 0, the backend will never be marked as down. Default is 1.
|
* `max_fails` is the number of failures within fail_timeout that are needed before considering a backend to be down. If 0, the backend will never be marked as down. Default is 1.
|
||||||
* `health_check` will check path on each backend. If a backend returns a status code of 200-399, then that backend is healthy. If it doesn't, the backend is marked as unhealthy for duration and no requests are routed to it. If this option is not provided then health checks are disabled. The default duration is 10 seconds ("10s").
|
* `health_check` will check path (on port) on each backend. If a backend returns a status code of 200-399, then that backend is healthy. If it doesn't, the backend is marked as unhealthy for duration and no requests are routed to it. If this option is not provided then health checks are disabled. The default duration is 10 seconds ("10s").
|
||||||
* `ignored_names...` is a space-separated list of paths to exclude from proxying. Requests that match any of these paths will be passed thru.
|
* `ignored_names...` is a space-separated list of paths to exclude from proxying. Requests that match any of these paths will be passed thru.
|
||||||
|
* `tcp` use TCP for all upstream queries, otherwise it depends on the transport of the incoming
|
||||||
|
query. TODO(miek): implement.
|
||||||
|
|
||||||
## Policies
|
## Policies
|
||||||
|
|
||||||
@@ -68,9 +70,9 @@ proxy . web1.local:53 web2.local:1053 web3.local {
|
|||||||
With health checks and proxy headers to pass hostname, IP, and scheme upstream:
|
With health checks and proxy headers to pass hostname, IP, and scheme upstream:
|
||||||
|
|
||||||
~~~
|
~~~
|
||||||
proxy / web1.local:80 web2.local:90 web3.local:100 {
|
proxy . web1.local:53 web2.local:53 web3.local:53 {
|
||||||
policy round_robin
|
policy round_robin
|
||||||
health_check /health
|
health_check /health:8080
|
||||||
}
|
}
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package proxy
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -28,6 +29,7 @@ type staticUpstream struct {
|
|||||||
MaxFails int32
|
MaxFails int32
|
||||||
HealthCheck struct {
|
HealthCheck struct {
|
||||||
Path string
|
Path string
|
||||||
|
Port string
|
||||||
Interval time.Duration
|
Interval time.Duration
|
||||||
}
|
}
|
||||||
WithoutPathPrefix string
|
WithoutPathPrefix string
|
||||||
@@ -138,7 +140,11 @@ func parseBlock(c *parse.Dispenser, u *staticUpstream) error {
|
|||||||
if !c.NextArg() {
|
if !c.NextArg() {
|
||||||
return c.ArgErr()
|
return c.ArgErr()
|
||||||
}
|
}
|
||||||
u.HealthCheck.Path = c.Val()
|
var err error
|
||||||
|
u.HealthCheck.Path, u.HealthCheck.Port, err = net.SplitHostPort(c.Val())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
u.HealthCheck.Interval = 30 * time.Second
|
u.HealthCheck.Interval = 30 * time.Second
|
||||||
if c.NextArg() {
|
if c.NextArg() {
|
||||||
dur, err := time.ParseDuration(c.Val())
|
dur, err := time.ParseDuration(c.Val())
|
||||||
@@ -175,7 +181,11 @@ func parseBlock(c *parse.Dispenser, u *staticUpstream) error {
|
|||||||
|
|
||||||
func (u *staticUpstream) healthCheck() {
|
func (u *staticUpstream) healthCheck() {
|
||||||
for _, host := range u.Hosts {
|
for _, host := range u.Hosts {
|
||||||
hostURL := host.Name + u.HealthCheck.Path
|
port := ""
|
||||||
|
if u.HealthCheck.Port != "" {
|
||||||
|
port = ":" + u.HealthCheck.Port
|
||||||
|
}
|
||||||
|
hostURL := host.Name + port + u.HealthCheck.Path
|
||||||
if r, err := http.Get(hostURL); err == nil {
|
if r, err := http.Get(hostURL); err == nil {
|
||||||
io.Copy(ioutil.Discard, r.Body)
|
io.Copy(ioutil.Discard, r.Body)
|
||||||
r.Body.Close()
|
r.Body.Close()
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ rewritten. I.e. to rewrite CH queries to IN use `rewrite CH IN`.
|
|||||||
If it does not look like a type a name is assumed and the qname in the message is rewritten, this
|
If it does not look like a type a name is assumed and the qname in the message is rewritten, this
|
||||||
needs to be a full match of the name `rewrite miek.nl example.org`.
|
needs to be a full match of the name `rewrite miek.nl example.org`.
|
||||||
|
|
||||||
Advanced users may open a block and make a complex rewrite rule:
|
If you specify multiple rules and an incoming query matches on multiple (simple) rules only one
|
||||||
TODO(miek): this has not yet been implemented.
|
the first rewrite is applied.
|
||||||
|
|
||||||
> Everything below this line has not been implemented, yet.
|
> Everything below this line has not been implemented, yet.
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
// Package rewrite is middleware for rewriting requests internally to
|
// Package rewrite is middleware for rewriting requests internally to something different.
|
||||||
// something different.
|
|
||||||
package rewrite
|
package rewrite
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/miekg/coredns/middleware"
|
"github.com/miekg/coredns/middleware"
|
||||||
|
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
@@ -25,16 +25,20 @@ const (
|
|||||||
|
|
||||||
// Rewrite is middleware to rewrite requests internally before being handled.
|
// Rewrite is middleware to rewrite requests internally before being handled.
|
||||||
type Rewrite struct {
|
type Rewrite struct {
|
||||||
Next middleware.Handler
|
Next middleware.Handler
|
||||||
Rules []Rule
|
Rules []Rule
|
||||||
|
noRevert bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeHTTP implements the middleware.Handler interface.
|
// ServeHTTP implements the middleware.Handler interface.
|
||||||
func (rw Rewrite) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
func (rw Rewrite) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||||
wr := NewResponseReverter(w, r)
|
|
||||||
for _, rule := range rw.Rules {
|
for _, rule := range rw.Rules {
|
||||||
switch result := rule.Rewrite(r); result {
|
switch result := rule.Rewrite(r); result {
|
||||||
case RewriteDone:
|
case RewriteDone:
|
||||||
|
if rw.noRevert {
|
||||||
|
return rw.Next.ServeDNS(ctx, w, r)
|
||||||
|
}
|
||||||
|
wr := NewResponseReverter(w, r)
|
||||||
return rw.Next.ServeDNS(ctx, wr, r)
|
return rw.Next.ServeDNS(ctx, wr, r)
|
||||||
case RewriteIgnored:
|
case RewriteIgnored:
|
||||||
break
|
break
|
||||||
@@ -67,8 +71,16 @@ type SimpleRule struct {
|
|||||||
func NewSimpleRule(from, to string) SimpleRule {
|
func NewSimpleRule(from, to string) SimpleRule {
|
||||||
tpf := dns.StringToType[from]
|
tpf := dns.StringToType[from]
|
||||||
tpt := dns.StringToType[to]
|
tpt := dns.StringToType[to]
|
||||||
|
|
||||||
|
// ANY is both a type and class, ANY class rewritting is way more less frequent
|
||||||
|
// so we default to ANY as a type.
|
||||||
clf := dns.StringToClass[from]
|
clf := dns.StringToClass[from]
|
||||||
clt := dns.StringToClass[to]
|
clt := dns.StringToClass[to]
|
||||||
|
if from == "ANY" {
|
||||||
|
clf = 0
|
||||||
|
clt = 0
|
||||||
|
}
|
||||||
|
|
||||||
// It's only a type/class if uppercase is used.
|
// It's only a type/class if uppercase is used.
|
||||||
if from != strings.ToUpper(from) {
|
if from != strings.ToUpper(from) {
|
||||||
tpf = 0
|
tpf = 0
|
||||||
@@ -85,25 +97,20 @@ func NewSimpleRule(from, to string) SimpleRule {
|
|||||||
|
|
||||||
// Rewrite rewrites the the current request.
|
// Rewrite rewrites the the current request.
|
||||||
func (s SimpleRule) Rewrite(r *dns.Msg) Result {
|
func (s SimpleRule) Rewrite(r *dns.Msg) Result {
|
||||||
// type rewrite
|
|
||||||
if s.fromType > 0 && s.toType > 0 {
|
if s.fromType > 0 && s.toType > 0 {
|
||||||
if r.Question[0].Qtype == s.fromType {
|
if r.Question[0].Qtype == s.fromType {
|
||||||
r.Question[0].Qtype = s.toType
|
r.Question[0].Qtype = s.toType
|
||||||
return RewriteDone
|
return RewriteDone
|
||||||
}
|
}
|
||||||
return RewriteIgnored
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// class rewrite
|
|
||||||
if s.fromClass > 0 && s.toClass > 0 {
|
if s.fromClass > 0 && s.toClass > 0 {
|
||||||
if r.Question[0].Qclass == s.fromClass {
|
if r.Question[0].Qclass == s.fromClass {
|
||||||
r.Question[0].Qclass = s.toClass
|
r.Question[0].Qclass = s.toClass
|
||||||
return RewriteDone
|
return RewriteDone
|
||||||
}
|
}
|
||||||
return RewriteIgnored
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// name rewite
|
|
||||||
if s.From == r.Question[0].Name {
|
if s.From == r.Question[0].Name {
|
||||||
r.Question[0].Name = s.To
|
r.Question[0].Name = s.To
|
||||||
return RewriteDone
|
return RewriteDone
|
||||||
|
|||||||
@@ -1,151 +1,142 @@
|
|||||||
package rewrite
|
package rewrite
|
||||||
|
|
||||||
/*
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/miekg/coredns/middleware"
|
||||||
|
coretest "github.com/miekg/coredns/middleware/testing"
|
||||||
|
|
||||||
|
"github.com/miekg/dns"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
func msgPrinter(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||||
|
w.WriteMsg(r)
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
func TestRewrite(t *testing.T) {
|
func TestRewrite(t *testing.T) {
|
||||||
rw := Rewrite{
|
rw := Rewrite{
|
||||||
Next: middleware.HandlerFunc(urlPrinter),
|
Next: middleware.HandlerFunc(msgPrinter),
|
||||||
Rules: []Rule{
|
Rules: []Rule{
|
||||||
NewSimpleRule("/from", "/to"),
|
NewSimpleRule("from.nl.", "to.nl."),
|
||||||
NewSimpleRule("/a", "/b"),
|
NewSimpleRule("CH", "IN"),
|
||||||
NewSimpleRule("/b", "/b{uri}"),
|
NewSimpleRule("ANY", "HINFO"),
|
||||||
},
|
},
|
||||||
FileSys: http.Dir("."),
|
noRevert: true,
|
||||||
}
|
|
||||||
|
|
||||||
regexps := [][]string{
|
|
||||||
{"/reg/", ".*", "/to", ""},
|
|
||||||
{"/r/", "[a-z]+", "/toaz", "!.html|"},
|
|
||||||
{"/url/", "a([a-z0-9]*)s([A-Z]{2})", "/to/{path}", ""},
|
|
||||||
{"/ab/", "ab", "/ab?{query}", ".txt|"},
|
|
||||||
{"/ab/", "ab", "/ab?type=html&{query}", ".html|"},
|
|
||||||
{"/abc/", "ab", "/abc/{file}", ".html|"},
|
|
||||||
{"/abcd/", "ab", "/a/{dir}/{file}", ".html|"},
|
|
||||||
{"/abcde/", "ab", "/a#{fragment}", ".html|"},
|
|
||||||
{"/ab/", `.*\.jpg`, "/ajpg", ""},
|
|
||||||
{"/reggrp", `/ad/([0-9]+)([a-z]*)`, "/a{1}/{2}", ""},
|
|
||||||
{"/reg2grp", `(.*)`, "/{1}", ""},
|
|
||||||
{"/reg3grp", `(.*)/(.*)/(.*)`, "/{1}{2}{3}", ""},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, regexpRule := range regexps {
|
|
||||||
var ext []string
|
|
||||||
if s := strings.Split(regexpRule[3], "|"); len(s) > 1 {
|
|
||||||
ext = s[:len(s)-1]
|
|
||||||
}
|
|
||||||
rule, err := NewComplexRule(regexpRule[0], regexpRule[1], regexpRule[2], 0, ext, nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
rw.Rules = append(rw.Rules, rule)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
from string
|
from string
|
||||||
expectedTo string
|
fromT uint16
|
||||||
|
fromC uint16
|
||||||
|
to string
|
||||||
|
toT uint16
|
||||||
|
toC uint16
|
||||||
}{
|
}{
|
||||||
{"/from", "/to"},
|
{"from.nl.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET},
|
||||||
{"/a", "/b"},
|
{"a.nl.", dns.TypeA, dns.ClassINET, "a.nl.", dns.TypeA, dns.ClassINET},
|
||||||
{"/b", "/b/b"},
|
{"a.nl.", dns.TypeA, dns.ClassCHAOS, "a.nl.", dns.TypeA, dns.ClassINET},
|
||||||
{"/aa", "/aa"},
|
{"a.nl.", dns.TypeANY, dns.ClassINET, "a.nl.", dns.TypeHINFO, dns.ClassINET},
|
||||||
{"/", "/"},
|
// name is rewritten, type is not.
|
||||||
{"/a?foo=bar", "/b?foo=bar"},
|
{"from.nl.", dns.TypeANY, dns.ClassINET, "to.nl.", dns.TypeANY, dns.ClassINET},
|
||||||
{"/asdf?foo=bar", "/asdf?foo=bar"},
|
// name is not, type is, but class is, because class is the 2nd rule.
|
||||||
{"/foo#bar", "/foo#bar"},
|
{"a.nl.", dns.TypeANY, dns.ClassCHAOS, "a.nl.", dns.TypeANY, dns.ClassINET},
|
||||||
{"/a#foo", "/b#foo"},
|
|
||||||
{"/reg/foo", "/to"},
|
|
||||||
{"/re", "/re"},
|
|
||||||
{"/r/", "/r/"},
|
|
||||||
{"/r/123", "/r/123"},
|
|
||||||
{"/r/a123", "/toaz"},
|
|
||||||
{"/r/abcz", "/toaz"},
|
|
||||||
{"/r/z", "/toaz"},
|
|
||||||
{"/r/z.html", "/r/z.html"},
|
|
||||||
{"/r/z.js", "/toaz"},
|
|
||||||
{"/url/asAB", "/to/url/asAB"},
|
|
||||||
{"/url/aBsAB", "/url/aBsAB"},
|
|
||||||
{"/url/a00sAB", "/to/url/a00sAB"},
|
|
||||||
{"/url/a0z0sAB", "/to/url/a0z0sAB"},
|
|
||||||
{"/ab/aa", "/ab/aa"},
|
|
||||||
{"/ab/ab", "/ab/ab"},
|
|
||||||
{"/ab/ab.txt", "/ab"},
|
|
||||||
{"/ab/ab.txt?name=name", "/ab?name=name"},
|
|
||||||
{"/ab/ab.html?name=name", "/ab?type=html&name=name"},
|
|
||||||
{"/abc/ab.html", "/abc/ab.html"},
|
|
||||||
{"/abcd/abcd.html", "/a/abcd/abcd.html"},
|
|
||||||
{"/abcde/abcde.html", "/a"},
|
|
||||||
{"/abcde/abcde.html#1234", "/a#1234"},
|
|
||||||
{"/ab/ab.jpg", "/ajpg"},
|
|
||||||
{"/reggrp/ad/12", "/a12"},
|
|
||||||
{"/reggrp/ad/124a", "/a124/a"},
|
|
||||||
{"/reggrp/ad/124abc", "/a124/abc"},
|
|
||||||
{"/reg2grp/ad/124abc", "/ad/124abc"},
|
|
||||||
{"/reg3grp/ad/aa/66", "/adaa66"},
|
|
||||||
{"/reg3grp/ad612/n1n/ab", "/ad612n1nab"},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx := context.TODO()
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
req, err := http.NewRequest("GET", test.from, nil)
|
m := new(dns.Msg)
|
||||||
if err != nil {
|
m.SetQuestion(test.from, test.fromT)
|
||||||
t.Fatalf("Test %d: Could not create HTTP request: %v", i, err)
|
m.Question[0].Qclass = test.fromC
|
||||||
|
|
||||||
|
rec := middleware.NewResponseRecorder(&coretest.ResponseWriter{})
|
||||||
|
rw.ServeDNS(ctx, rec, m)
|
||||||
|
resp := rec.Msg()
|
||||||
|
|
||||||
|
if resp.Question[0].Name != test.to {
|
||||||
|
t.Errorf("Test %d: Expected Name to be '%s' but was '%s'", i, test.to, resp.Question[0].Name)
|
||||||
}
|
}
|
||||||
|
if resp.Question[0].Qtype != test.toT {
|
||||||
rec := httptest.NewRecorder()
|
t.Errorf("Test %d: Expected Type to be '%d' but was '%d'", i, test.toT, resp.Question[0].Qtype)
|
||||||
rw.ServeHTTP(rec, req)
|
}
|
||||||
|
if resp.Question[0].Qclass != test.toC {
|
||||||
if rec.Body.String() != test.expectedTo {
|
t.Errorf("Test %d: Expected Class to be '%d' but was '%d'", i, test.toC, resp.Question[0].Qclass)
|
||||||
t.Errorf("Test %d: Expected URL to be '%s' but was '%s'",
|
|
||||||
i, test.expectedTo, rec.Body.String())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
statusTests := []struct {
|
/*
|
||||||
status int
|
regexps := [][]string{
|
||||||
base string
|
{"/reg/", ".*", "/to", ""},
|
||||||
to string
|
{"/r/", "[a-z]+", "/toaz", "!.html|"},
|
||||||
regexp string
|
{"/url/", "a([a-z0-9]*)s([A-Z]{2})", "/to/{path}", ""},
|
||||||
statusExpected bool
|
{"/ab/", "ab", "/ab?{query}", ".txt|"},
|
||||||
}{
|
{"/ab/", "ab", "/ab?type=html&{query}", ".html|"},
|
||||||
{400, "/status", "", "", true},
|
{"/abc/", "ab", "/abc/{file}", ".html|"},
|
||||||
{400, "/ignore", "", "", false},
|
{"/abcd/", "ab", "/a/{dir}/{file}", ".html|"},
|
||||||
{400, "/", "", "^/ignore", false},
|
{"/abcde/", "ab", "/a#{fragment}", ".html|"},
|
||||||
{400, "/", "", "(.*)", true},
|
{"/ab/", `.*\.jpg`, "/ajpg", ""},
|
||||||
{400, "/status", "", "", true},
|
{"/reggrp", `/ad/([0-9]+)([a-z]*)`, "/a{1}/{2}", ""},
|
||||||
}
|
{"/reg2grp", `(.*)`, "/{1}", ""},
|
||||||
|
{"/reg3grp", `(.*)/(.*)/(.*)`, "/{1}{2}{3}", ""},
|
||||||
for i, s := range statusTests {
|
|
||||||
urlPath := fmt.Sprintf("/status%d", i)
|
|
||||||
rule, err := NewComplexRule(s.base, s.regexp, s.to, s.status, nil, nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Test %d: No error expected for rule but found %v", i, err)
|
|
||||||
}
|
|
||||||
rw.Rules = []Rule{rule}
|
|
||||||
req, err := http.NewRequest("GET", urlPath, nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Test %d: Could not create HTTP request: %v", i, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rec := httptest.NewRecorder()
|
for _, regexpRule := range regexps {
|
||||||
code, err := rw.ServeHTTP(rec, req)
|
var ext []string
|
||||||
if err != nil {
|
if s := strings.Split(regexpRule[3], "|"); len(s) > 1 {
|
||||||
t.Fatalf("Test %d: No error expected for handler but found %v", i, err)
|
ext = s[:len(s)-1]
|
||||||
|
}
|
||||||
|
rule, err := NewComplexRule(regexpRule[0], regexpRule[1], regexpRule[2], 0, ext, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
rw.Rules = append(rw.Rules, rule)
|
||||||
}
|
}
|
||||||
if s.statusExpected {
|
*/
|
||||||
if rec.Body.String() != "" {
|
/*
|
||||||
t.Errorf("Test %d: Expected empty body but found %s", i, rec.Body.String())
|
statusTests := []struct {
|
||||||
|
status int
|
||||||
|
base string
|
||||||
|
to string
|
||||||
|
regexp string
|
||||||
|
statusExpected bool
|
||||||
|
}{
|
||||||
|
{400, "/status", "", "", true},
|
||||||
|
{400, "/ignore", "", "", false},
|
||||||
|
{400, "/", "", "^/ignore", false},
|
||||||
|
{400, "/", "", "(.*)", true},
|
||||||
|
{400, "/status", "", "", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, s := range statusTests {
|
||||||
|
urlPath := fmt.Sprintf("/status%d", i)
|
||||||
|
rule, err := NewComplexRule(s.base, s.regexp, s.to, s.status, nil, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Test %d: No error expected for rule but found %v", i, err)
|
||||||
}
|
}
|
||||||
if code != s.status {
|
rw.Rules = []Rule{rule}
|
||||||
t.Errorf("Test %d: Expected status code %d found %d", i, s.status, code)
|
req, err := http.NewRequest("GET", urlPath, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Test %d: Could not create HTTP request: %v", i, err)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if code != 0 {
|
rec := httptest.NewRecorder()
|
||||||
t.Errorf("Test %d: Expected no status code found %d", i, code)
|
code, err := rw.ServeHTTP(rec, req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Test %d: No error expected for handler but found %v", i, err)
|
||||||
|
}
|
||||||
|
if s.statusExpected {
|
||||||
|
if rec.Body.String() != "" {
|
||||||
|
t.Errorf("Test %d: Expected empty body but found %s", i, rec.Body.String())
|
||||||
|
}
|
||||||
|
if code != s.status {
|
||||||
|
t.Errorf("Test %d: Expected status code %d found %d", i, s.status, code)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if code != 0 {
|
||||||
|
t.Errorf("Test %d: Expected no status code found %d", i, code)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
func urlPrinter(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
||||||
fmt.Fprintf(w, r.URL.String())
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|||||||
Reference in New Issue
Block a user