This commit is contained in:
parent
9a393ac5c8
commit
f51c110511
2 changed files with 29 additions and 1 deletions
|
@ -71,7 +71,7 @@ func (rule *exactNameRule) Rewrite(ctx context.Context, state request.Request) R
|
||||||
// Rewrite rewrites the current request when the name begins with the matching string.
|
// Rewrite rewrites the current request when the name begins with the matching string.
|
||||||
func (rule *prefixNameRule) Rewrite(ctx context.Context, state request.Request) Result {
|
func (rule *prefixNameRule) Rewrite(ctx context.Context, state request.Request) Result {
|
||||||
if strings.HasPrefix(state.Name(), rule.Prefix) {
|
if strings.HasPrefix(state.Name(), rule.Prefix) {
|
||||||
state.Req.Question[0].Name = rule.Replacement + strings.TrimLeft(state.Name(), rule.Prefix)
|
state.Req.Question[0].Name = rule.Replacement + strings.TrimPrefix(state.Name(), rule.Prefix)
|
||||||
return RewriteDone
|
return RewriteDone
|
||||||
}
|
}
|
||||||
return RewriteIgnored
|
return RewriteIgnored
|
||||||
|
|
|
@ -32,6 +32,34 @@ func TestRewriteIllegalName(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRewriteNamePrefix(t *testing.T) {
|
||||||
|
r, err := newNameRule("stop", "prefix", "test", "not-a-test")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Expected no error, got %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rw := Rewrite{
|
||||||
|
Next: plugin.HandlerFunc(msgPrinter),
|
||||||
|
Rules: []Rule{r},
|
||||||
|
noRevert: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.TODO()
|
||||||
|
m := new(dns.Msg)
|
||||||
|
m.SetQuestion("test.example.org.", dns.TypeA)
|
||||||
|
|
||||||
|
rec := dnstest.NewRecorder(&test.ResponseWriter{})
|
||||||
|
_, err = rw.ServeDNS(ctx, rec, m)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Expected no error, got %s", err)
|
||||||
|
}
|
||||||
|
expected := "not-a-test.example.org."
|
||||||
|
actual := rec.Msg.Question[0].Name
|
||||||
|
if actual != expected {
|
||||||
|
t.Fatalf("Expected rewrite to %v, got %v", expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewNameRule(t *testing.T) {
|
func TestNewNameRule(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
next string
|
next string
|
||||||
|
|
Loading…
Add table
Reference in a new issue