aboutsummaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
authorGravatar Daniel Garcia <daniel@danielgarcia.info> 2018-12-06 15:02:07 -0600
committerGravatar Miek Gieben <miek@miek.nl> 2018-12-06 21:02:07 +0000
commitf51c110511c13b80c7d7234c3d56dbefa79705a2 (patch)
treec43b8ecf9039ae191b7cb82a23a0102b7409936e /plugin
parent9a393ac5c8e57b7fa38c73eb2dc6a96cf51ad800 (diff)
downloadcoredns-f51c110511c13b80c7d7234c3d56dbefa79705a2.tar.gz
coredns-f51c110511c13b80c7d7234c3d56dbefa79705a2.tar.zst
coredns-f51c110511c13b80c7d7234c3d56dbefa79705a2.zip
Use TrimPrefix instead of TrimLeft in rewrite prefix plugin (#2364) (#2370)
Diffstat (limited to 'plugin')
-rw-r--r--plugin/rewrite/name.go2
-rw-r--r--plugin/rewrite/name_test.go28
2 files changed, 29 insertions, 1 deletions
diff --git a/plugin/rewrite/name.go b/plugin/rewrite/name.go
index 3be1aec4d..84ba99824 100644
--- a/plugin/rewrite/name.go
+++ b/plugin/rewrite/name.go
@@ -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.
func (rule *prefixNameRule) Rewrite(ctx context.Context, state request.Request) Result {
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 RewriteIgnored
diff --git a/plugin/rewrite/name_test.go b/plugin/rewrite/name_test.go
index fe2a2eb06..f2245f448 100644
--- a/plugin/rewrite/name_test.go
+++ b/plugin/rewrite/name_test.go
@@ -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) {
tests := []struct {
next string