addon-sdk/source/test/test-httpd.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-httpd.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,73 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const port = 8099;
     1.9 +const file = require("sdk/io/file");
    1.10 +const { pathFor } = require("sdk/system");
    1.11 +const { Loader } = require("sdk/test/loader");
    1.12 +const options = require("@test/options");
    1.13 +
    1.14 +const loader = Loader(module);
    1.15 +const httpd = loader.require("sdk/test/httpd");
    1.16 +if (options.parseable || options.verbose)
    1.17 +  loader.sandbox("sdk/test/httpd").DEBUG = true;
    1.18 +
    1.19 +exports.testBasicHTTPServer = function(assert, done) {
    1.20 +  // Use the profile directory for the temporary file as that will be deleted
    1.21 +  // when tests are complete
    1.22 +  let basePath = pathFor("ProfD");
    1.23 +  let filePath = file.join(basePath, 'test-httpd.txt');
    1.24 +  let content = "This is the HTTPD test file.\n";
    1.25 +  let fileStream = file.open(filePath, 'w');
    1.26 +  fileStream.write(content);
    1.27 +  fileStream.close();
    1.28 +
    1.29 +  let srv = httpd.startServerAsync(port, basePath);
    1.30 +
    1.31 +  // Request this very file.
    1.32 +  let Request = require('sdk/request').Request;
    1.33 +  Request({
    1.34 +    url: "http://localhost:" + port + "/test-httpd.txt",
    1.35 +    onComplete: function (response) {
    1.36 +      assert.equal(response.text, content);
    1.37 +      srv.stop(done);
    1.38 +    }
    1.39 +  }).get();
    1.40 +};
    1.41 +
    1.42 +exports.testDynamicServer = function (assert, done) {
    1.43 +  let content = "This is the HTTPD test file.\n";
    1.44 +
    1.45 +  let srv = httpd.startServerAsync(port);
    1.46 +
    1.47 +  // See documentation here:
    1.48 +  //http://doxygen.db48x.net/mozilla/html/interfacensIHttpServer.html#a81fc7e7e29d82aac5ce7d56d0bedfb3a
    1.49 +  //http://doxygen.db48x.net/mozilla/html/interfacensIHttpRequestHandler.html
    1.50 +  srv.registerPathHandler("/test-httpd.txt", function handle(request, response) {
    1.51 +    // Add text content type, only to avoid error in `Request` API
    1.52 +    response.setHeader("Content-Type", "text/plain", false);
    1.53 +    response.write(content);
    1.54 +  });
    1.55 +
    1.56 +  // Request this very file.
    1.57 +  let Request = require('sdk/request').Request;
    1.58 +  Request({
    1.59 +    url: "http://localhost:" + port + "/test-httpd.txt",
    1.60 +    onComplete: function (response) {
    1.61 +      assert.equal(response.text, content);
    1.62 +      srv.stop(done);
    1.63 +    }
    1.64 +  }).get();
    1.65 +};
    1.66 +
    1.67 +exports.testAutomaticPortSelection = function (assert, done) {
    1.68 +  const srv = httpd.startServerAsync(-1);
    1.69 +
    1.70 +  const port = srv.identity.primaryPort;
    1.71 +  assert.ok(0 <= port && port <= 65535);
    1.72 +
    1.73 +  srv.stop(done);
    1.74 +};
    1.75 +
    1.76 +require('sdk/test').run(exports);

mercurial