aboutsummaryrefslogtreecommitdiff
path: root/middleware/rewrite/rewrite_test.go
blob: 50aa64ad278921633b58b7f56fe6f580a74f80a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package rewrite

import (
	"testing"

	"github.com/miekg/coredns/middleware"
	"github.com/miekg/coredns/middleware/test"

	"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) {
	rw := Rewrite{
		Next: middleware.HandlerFunc(msgPrinter),
		Rules: []Rule{
			NewSimpleRule("from.nl.", "to.nl."),
			NewSimpleRule("CH", "IN"),
			NewSimpleRule("ANY", "HINFO"),
		},
		noRevert: true,
	}

	tests := []struct {
		from  string
		fromT uint16
		fromC uint16
		to    string
		toT   uint16
		toC   uint16
	}{
		{"from.nl.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET},
		{"a.nl.", dns.TypeA, dns.ClassINET, "a.nl.", dns.TypeA, dns.ClassINET},
		{"a.nl.", dns.TypeA, dns.ClassCHAOS, "a.nl.", dns.TypeA, dns.ClassINET},
		{"a.nl.", dns.TypeANY, dns.ClassINET, "a.nl.", dns.TypeHINFO, dns.ClassINET},
		// name is rewritten, type is not.
		{"from.nl.", dns.TypeANY, dns.ClassINET, "to.nl.", dns.TypeANY, dns.ClassINET},
		// name is not, type is, but class is, because class is the 2nd rule.
		{"a.nl.", dns.TypeANY, dns.ClassCHAOS, "a.nl.", dns.TypeANY, dns.ClassINET},
	}

	ctx := context.TODO()
	for i, tc := range tests {
		m := new(dns.Msg)
		m.SetQuestion(tc.from, tc.fromT)
		m.Question[0].Qclass = tc.fromC

		rec := middleware.NewResponseRecorder(&test.ResponseWriter{})
		rw.ServeDNS(ctx, rec, m)
		resp := rec.Msg()

		if resp.Question[0].Name != tc.to {
			t.Errorf("Test %d: Expected Name to be '%s' but was '%s'", i, tc.to, resp.Question[0].Name)
		}
		if resp.Question[0].Qtype != tc.toT {
			t.Errorf("Test %d: Expected Type to be '%d' but was '%d'", i, tc.toT, resp.Question[0].Qtype)
		}
		if resp.Question[0].Qclass != tc.toC {
			t.Errorf("Test %d: Expected Class to be '%d' but was '%d'", i, tc.toC, resp.Question[0].Qclass)
		}
	}

	/*
		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)
		}
	*/
	/*
		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)
			}
			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()
			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)
				}
			}
		}
	*/
}