| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | package replacer
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 03:49:40 -07:00
										 |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-06-07 11:21:17 -04:00
										 |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2017-08-07 03:49:40 -07:00
										 |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnstest"
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/test"
 | 
					
						
							| 
									
										
										
										
											2017-08-07 03:49:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | func TestNewReplacer(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 	w := dnstest.NewRecorder(&test.ResponseWriter{})
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 03:49:40 -07:00
										 |  |  | 	r := new(dns.Msg)
 | 
					
						
							|  |  |  | 	r.SetQuestion("example.org.", dns.TypeHINFO)
 | 
					
						
							|  |  |  | 	r.MsgHdr.AuthenticatedData = true
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	replaceValues := New(r, w, "")
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	switch v := replaceValues.(type) {
 | 
					
						
							|  |  |  | 	case replacer:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 03:49:40 -07:00
										 |  |  | 		if v.replacements["{type}"] != "HINFO" {
 | 
					
						
							|  |  |  | 			t.Errorf("Expected type to be HINFO, got %q", v.replacements["{type}"])
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-08-07 03:49:40 -07:00
										 |  |  | 		if v.replacements["{name}"] != "example.org." {
 | 
					
						
							|  |  |  | 			t.Errorf("Expected request name to be example.org., got %q", v.replacements["{name}"])
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-08-07 03:49:40 -07:00
										 |  |  | 		if v.replacements["{size}"] != "29" { // size of request
 | 
					
						
							|  |  |  | 			t.Errorf("Expected size to be 29, got %q", v.replacements["{size}"])
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-06-07 11:21:17 -04:00
										 |  |  | 		if !strings.Contains(v.replacements["{duration}"], "s") {
 | 
					
						
							|  |  |  | 			t.Errorf("Expected units of time to be in seconds")
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	default:
 | 
					
						
							|  |  |  | 		t.Fatal("Return Value from New Replacer expected pass type assertion into a replacer type\n")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestSet(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 	w := dnstest.NewRecorder(&test.ResponseWriter{})
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 03:49:40 -07:00
										 |  |  | 	r := new(dns.Msg)
 | 
					
						
							|  |  |  | 	r.SetQuestion("example.org.", dns.TypeHINFO)
 | 
					
						
							|  |  |  | 	r.MsgHdr.AuthenticatedData = true
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 03:49:40 -07:00
										 |  |  | 	repl := New(r, w, "")
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 03:49:40 -07:00
										 |  |  | 	repl.Set("name", "coredns.io.")
 | 
					
						
							|  |  |  | 	repl.Set("type", "A")
 | 
					
						
							|  |  |  | 	repl.Set("size", "20")
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if repl.Replace("This name is {name}") != "This name is coredns.io." {
 | 
					
						
							|  |  |  | 		t.Error("Expected name replacement failed")
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-08-07 03:49:40 -07:00
										 |  |  | 	if repl.Replace("This type is {type}") != "This type is A" {
 | 
					
						
							|  |  |  | 		t.Error("Expected type replacement failed")
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-08-07 03:49:40 -07:00
										 |  |  | 	if repl.Replace("The request size is {size}") != "The request size is 20" {
 | 
					
						
							|  |  |  | 		t.Error("Expected size replacement failed")
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 |