netwerk/test/httpserver/test/test_host.js

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

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

mercurial