|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 const port = 8099; |
|
6 const file = require("sdk/io/file"); |
|
7 const { pathFor } = require("sdk/system"); |
|
8 const { Loader } = require("sdk/test/loader"); |
|
9 const options = require("@test/options"); |
|
10 |
|
11 const loader = Loader(module); |
|
12 const httpd = loader.require("sdk/test/httpd"); |
|
13 if (options.parseable || options.verbose) |
|
14 loader.sandbox("sdk/test/httpd").DEBUG = true; |
|
15 |
|
16 exports.testBasicHTTPServer = function(assert, done) { |
|
17 // Use the profile directory for the temporary file as that will be deleted |
|
18 // when tests are complete |
|
19 let basePath = pathFor("ProfD"); |
|
20 let filePath = file.join(basePath, 'test-httpd.txt'); |
|
21 let content = "This is the HTTPD test file.\n"; |
|
22 let fileStream = file.open(filePath, 'w'); |
|
23 fileStream.write(content); |
|
24 fileStream.close(); |
|
25 |
|
26 let srv = httpd.startServerAsync(port, basePath); |
|
27 |
|
28 // Request this very file. |
|
29 let Request = require('sdk/request').Request; |
|
30 Request({ |
|
31 url: "http://localhost:" + port + "/test-httpd.txt", |
|
32 onComplete: function (response) { |
|
33 assert.equal(response.text, content); |
|
34 srv.stop(done); |
|
35 } |
|
36 }).get(); |
|
37 }; |
|
38 |
|
39 exports.testDynamicServer = function (assert, done) { |
|
40 let content = "This is the HTTPD test file.\n"; |
|
41 |
|
42 let srv = httpd.startServerAsync(port); |
|
43 |
|
44 // See documentation here: |
|
45 //http://doxygen.db48x.net/mozilla/html/interfacensIHttpServer.html#a81fc7e7e29d82aac5ce7d56d0bedfb3a |
|
46 //http://doxygen.db48x.net/mozilla/html/interfacensIHttpRequestHandler.html |
|
47 srv.registerPathHandler("/test-httpd.txt", function handle(request, response) { |
|
48 // Add text content type, only to avoid error in `Request` API |
|
49 response.setHeader("Content-Type", "text/plain", false); |
|
50 response.write(content); |
|
51 }); |
|
52 |
|
53 // Request this very file. |
|
54 let Request = require('sdk/request').Request; |
|
55 Request({ |
|
56 url: "http://localhost:" + port + "/test-httpd.txt", |
|
57 onComplete: function (response) { |
|
58 assert.equal(response.text, content); |
|
59 srv.stop(done); |
|
60 } |
|
61 }).get(); |
|
62 }; |
|
63 |
|
64 exports.testAutomaticPortSelection = function (assert, done) { |
|
65 const srv = httpd.startServerAsync(-1); |
|
66 |
|
67 const port = srv.identity.primaryPort; |
|
68 assert.ok(0 <= port && port <= 65535); |
|
69 |
|
70 srv.stop(done); |
|
71 }; |
|
72 |
|
73 require('sdk/test').run(exports); |