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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Tests that the scheme, host, and port of the server are correctly recorded
* and used in HTTP requests and responses.
*/
const PORT = 4444;
const FAKE_PORT_ONE = 8888;
const FAKE_PORT_TWO = 8889;
var srv, id;
function run_test()
{
dumpn("*** run_test");
srv = createServer();
srv.registerPathHandler("/http/1.0-request", http10Request);
srv.registerPathHandler("/http/1.1-good-host", http11goodHost);
srv.registerPathHandler("/http/1.1-good-host-wacky-port",
http11goodHostWackyPort);
srv.registerPathHandler("/http/1.1-ip-host", http11ipHost);
srv.start(FAKE_PORT_ONE);
id = srv.identity;
// The default location is http://localhost:PORT, where PORT is whatever you
// provided when you started the server. http://127.0.0.1:PORT is also part
// of the default set of locations.
do_check_eq(id.primaryScheme, "http");
do_check_eq(id.primaryHost, "localhost");
do_check_eq(id.primaryPort, FAKE_PORT_ONE);
do_check_true(id.has("http", "localhost", FAKE_PORT_ONE));
do_check_true(id.has("http", "127.0.0.1", FAKE_PORT_ONE));
// This should be a nop.
id.add("http", "localhost", FAKE_PORT_ONE);
do_check_eq(id.primaryScheme, "http");
do_check_eq(id.primaryHost, "localhost");
do_check_eq(id.primaryPort, FAKE_PORT_ONE);
do_check_true(id.has("http", "localhost", FAKE_PORT_ONE));
do_check_true(id.has("http", "127.0.0.1", FAKE_PORT_ONE));
// Change the primary location and make sure all the getters work correctly.
id.setPrimary("http", "127.0.0.1", FAKE_PORT_ONE);
do_check_eq(id.primaryScheme, "http");
do_check_eq(id.primaryHost, "127.0.0.1");
do_check_eq(id.primaryPort, FAKE_PORT_ONE);
do_check_true(id.has("http", "localhost", FAKE_PORT_ONE));
do_check_true(id.has("http", "127.0.0.1", FAKE_PORT_ONE));
// Okay, now remove the primary location -- we fall back to the original
// location.
id.remove("http", "127.0.0.1", FAKE_PORT_ONE);
do_check_eq(id.primaryScheme, "http");
do_check_eq(id.primaryHost, "localhost");
do_check_eq(id.primaryPort, FAKE_PORT_ONE);
do_check_true(id.has("http", "localhost", FAKE_PORT_ONE));
do_check_false(id.has("http", "127.0.0.1", FAKE_PORT_ONE));
// You can't remove every location -- try this and the original default
// location will be silently readded.
id.remove("http", "localhost", FAKE_PORT_ONE);
do_check_eq(id.primaryScheme, "http");
do_check_eq(id.primaryHost, "localhost");
do_check_eq(id.primaryPort, FAKE_PORT_ONE);
do_check_true(id.has("http", "localhost", FAKE_PORT_ONE));
do_check_false(id.has("http", "127.0.0.1", FAKE_PORT_ONE));
// Okay, now that we've exercised that behavior, shut down the server and
// restart it on the correct port, to exercise port-changing behaviors at
// server start and stop.
do_test_pending();
srv.stop(function()
{
try
{
do_test_pending();
run_test_2();
}
finally
{
do_test_finished();
}
});
}
function run_test_2()
{
dumpn("*** run_test_2");
do_test_finished();
// Our primary location is gone because it was dependent on the port on which
// the server was running.
checkPrimariesThrow(id);
do_check_false(id.has("http", "127.0.0.1", FAKE_PORT_ONE));
do_check_false(id.has("http", "localhost", FAKE_PORT_ONE));
srv.start(FAKE_PORT_TWO);
// We should have picked up http://localhost:8889 as our primary location now
// that we've restarted.
do_check_eq(id.primaryScheme, "http");
do_check_eq(id.primaryHost, "localhost", FAKE_PORT_TWO);
do_check_eq(id.primaryPort, FAKE_PORT_TWO);
do_check_false(id.has("http", "localhost", FAKE_PORT_ONE));
do_check_false(id.has("http", "127.0.0.1", FAKE_PORT_ONE));
do_check_true(id.has("http", "localhost", FAKE_PORT_TWO));
do_check_true(id.has("http", "127.0.0.1", FAKE_PORT_TWO));
// Now we're going to see what happens when we shut down with a primary
// location that wasn't a default. That location should persist, and the
// default we remove should still not be present.
id.setPrimary("http", "example.com", FAKE_PORT_TWO);
do_check_true(id.has("http", "example.com", FAKE_PORT_TWO));
do_check_true(id.has("http", "127.0.0.1", FAKE_PORT_TWO));
do_check_true(id.has("http", "localhost", FAKE_PORT_TWO));
do_check_false(id.has("http", "127.0.0.1", FAKE_PORT_ONE));
do_check_false(id.has("http", "localhost", FAKE_PORT_ONE));
id.remove("http", "localhost", FAKE_PORT_TWO);
do_check_true(id.has("http", "example.com", FAKE_PORT_TWO));
do_check_false(id.has("http", "localhost", FAKE_PORT_TWO));
do_check_true(id.has("http", "127.0.0.1", FAKE_PORT_TWO));
do_check_false(id.has("http", "localhost", FAKE_PORT_ONE));
do_check_false(id.has("http", "127.0.0.1", FAKE_PORT_ONE));
id.remove("http", "127.0.0.1", FAKE_PORT_TWO);
do_check_true(id.has("http", "example.com", FAKE_PORT_TWO));
do_check_false(id.has("http", "localhost", FAKE_PORT_TWO));
do_check_false(id.has("http", "127.0.0.1", FAKE_PORT_TWO));
do_check_false(id.has("http", "localhost", FAKE_PORT_ONE));
do_check_false(id.has("http", "127.0.0.1", FAKE_PORT_ONE));
do_test_pending();
srv.stop(function()
{
try
{
do_test_pending();
run_test_3();
}
finally
{
do_test_finished();
}
});
}
function run_test_3()
{
dumpn("*** run_test_3");
do_test_finished();
// Only the default added location disappears; any others stay around,
// possibly as the primary location. We may have removed the default primary
// location, but the one we set manually should persist here.
do_check_eq(id.primaryScheme, "http");
do_check_eq(id.primaryHost, "example.com");
do_check_eq(id.primaryPort, FAKE_PORT_TWO);
do_check_true(id.has("http", "example.com", FAKE_PORT_TWO));
do_check_false(id.has("http", "localhost", FAKE_PORT_TWO));
do_check_false(id.has("http", "127.0.0.1", FAKE_PORT_TWO));
do_check_false(id.has("http", "localhost", FAKE_PORT_ONE));
do_check_false(id.has("http", "127.0.0.1", FAKE_PORT_ONE));
srv.start(PORT);
// Starting always adds HTTP entries for 127.0.0.1:port and localhost:port.
do_check_true(id.has("http", "example.com", FAKE_PORT_TWO));
do_check_false(id.has("http", "localhost", FAKE_PORT_TWO));
do_check_false(id.has("http", "127.0.0.1", FAKE_PORT_TWO));
do_check_false(id.has("http", "localhost", FAKE_PORT_ONE));
do_check_false(id.has("http", "127.0.0.1", FAKE_PORT_ONE));
do_check_true(id.has("http", "localhost", PORT));
do_check_true(id.has("http", "127.0.0.1", PORT));
// Remove the primary location we'd left set from last time.
id.remove("http", "example.com", FAKE_PORT_TWO);
// Default-port behavior testing requires the server responds to requests
// claiming to be on one such port.
id.add("http", "localhost", 80);
// Make sure we don't have anything lying around from running on either the
// first or the second port -- all we should have is our generated default,
// plus the additional port to test "portless" hostport variants.
do_check_true(id.has("http", "localhost", 80));
do_check_eq(id.primaryScheme, "http");
do_check_eq(id.primaryHost, "localhost");
do_check_eq(id.primaryPort, PORT);
do_check_true(id.has("http", "localhost", PORT));
do_check_true(id.has("http", "127.0.0.1", PORT));
do_check_false(id.has("http", "localhost", FAKE_PORT_ONE));
do_check_false(id.has("http", "127.0.0.1", FAKE_PORT_ONE));
do_check_false(id.has("http", "example.com", FAKE_PORT_TWO));
do_check_false(id.has("http", "localhost", FAKE_PORT_TWO));
do_check_false(id.has("http", "127.0.0.1", FAKE_PORT_TWO));
// Okay, finally done with identity testing. Our primary location is the one
// we want it to be, so we're off!
runRawTests(tests, testComplete(srv));
}
/*********************
* UTILITY FUNCTIONS *
*********************/
/**
* Verifies that all .primary* getters on a server identity correctly throw
* NS_ERROR_NOT_INITIALIZED.
*
* @param id : nsIHttpServerIdentity
* the server identity to test
*/
function checkPrimariesThrow(id)
{
var threw = false;
try
{
id.primaryScheme;
}
catch (e)
{
threw = e === Cr.NS_ERROR_NOT_INITIALIZED;
}
do_check_true(threw);
threw = false;
try
{
id.primaryHost;
}
catch (e)
{
threw = e === Cr.NS_ERROR_NOT_INITIALIZED;
}
do_check_true(threw);
threw = false;
try
{
id.primaryPort;
}
catch (e)
{
threw = e === Cr.NS_ERROR_NOT_INITIALIZED;
}
do_check_true(threw);
}
/**
* Utility function to check for a 400 response.
*/
function check400(data)
{
var iter = LineIterator(data);
// Status-Line
var firstLine = iter.next();
do_check_eq(firstLine.substring(0, HTTP_400_LEADER_LENGTH), HTTP_400_LEADER);
}
/***************
* BEGIN TESTS *
***************/
const HTTP_400_LEADER = "HTTP/1.1 400 ";
const HTTP_400_LEADER_LENGTH = HTTP_400_LEADER.length;
var test, data;
var tests = [];
// HTTP/1.0 request, to ensure we see our default scheme/host/port
function http10Request(request, response)
{
writeDetails(request, response);
response.setStatusLine("1.0", 200, "TEST PASSED");
}
data = "GET /http/1.0-request HTTP/1.0\r\n" +
"\r\n";
function check10(data)
{
var iter = LineIterator(data);
// Status-Line
do_check_eq(iter.next(), "HTTP/1.0 200 TEST PASSED");
skipHeaders(iter);
// Okay, next line must be the data we expected to be written
var body =
[
"Method: GET",
"Path: /http/1.0-request",
"Query: ",
"Version: 1.0",
"Scheme: http",
"Host: localhost",
"Port: 4444",
];
expectLines(iter, body);
}
test = new RawTest("localhost", PORT, data, check10),
tests.push(test);
// HTTP/1.1 request, no Host header, expect a 400 response
data = "GET /http/1.1-request HTTP/1.1\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check400),
tests.push(test);
// HTTP/1.1 request, wrong host, expect a 400 response
data = "GET /http/1.1-request HTTP/1.1\r\n" +
"Host: not-localhost\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check400),
tests.push(test);
// HTTP/1.1 request, wrong host/right port, expect a 400 response
data = "GET /http/1.1-request HTTP/1.1\r\n" +
"Host: not-localhost:4444\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check400),
tests.push(test);
// HTTP/1.1 request, Host header has host but no port, expect a 400 response
data = "GET /http/1.1-request HTTP/1.1\r\n" +
"Host: 127.0.0.1\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check400),
tests.push(test);
// HTTP/1.1 request, Request-URI has wrong port, expect a 400 response
data = "GET http://127.0.0.1/http/1.1-request HTTP/1.1\r\n" +
"Host: 127.0.0.1\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check400),
tests.push(test);
// HTTP/1.1 request, Request-URI has wrong port, expect a 400 response
data = "GET http://localhost:31337/http/1.1-request HTTP/1.1\r\n" +
"Host: localhost:31337\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check400),
tests.push(test);
// HTTP/1.1 request, Request-URI has wrong scheme, expect a 400 response
data = "GET https://localhost:4444/http/1.1-request HTTP/1.1\r\n" +
"Host: localhost:4444\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check400),
tests.push(test);
// HTTP/1.1 request, correct Host header, expect handler's response
function http11goodHost(request, response)
{
writeDetails(request, response);
response.setStatusLine("1.1", 200, "TEST PASSED");
}
data = "GET /http/1.1-good-host HTTP/1.1\r\n" +
"Host: localhost:4444\r\n" +
"\r\n";
function check11goodHost(data)
{
var iter = LineIterator(data);
// Status-Line
do_check_eq(iter.next(), "HTTP/1.1 200 TEST PASSED");
skipHeaders(iter);
// Okay, next line must be the data we expected to be written
var body =
[
"Method: GET",
"Path: /http/1.1-good-host",
"Query: ",
"Version: 1.1",
"Scheme: http",
"Host: localhost",
"Port: 4444",
];
expectLines(iter, body);
}
test = new RawTest("localhost", PORT, data, check11goodHost),
tests.push(test);
// HTTP/1.1 request, Host header is secondary identity
function http11ipHost(request, response)
{
writeDetails(request, response);
response.setStatusLine("1.1", 200, "TEST PASSED");
}
data = "GET /http/1.1-ip-host HTTP/1.1\r\n" +
"Host: 127.0.0.1:4444\r\n" +
"\r\n";
function check11ipHost(data)
{
var iter = LineIterator(data);
// Status-Line
do_check_eq(iter.next(), "HTTP/1.1 200 TEST PASSED");
skipHeaders(iter);
// Okay, next line must be the data we expected to be written
var body =
[
"Method: GET",
"Path: /http/1.1-ip-host",
"Query: ",
"Version: 1.1",
"Scheme: http",
"Host: 127.0.0.1",
"Port: 4444",
];
expectLines(iter, body);
}
test = new RawTest("localhost", PORT, data, check11ipHost),
tests.push(test);
// HTTP/1.1 request, absolute path, accurate Host header
// reusing previous request handler so not defining a new one
data = "GET http://localhost:4444/http/1.1-good-host HTTP/1.1\r\n" +
"Host: localhost:4444\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check11goodHost),
tests.push(test);
// HTTP/1.1 request, absolute path, inaccurate Host header
// reusing previous request handler so not defining a new one
data = "GET http://localhost:4444/http/1.1-good-host HTTP/1.1\r\n" +
"Host: localhost:1234\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check11goodHost),
tests.push(test);
// HTTP/1.1 request, absolute path, different inaccurate Host header
// reusing previous request handler so not defining a new one
data = "GET http://localhost:4444/http/1.1-good-host HTTP/1.1\r\n" +
"Host: not-localhost:4444\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check11goodHost),
tests.push(test);
// HTTP/1.1 request, absolute path, yet another inaccurate Host header
// reusing previous request handler so not defining a new one
data = "GET http://localhost:4444/http/1.1-good-host HTTP/1.1\r\n" +
"Host: yippity-skippity\r\n" +
"\r\n";
function checkInaccurate(data)
{
check11goodHost(data);
// dynamism setup
srv.identity.setPrimary("http", "127.0.0.1", 4444);
}
test = new RawTest("localhost", PORT, data, checkInaccurate),
tests.push(test);
// HTTP/1.0 request, absolute path, different inaccurate Host header
// reusing previous request handler so not defining a new one
data = "GET /http/1.0-request HTTP/1.0\r\n" +
"Host: not-localhost:4444\r\n" +
"\r\n";
function check10ip(data)
{
var iter = LineIterator(data);
// Status-Line
do_check_eq(iter.next(), "HTTP/1.0 200 TEST PASSED");
skipHeaders(iter);
// Okay, next line must be the data we expected to be written
var body =
[
"Method: GET",
"Path: /http/1.0-request",
"Query: ",
"Version: 1.0",
"Scheme: http",
"Host: 127.0.0.1",
"Port: 4444",
];
expectLines(iter, body);
}
test = new RawTest("localhost", PORT, data, check10ip),
tests.push(test);
// HTTP/1.1 request, Host header with implied port
function http11goodHostWackyPort(request, response)
{
writeDetails(request, response);
response.setStatusLine("1.1", 200, "TEST PASSED");
}
data = "GET /http/1.1-good-host-wacky-port HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"\r\n";
function check11goodHostWackyPort(data)
{
var iter = LineIterator(data);
// Status-Line
do_check_eq(iter.next(), "HTTP/1.1 200 TEST PASSED");
skipHeaders(iter);
// Okay, next line must be the data we expected to be written
var body =
[
"Method: GET",
"Path: /http/1.1-good-host-wacky-port",
"Query: ",
"Version: 1.1",
"Scheme: http",
"Host: localhost",
"Port: 80",
];
expectLines(iter, body);
}
test = new RawTest("localhost", PORT, data, check11goodHostWackyPort),
tests.push(test);
// HTTP/1.1 request, Host header with wacky implied port
data = "GET /http/1.1-good-host-wacky-port HTTP/1.1\r\n" +
"Host: localhost:\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check11goodHostWackyPort),
tests.push(test);
// HTTP/1.1 request, absolute URI with implied port
data = "GET http://localhost/http/1.1-good-host-wacky-port HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check11goodHostWackyPort),
tests.push(test);
// HTTP/1.1 request, absolute URI with wacky implied port
data = "GET http://localhost:/http/1.1-good-host-wacky-port HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check11goodHostWackyPort),
tests.push(test);
// HTTP/1.1 request, absolute URI with explicit implied port, ignored Host
data = "GET http://localhost:80/http/1.1-good-host-wacky-port HTTP/1.1\r\n" +
"Host: who-cares\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check11goodHostWackyPort),
tests.push(test);
// HTTP/1.1 request, a malformed Request-URI
data = "GET is-this-the-real-life-is-this-just-fantasy HTTP/1.1\r\n" +
"Host: localhost:4444\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check400),
tests.push(test);
// HTTP/1.1 request, a malformed Host header
data = "GET /http/1.1-request HTTP/1.1\r\n" +
"Host: la la la\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check400),
tests.push(test);
// HTTP/1.1 request, a malformed Host header but absolute URI, 5.2 sez fine
data = "GET http://localhost:4444/http/1.1-good-host HTTP/1.1\r\n" +
"Host: la la la\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check11goodHost),
tests.push(test);
// HTTP/1.0 request, absolute URI, but those aren't valid in HTTP/1.0
data = "GET http://localhost:4444/http/1.1-request HTTP/1.0\r\n" +
"Host: localhost:4444\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check400),
tests.push(test);
// HTTP/1.1 request, absolute URI with unrecognized host
data = "GET http://not-localhost:4444/http/1.1-request HTTP/1.1\r\n" +
"Host: not-localhost:4444\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check400),
tests.push(test);
// HTTP/1.1 request, absolute URI with unrecognized host (but not in Host)
data = "GET http://not-localhost:4444/http/1.1-request HTTP/1.1\r\n" +
"Host: localhost:4444\r\n" +
"\r\n";
test = new RawTest("localhost", PORT, data, check400),
tests.push(test);
|