michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const port = 8099; michael@0: const file = require("sdk/io/file"); michael@0: const { pathFor } = require("sdk/system"); michael@0: const { Loader } = require("sdk/test/loader"); michael@0: const options = require("@test/options"); michael@0: michael@0: const loader = Loader(module); michael@0: const httpd = loader.require("sdk/test/httpd"); michael@0: if (options.parseable || options.verbose) michael@0: loader.sandbox("sdk/test/httpd").DEBUG = true; michael@0: michael@0: exports.testBasicHTTPServer = function(assert, done) { michael@0: // Use the profile directory for the temporary file as that will be deleted michael@0: // when tests are complete michael@0: let basePath = pathFor("ProfD"); michael@0: let filePath = file.join(basePath, 'test-httpd.txt'); michael@0: let content = "This is the HTTPD test file.\n"; michael@0: let fileStream = file.open(filePath, 'w'); michael@0: fileStream.write(content); michael@0: fileStream.close(); michael@0: michael@0: let srv = httpd.startServerAsync(port, basePath); michael@0: michael@0: // Request this very file. michael@0: let Request = require('sdk/request').Request; michael@0: Request({ michael@0: url: "http://localhost:" + port + "/test-httpd.txt", michael@0: onComplete: function (response) { michael@0: assert.equal(response.text, content); michael@0: srv.stop(done); michael@0: } michael@0: }).get(); michael@0: }; michael@0: michael@0: exports.testDynamicServer = function (assert, done) { michael@0: let content = "This is the HTTPD test file.\n"; michael@0: michael@0: let srv = httpd.startServerAsync(port); michael@0: michael@0: // See documentation here: michael@0: //http://doxygen.db48x.net/mozilla/html/interfacensIHttpServer.html#a81fc7e7e29d82aac5ce7d56d0bedfb3a michael@0: //http://doxygen.db48x.net/mozilla/html/interfacensIHttpRequestHandler.html michael@0: srv.registerPathHandler("/test-httpd.txt", function handle(request, response) { michael@0: // Add text content type, only to avoid error in `Request` API michael@0: response.setHeader("Content-Type", "text/plain", false); michael@0: response.write(content); michael@0: }); michael@0: michael@0: // Request this very file. michael@0: let Request = require('sdk/request').Request; michael@0: Request({ michael@0: url: "http://localhost:" + port + "/test-httpd.txt", michael@0: onComplete: function (response) { michael@0: assert.equal(response.text, content); michael@0: srv.stop(done); michael@0: } michael@0: }).get(); michael@0: }; michael@0: michael@0: exports.testAutomaticPortSelection = function (assert, done) { michael@0: const srv = httpd.startServerAsync(-1); michael@0: michael@0: const port = srv.identity.primaryPort; michael@0: assert.ok(0 <= port && port <= 65535); michael@0: michael@0: srv.stop(done); michael@0: }; michael@0: michael@0: require('sdk/test').run(exports);