mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 10:13:14 -04:00 
			
		
		
		
	This commit is contained in:
		
				
					committed by
					
						 Miek Gieben
						Miek Gieben
					
				
			
			
				
	
			
			
			
						parent
						
							fc667b98e0
						
					
				
				
					commit
					4a3f5cc41e
				
			| @@ -80,7 +80,7 @@ func (rule *prefixNameRule) Rewrite(ctx context.Context, state request.Request) | |||||||
| // Rewrite rewrites the current request when the name ends with the matching string. | // Rewrite rewrites the current request when the name ends with the matching string. | ||||||
| func (rule *suffixNameRule) Rewrite(ctx context.Context, state request.Request) Result { | func (rule *suffixNameRule) Rewrite(ctx context.Context, state request.Request) Result { | ||||||
| 	if strings.HasSuffix(state.Name(), rule.Suffix) { | 	if strings.HasSuffix(state.Name(), rule.Suffix) { | ||||||
| 		state.Req.Question[0].Name = strings.TrimRight(state.Name(), rule.Suffix) + rule.Replacement | 		state.Req.Question[0].Name = strings.TrimSuffix(state.Name(), rule.Suffix) + rule.Replacement | ||||||
| 		return RewriteDone | 		return RewriteDone | ||||||
| 	} | 	} | ||||||
| 	return RewriteIgnored | 	return RewriteIgnored | ||||||
|   | |||||||
| @@ -32,31 +32,46 @@ func TestRewriteIllegalName(t *testing.T) { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func TestRewriteNamePrefix(t *testing.T) { | func TestRewriteNamePrefixSuffix(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{ | 	ctx, close := context.WithCancel(context.TODO()) | ||||||
| 		Next:     plugin.HandlerFunc(msgPrinter), | 	defer close() | ||||||
| 		Rules:    []Rule{r}, |  | ||||||
| 		noRevert: true, |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	ctx := context.TODO() | 	tests := []struct { | ||||||
| 	m := new(dns.Msg) | 		next     string | ||||||
| 	m.SetQuestion("test.example.org.", dns.TypeA) | 		args     []string | ||||||
|  | 		question string | ||||||
| 	rec := dnstest.NewRecorder(&test.ResponseWriter{}) | 		expected string | ||||||
| 	_, err = rw.ServeDNS(ctx, rec, m) | 	}{ | ||||||
| 	if err != nil { | 		{"stop", []string{"prefix", "foo", "bar"}, "foo.example.com.", "bar.example.com."}, | ||||||
| 		t.Fatalf("Expected no error, got %s", err) | 		{"stop", []string{"prefix", "foo.", "bar."}, "foo.example.com.", "bar.example.com."}, | ||||||
|  | 		{"stop", []string{"suffix", "com", "org"}, "foo.example.com.", "foo.example.org."}, | ||||||
|  | 		{"stop", []string{"suffix", ".com", ".org"}, "foo.example.com.", "foo.example.org."}, | ||||||
| 	} | 	} | ||||||
| 	expected := "not-a-test.example.org." | 	for _, tc := range tests { | ||||||
| 	actual := rec.Msg.Question[0].Name | 		r, err := newNameRule(tc.next, tc.args...) | ||||||
| 	if actual != expected { | 		if err != nil { | ||||||
| 		t.Fatalf("Expected rewrite to %v, got %v", expected, actual) | 			t.Fatalf("Expected no error, got %s", err) | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		rw := Rewrite{ | ||||||
|  | 			Next:     plugin.HandlerFunc(msgPrinter), | ||||||
|  | 			Rules:    []Rule{r}, | ||||||
|  | 			noRevert: true, | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		m := new(dns.Msg) | ||||||
|  | 		m.SetQuestion(tc.question, dns.TypeA) | ||||||
|  |  | ||||||
|  | 		rec := dnstest.NewRecorder(&test.ResponseWriter{}) | ||||||
|  | 		_, err = rw.ServeDNS(ctx, rec, m) | ||||||
|  | 		if err != nil { | ||||||
|  | 			t.Fatalf("Expected no error, got %s", err) | ||||||
|  | 		} | ||||||
|  | 		actual := rec.Msg.Question[0].Name | ||||||
|  | 		if actual != tc.expected { | ||||||
|  | 			t.Fatalf("Expected rewrite to %v, got %v", tc.expected, actual) | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user