1 | var dns = require('../dns'),
|
2 | Request = dns.Request,
|
3 | Question = dns.Question,
|
4 | consts = dns.consts,
|
5 | TypeMap = require('../lib/types');
|
6 |
|
7 | var q = Question({
|
8 | name: 'www.google.com',
|
9 | type: consts.NAME_TO_QTYPE.A,
|
10 | });
|
11 |
|
12 | var noServer = {
|
13 | address: '127.0.0.1',
|
14 | port: 19999,
|
15 | type: 'udp',
|
16 | };
|
17 |
|
18 | var udpServ = {
|
19 | address: '8.8.8.8',
|
20 | port: 53,
|
21 | type: 'udp',
|
22 | };
|
23 |
|
24 | var tcpServ = {
|
25 | address: '8.8.8.8',
|
26 | port: 53,
|
27 | type: 'tcp',
|
28 | };
|
29 |
|
30 | exports.setUp = function (cb) {
|
31 | cb();
|
32 | };
|
33 |
|
34 | exports.timeout = function (test) {
|
35 | var r = Request({
|
36 | question: q,
|
37 | server: noServer,
|
38 | timeout: 100,
|
39 | });
|
40 |
|
41 | var timedout = false;
|
42 |
|
43 | r.on('timeout', function () {
|
44 | timedout = true;
|
45 | });
|
46 |
|
47 | r.on('end', function () {
|
48 | test.ok(timedout, 'Failed to timeout');
|
49 | test.done();
|
50 | });
|
51 |
|
52 | r.send();
|
53 | };
|
54 |
|
55 | exports.udpResponse = function (test) {
|
56 | var r = Request({
|
57 | question: q,
|
58 | server: udpServ,
|
59 | timeout: 4000,
|
60 | });
|
61 |
|
62 | r.on('message', function (err, answer) {
|
63 | test.ok(!err, 'UDP Request should not error');
|
64 | test.ok(answer, 'No UDP answer provided');
|
65 | test.ok(answer.answer.length > 0, 'No answers found');
|
66 | });
|
67 |
|
68 | r.on('timeout', function () {
|
69 | test.ok(false, 'UDP Requests should not timeout');
|
70 | });
|
71 |
|
72 | r.on('end', function () {
|
73 | test.done();
|
74 | });
|
75 |
|
76 | r.send();
|
77 | };
|
78 |
|
79 | exports.tcpResponse = function (test) {
|
80 | var r = Request({
|
81 | question: q,
|
82 | server: tcpServ,
|
83 | timeout: 4000,
|
84 | });
|
85 |
|
86 | r.on('message', function (err, answer) {
|
87 | test.ok(!err, 'TCP Request should not error');
|
88 | test.ok(answer, 'No TCP answer provided');
|
89 | test.ok(answer.answer.length > 0, 'No answers found');
|
90 | });
|
91 |
|
92 | r.on('timeout', function () {
|
93 | test.ok(false, 'TCP Requests should not timeout');
|
94 | });
|
95 |
|
96 | r.on('end', function () {
|
97 | test.done();
|
98 | });
|
99 |
|
100 | r.send();
|
101 | };
|
102 |
|
103 | exports.autoPromote = function (test) {
|
104 | var r = Request({
|
105 | question: q,
|
106 | server: udpServ,
|
107 | timeout: 4000,
|
108 | });
|
109 |
|
110 | var PendingRequests = require('../lib/pending');
|
111 | PendingRequests.autopromote = true;
|
112 |
|
113 | r.on('message', function (err, answer) {
|
114 |
|
115 | test.ok(answer.answer.length > 0, 'no answers found');
|
116 |
|
117 | answer.answer.forEach(function (a) {
|
118 | test.ok(a instanceof TypeMap.fromQtype(a.type), 'Not an instance of derived type');
|
119 | });
|
120 | });
|
121 |
|
122 | r.on('timeout', function () {
|
123 | test.ok(false, 'Should not timeout');
|
124 | });
|
125 |
|
126 | r.on('end', function () {
|
127 | PendingRequests.autopromote = false;
|
128 | test.done();
|
129 | });
|
130 |
|
131 | r.send();
|
132 | };
|
133 |
|
134 | exports.noPromote = function (test) {
|
135 | var r = Request({
|
136 | question: q,
|
137 | server: udpServ,
|
138 | timeout: 4000,
|
139 | });
|
140 |
|
141 | r.on('message', function (err, answer) {
|
142 |
|
143 | test.ok(answer.answer.length > 0, 'no answers found');
|
144 |
|
145 | answer.answer.forEach(function (a) {
|
146 | test.ok(!(a instanceof TypeMap.fromQtype(a.type)), 'Record an instance of derived type');
|
147 | });
|
148 | });
|
149 |
|
150 | r.on('timeout', function () {
|
151 | test.ok(false, 'Should not timeout');
|
152 | });
|
153 |
|
154 | r.on('end', function () {
|
155 | test.done();
|
156 | });
|
157 |
|
158 | r.send();
|
159 | };
|
160 |
|
161 | exports.serverString = function (test) {
|
162 | var r = Request({
|
163 | question: q,
|
164 | server: '8.8.8.8',
|
165 | });
|
166 |
|
167 | r.on('message', function (err, answer) {
|
168 | test.ok(answer.answer.length > 0, 'no answers found');
|
169 | });
|
170 |
|
171 | r.on('timeout', function () {
|
172 | test.ok(false, 'Should not timeout');
|
173 | });
|
174 |
|
175 | r.on('end', function () {
|
176 | test.done();
|
177 | });
|
178 |
|
179 | r.send();
|
180 | };
|
181 |
|
182 | exports.questionString = function (test) {
|
183 | var r = Request({
|
184 | question: Question({
|
185 | name: 'www.google.com',
|
186 | type: 'a',
|
187 | }),
|
188 | server: '8.8.8.8',
|
189 | });
|
190 |
|
191 | r.on('message', function (err, answer) {
|
192 | test.ok(answer.answer.length > 0, 'no answers found');
|
193 | });
|
194 |
|
195 | r.on('timeout', function () {
|
196 | test.ok(false, 'Should not timeout');
|
197 | });
|
198 |
|
199 | r.on('end', function () {
|
200 | test.done();
|
201 | });
|
202 |
|
203 | r.send();
|
204 | };
|
205 |
|
206 | exports.tearDown = function (cb) {
|
207 | cb();
|
208 | };
|