2009-08-05 13:19:01 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 sts=2 et
|
2012-05-21 04:12:37 -07:00
|
|
|
* 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/. */
|
2009-08-05 13:19:01 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This file tests the methods on NetUtil.jsm.
|
|
|
|
*/
|
|
|
|
|
2012-08-14 07:06:04 -07:00
|
|
|
Cu.import("resource://testing-common/httpd.js");
|
2009-10-15 16:16:01 -07:00
|
|
|
|
2014-04-10 01:00:08 -07:00
|
|
|
Cu.import("resource://gre/modules/NetUtil.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
2009-08-05 13:19:01 -07:00
|
|
|
|
2009-12-15 23:33:06 -08:00
|
|
|
// We need the profile directory so the test harness will clean up our test
|
|
|
|
// files.
|
|
|
|
do_get_profile();
|
|
|
|
|
2011-03-04 16:36:56 -08:00
|
|
|
const OUTPUT_STREAM_CONTRACT_ID = "@mozilla.org/network/file-output-stream;1";
|
|
|
|
const SAFE_OUTPUT_STREAM_CONTRACT_ID = "@mozilla.org/network/safe-file-output-stream;1";
|
|
|
|
|
2009-08-05 13:19:01 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Helper Methods
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads the contents of a file and returns it as a string.
|
|
|
|
*
|
|
|
|
* @param aFile
|
|
|
|
* The file to return from.
|
|
|
|
* @return the contents of the file in the form of a string.
|
|
|
|
*/
|
|
|
|
function getFileContents(aFile)
|
|
|
|
{
|
2014-04-10 01:00:08 -07:00
|
|
|
"use strict";
|
|
|
|
|
2009-08-05 13:19:01 -07:00
|
|
|
let fstream = Cc["@mozilla.org/network/file-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIFileInputStream);
|
|
|
|
fstream.init(aFile, -1, 0, 0);
|
|
|
|
|
|
|
|
let cstream = Cc["@mozilla.org/intl/converter-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIConverterInputStream);
|
|
|
|
cstream.init(fstream, "UTF-8", 0, 0);
|
|
|
|
|
|
|
|
let string = {};
|
|
|
|
cstream.readString(-1, string);
|
|
|
|
cstream.close();
|
|
|
|
return string.value;
|
|
|
|
}
|
|
|
|
|
2014-04-10 01:00:08 -07:00
|
|
|
|
2011-03-04 16:36:56 -08:00
|
|
|
/**
|
|
|
|
* Tests asynchronously writing a file using NetUtil.asyncCopy.
|
|
|
|
*
|
|
|
|
* @param aContractId
|
|
|
|
* The contract ID to use for the output stream
|
|
|
|
* @param aDeferOpen
|
|
|
|
* Whether to use DEFER_OPEN in the output stream.
|
|
|
|
*/
|
|
|
|
function async_write_file(aContractId, aDeferOpen)
|
2009-08-05 13:19:01 -07:00
|
|
|
{
|
|
|
|
do_test_pending();
|
|
|
|
|
|
|
|
// First, we need an output file to write to.
|
|
|
|
let file = Cc["@mozilla.org/file/directory_service;1"].
|
|
|
|
getService(Ci.nsIProperties).
|
2009-12-15 23:33:06 -08:00
|
|
|
get("ProfD", Ci.nsIFile);
|
2009-08-05 13:19:01 -07:00
|
|
|
file.append("NetUtil-async-test-file.tmp");
|
|
|
|
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
|
|
|
|
|
|
|
|
// Then, we need an output stream to our output file.
|
2011-03-04 16:36:56 -08:00
|
|
|
let ostream = Cc[aContractId].createInstance(Ci.nsIFileOutputStream);
|
|
|
|
ostream.init(file, -1, -1, aDeferOpen ? Ci.nsIFileOutputStream.DEFER_OPEN : 0);
|
2009-08-05 13:19:01 -07:00
|
|
|
|
|
|
|
// Finally, we need an input stream to take data from.
|
|
|
|
const TEST_DATA = "this is a test string";
|
|
|
|
let istream = Cc["@mozilla.org/io/string-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIStringInputStream);
|
|
|
|
istream.setData(TEST_DATA, TEST_DATA.length);
|
|
|
|
|
|
|
|
NetUtil.asyncCopy(istream, ostream, function(aResult) {
|
|
|
|
// Make sure the copy was successful!
|
|
|
|
do_check_true(Components.isSuccessCode(aResult));
|
|
|
|
|
|
|
|
// Check the file contents.
|
|
|
|
do_check_eq(TEST_DATA, getFileContents(file));
|
|
|
|
|
2009-12-15 23:33:06 -08:00
|
|
|
// Finish the test.
|
2009-08-05 13:19:01 -07:00
|
|
|
do_test_finished();
|
|
|
|
run_next_test();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-03-04 16:36:56 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Tests
|
2009-08-06 13:34:46 -07:00
|
|
|
|
2014-04-10 01:00:08 -07:00
|
|
|
// Test NetUtil.asyncCopy for all possible buffering scenarios
|
|
|
|
function test_async_copy()
|
|
|
|
{
|
|
|
|
// Create a data sample
|
|
|
|
function make_sample(text) {
|
|
|
|
let data = [];
|
|
|
|
for (let i = 0; i <= 100; ++i) {
|
|
|
|
data.push(text);
|
|
|
|
}
|
|
|
|
return data.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an input buffer holding some data
|
|
|
|
function make_input(isBuffered, data) {
|
|
|
|
if (isBuffered) {
|
|
|
|
// String input streams are buffered
|
|
|
|
let istream = Cc["@mozilla.org/io/string-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIStringInputStream);
|
|
|
|
istream.setData(data, data.length);
|
|
|
|
return istream;
|
|
|
|
}
|
|
|
|
|
|
|
|
// File input streams are not buffered, so let's create a file
|
|
|
|
let file = Cc["@mozilla.org/file/directory_service;1"].
|
|
|
|
getService(Ci.nsIProperties).
|
|
|
|
get("ProfD", Ci.nsIFile);
|
|
|
|
file.append("NetUtil-asyncFetch-test-file.tmp");
|
|
|
|
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
|
|
|
|
|
|
|
|
let ostream = Cc["@mozilla.org/network/file-output-stream;1"].
|
|
|
|
createInstance(Ci.nsIFileOutputStream);
|
|
|
|
ostream.init(file, -1, -1, 0);
|
|
|
|
ostream.write(data, data.length);
|
|
|
|
ostream.close();
|
|
|
|
|
|
|
|
let istream = Cc["@mozilla.org/network/file-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIFileInputStream);
|
|
|
|
istream.init(file, -1, 0, 0);
|
|
|
|
|
|
|
|
return istream;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an output buffer holding some data
|
|
|
|
function make_output(isBuffered) {
|
|
|
|
let file = Cc["@mozilla.org/file/directory_service;1"].
|
|
|
|
getService(Ci.nsIProperties).
|
|
|
|
get("ProfD", Ci.nsIFile);
|
|
|
|
file.append("NetUtil-asyncFetch-test-file.tmp");
|
|
|
|
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
|
|
|
|
|
|
|
|
let ostream = Cc["@mozilla.org/network/file-output-stream;1"].
|
|
|
|
createInstance(Ci.nsIFileOutputStream);
|
|
|
|
ostream.init(file, -1, -1, 0);
|
|
|
|
|
|
|
|
if (!isBuffered) {
|
|
|
|
return {file: file, sink: ostream};
|
|
|
|
}
|
|
|
|
|
|
|
|
let bstream = Cc["@mozilla.org/network/buffered-output-stream;1"].
|
|
|
|
createInstance(Ci.nsIBufferedOutputStream);
|
|
|
|
bstream.init(ostream, 256);
|
|
|
|
return {file: file, sink: bstream};
|
|
|
|
}
|
|
|
|
Task.spawn(function*() {
|
|
|
|
do_test_pending();
|
|
|
|
for (let bufferedInput of [true, false]) {
|
|
|
|
for (let bufferedOutput of [true, false]) {
|
|
|
|
let text = "test_async_copy with "
|
|
|
|
+ (bufferedInput?"buffered input":"unbuffered input")
|
|
|
|
+ ", "
|
|
|
|
+ (bufferedOutput?"buffered output":"unbuffered output");
|
|
|
|
do_print(text);
|
|
|
|
let TEST_DATA = "[" + make_sample(text) + "]";
|
|
|
|
let source = make_input(bufferedInput, TEST_DATA);
|
|
|
|
let {file, sink} = make_output(bufferedOutput);
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
NetUtil.asyncCopy(source, sink, deferred.resolve);
|
|
|
|
let result = yield deferred.promise;
|
|
|
|
|
|
|
|
// Make sure the copy was successful!
|
|
|
|
if (!Components.isSuccessCode(result)) {
|
|
|
|
do_throw(new Components.Exception("asyncCopy error", result));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the file contents.
|
|
|
|
do_check_eq(TEST_DATA, getFileContents(file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do_test_finished();
|
|
|
|
run_next_test();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-03-04 16:36:56 -08:00
|
|
|
function test_async_write_file() {
|
|
|
|
async_write_file(OUTPUT_STREAM_CONTRACT_ID);
|
|
|
|
}
|
2009-08-06 13:34:46 -07:00
|
|
|
|
2011-03-04 16:36:56 -08:00
|
|
|
function test_async_write_file_deferred() {
|
|
|
|
async_write_file(OUTPUT_STREAM_CONTRACT_ID, true);
|
|
|
|
}
|
2009-08-06 13:34:46 -07:00
|
|
|
|
2011-03-04 16:36:56 -08:00
|
|
|
function test_async_write_file_safe() {
|
|
|
|
async_write_file(SAFE_OUTPUT_STREAM_CONTRACT_ID);
|
|
|
|
}
|
2009-08-06 13:34:46 -07:00
|
|
|
|
2011-03-04 16:36:56 -08:00
|
|
|
function test_async_write_file_safe_deferred() {
|
|
|
|
async_write_file(SAFE_OUTPUT_STREAM_CONTRACT_ID, true);
|
2009-08-06 13:34:46 -07:00
|
|
|
}
|
|
|
|
|
2009-09-02 13:24:49 -07:00
|
|
|
function test_newURI_no_spec_throws()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
NetUtil.newURI();
|
|
|
|
do_throw("should throw!");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_newURI()
|
|
|
|
{
|
|
|
|
let ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
|
|
|
|
|
2009-12-15 23:33:03 -08:00
|
|
|
// Check that we get the same URI back from the IO service and the utility
|
|
|
|
// method.
|
2009-09-02 13:24:49 -07:00
|
|
|
const TEST_URI = "http://mozilla.org";
|
|
|
|
let iosURI = ios.newURI(TEST_URI, null, null);
|
|
|
|
let NetUtilURI = NetUtil.newURI(TEST_URI);
|
|
|
|
do_check_true(iosURI.equals(NetUtilURI));
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:03 -08:00
|
|
|
function test_newURI_takes_nsIFile()
|
|
|
|
{
|
|
|
|
let ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
|
|
|
|
|
|
|
|
// Create a test file that we can pass into NetUtil.newURI
|
|
|
|
let file = Cc["@mozilla.org/file/directory_service;1"].
|
|
|
|
getService(Ci.nsIProperties).
|
2009-12-15 23:33:06 -08:00
|
|
|
get("ProfD", Ci.nsIFile);
|
2009-12-15 23:33:03 -08:00
|
|
|
file.append("NetUtil-test-file.tmp");
|
|
|
|
|
|
|
|
// Check that we get the same URI back from the IO service and the utility
|
|
|
|
// method.
|
|
|
|
let iosURI = ios.newFileURI(file);
|
|
|
|
let NetUtilURI = NetUtil.newURI(file);
|
|
|
|
do_check_true(iosURI.equals(NetUtilURI));
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
2009-10-02 22:11:20 -07:00
|
|
|
function test_ioService()
|
|
|
|
{
|
|
|
|
do_check_true(NetUtil.ioService instanceof Ci.nsIIOService);
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
2009-10-15 16:16:01 -07:00
|
|
|
function test_asyncFetch_no_channel()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
NetUtil.asyncFetch(null, function() { });
|
|
|
|
do_throw("should throw!");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_asyncFetch_no_callback()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
NetUtil.asyncFetch({ });
|
|
|
|
do_throw("should throw!");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:08 -08:00
|
|
|
function test_asyncFetch_with_nsIChannel()
|
2009-10-15 16:16:01 -07:00
|
|
|
{
|
|
|
|
const TEST_DATA = "this is a test string";
|
|
|
|
|
|
|
|
// Start the http server, and register our handler.
|
2012-08-14 07:06:04 -07:00
|
|
|
let server = new HttpServer();
|
2009-10-15 16:16:01 -07:00
|
|
|
server.registerPathHandler("/test", function(aRequest, aResponse) {
|
|
|
|
aResponse.setStatusLine(aRequest.httpVersion, 200, "OK");
|
|
|
|
aResponse.setHeader("Content-Type", "text/plain", false);
|
|
|
|
aResponse.write(TEST_DATA);
|
|
|
|
});
|
2013-07-19 10:19:28 -07:00
|
|
|
server.start(-1);
|
2009-10-15 16:16:01 -07:00
|
|
|
|
|
|
|
// Create our channel.
|
|
|
|
let channel = NetUtil.ioService.
|
2013-07-19 10:19:28 -07:00
|
|
|
newChannel("http://localhost:" +
|
|
|
|
server.identity.primaryPort + "/test", null, null);
|
2009-10-15 16:16:01 -07:00
|
|
|
|
|
|
|
// Open our channel asynchronously.
|
|
|
|
NetUtil.asyncFetch(channel, function(aInputStream, aResult) {
|
|
|
|
// Check that we had success.
|
|
|
|
do_check_true(Components.isSuccessCode(aResult));
|
|
|
|
|
|
|
|
// Check that we got the right data.
|
|
|
|
do_check_eq(aInputStream.available(), TEST_DATA.length);
|
|
|
|
let is = Cc["@mozilla.org/scriptableinputstream;1"].
|
|
|
|
createInstance(Ci.nsIScriptableInputStream);
|
|
|
|
is.init(aInputStream);
|
|
|
|
let result = is.read(TEST_DATA.length);
|
|
|
|
do_check_eq(TEST_DATA, result);
|
|
|
|
|
|
|
|
server.stop(run_next_test);
|
2009-10-19 12:23:17 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:08 -08:00
|
|
|
function test_asyncFetch_with_nsIURI()
|
|
|
|
{
|
|
|
|
const TEST_DATA = "this is a test string";
|
|
|
|
|
|
|
|
// Start the http server, and register our handler.
|
2012-08-14 07:06:04 -07:00
|
|
|
let server = new HttpServer();
|
2009-12-15 23:33:08 -08:00
|
|
|
server.registerPathHandler("/test", function(aRequest, aResponse) {
|
|
|
|
aResponse.setStatusLine(aRequest.httpVersion, 200, "OK");
|
|
|
|
aResponse.setHeader("Content-Type", "text/plain", false);
|
|
|
|
aResponse.write(TEST_DATA);
|
|
|
|
});
|
2013-07-19 10:19:28 -07:00
|
|
|
server.start(-1);
|
2009-12-15 23:33:08 -08:00
|
|
|
|
|
|
|
// Create our URI.
|
2013-07-19 10:19:28 -07:00
|
|
|
let uri = NetUtil.newURI("http://localhost:" +
|
|
|
|
server.identity.primaryPort + "/test");
|
2009-12-15 23:33:08 -08:00
|
|
|
|
|
|
|
// Open our URI asynchronously.
|
|
|
|
NetUtil.asyncFetch(uri, function(aInputStream, aResult) {
|
|
|
|
// Check that we had success.
|
|
|
|
do_check_true(Components.isSuccessCode(aResult));
|
|
|
|
|
|
|
|
// Check that we got the right data.
|
|
|
|
do_check_eq(aInputStream.available(), TEST_DATA.length);
|
|
|
|
let is = Cc["@mozilla.org/scriptableinputstream;1"].
|
|
|
|
createInstance(Ci.nsIScriptableInputStream);
|
|
|
|
is.init(aInputStream);
|
|
|
|
let result = is.read(TEST_DATA.length);
|
|
|
|
do_check_eq(TEST_DATA, result);
|
|
|
|
|
|
|
|
server.stop(run_next_test);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_asyncFetch_with_string()
|
|
|
|
{
|
|
|
|
const TEST_DATA = "this is a test string";
|
|
|
|
|
|
|
|
// Start the http server, and register our handler.
|
2012-08-14 07:06:04 -07:00
|
|
|
let server = new HttpServer();
|
2009-12-15 23:33:08 -08:00
|
|
|
server.registerPathHandler("/test", function(aRequest, aResponse) {
|
|
|
|
aResponse.setStatusLine(aRequest.httpVersion, 200, "OK");
|
|
|
|
aResponse.setHeader("Content-Type", "text/plain", false);
|
|
|
|
aResponse.write(TEST_DATA);
|
|
|
|
});
|
2013-07-19 10:19:28 -07:00
|
|
|
server.start(-1);
|
2009-12-15 23:33:08 -08:00
|
|
|
|
|
|
|
// Open our location asynchronously.
|
2013-07-19 10:19:28 -07:00
|
|
|
NetUtil.asyncFetch("http://localhost:" +
|
|
|
|
server.identity.primaryPort + "/test",
|
|
|
|
function(aInputStream, aResult) {
|
2009-12-15 23:33:08 -08:00
|
|
|
// Check that we had success.
|
|
|
|
do_check_true(Components.isSuccessCode(aResult));
|
|
|
|
|
|
|
|
// Check that we got the right data.
|
|
|
|
do_check_eq(aInputStream.available(), TEST_DATA.length);
|
|
|
|
let is = Cc["@mozilla.org/scriptableinputstream;1"].
|
|
|
|
createInstance(Ci.nsIScriptableInputStream);
|
|
|
|
is.init(aInputStream);
|
|
|
|
let result = is.read(TEST_DATA.length);
|
|
|
|
do_check_eq(TEST_DATA, result);
|
|
|
|
|
|
|
|
server.stop(run_next_test);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_asyncFetch_with_nsIFile()
|
|
|
|
{
|
|
|
|
const TEST_DATA = "this is a test string";
|
|
|
|
|
|
|
|
// First we need a file to read from.
|
|
|
|
let file = Cc["@mozilla.org/file/directory_service;1"].
|
|
|
|
getService(Ci.nsIProperties).
|
|
|
|
get("ProfD", Ci.nsIFile);
|
|
|
|
file.append("NetUtil-asyncFetch-test-file.tmp");
|
|
|
|
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
|
|
|
|
|
|
|
|
// Write the test data to the file.
|
|
|
|
let ostream = Cc["@mozilla.org/network/file-output-stream;1"].
|
|
|
|
createInstance(Ci.nsIFileOutputStream);
|
|
|
|
ostream.init(file, -1, -1, 0);
|
|
|
|
ostream.write(TEST_DATA, TEST_DATA.length);
|
|
|
|
|
|
|
|
// Sanity check to make sure the data was written.
|
|
|
|
do_check_eq(TEST_DATA, getFileContents(file));
|
|
|
|
|
|
|
|
// Open our file asynchronously.
|
|
|
|
NetUtil.asyncFetch(file, function(aInputStream, aResult) {
|
|
|
|
// Check that we had success.
|
|
|
|
do_check_true(Components.isSuccessCode(aResult));
|
|
|
|
|
|
|
|
// Check that we got the right data.
|
|
|
|
do_check_eq(aInputStream.available(), TEST_DATA.length);
|
|
|
|
let is = Cc["@mozilla.org/scriptableinputstream;1"].
|
|
|
|
createInstance(Ci.nsIScriptableInputStream);
|
|
|
|
is.init(aInputStream);
|
|
|
|
let result = is.read(TEST_DATA.length);
|
|
|
|
do_check_eq(TEST_DATA, result);
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-03-31 10:24:42 -07:00
|
|
|
function test_asyncFetch_with_nsIInputString()
|
|
|
|
{
|
|
|
|
const TEST_DATA = "this is a test string";
|
|
|
|
let istream = Cc["@mozilla.org/io/string-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIStringInputStream);
|
|
|
|
istream.setData(TEST_DATA, TEST_DATA.length);
|
|
|
|
|
|
|
|
// Read the input stream asynchronously.
|
|
|
|
NetUtil.asyncFetch(istream, function(aInputStream, aResult) {
|
|
|
|
// Check that we had success.
|
|
|
|
do_check_true(Components.isSuccessCode(aResult));
|
|
|
|
|
|
|
|
// Check that we got the right data.
|
|
|
|
do_check_eq(aInputStream.available(), TEST_DATA.length);
|
|
|
|
do_check_eq(NetUtil.readInputStreamToString(aInputStream, TEST_DATA.length),
|
|
|
|
TEST_DATA);
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-10-19 12:23:17 -07:00
|
|
|
function test_asyncFetch_does_not_block()
|
|
|
|
{
|
|
|
|
// Create our channel that has no data.
|
|
|
|
let channel = NetUtil.ioService.
|
|
|
|
newChannel("data:text/plain,", null, null);
|
|
|
|
|
|
|
|
// Open our channel asynchronously.
|
|
|
|
NetUtil.asyncFetch(channel, function(aInputStream, aResult) {
|
|
|
|
// Check that we had success.
|
|
|
|
do_check_true(Components.isSuccessCode(aResult));
|
|
|
|
|
|
|
|
// Check that reading a byte throws that the stream was closed (as opposed
|
|
|
|
// saying it would block).
|
|
|
|
let is = Cc["@mozilla.org/scriptableinputstream;1"].
|
|
|
|
createInstance(Ci.nsIScriptableInputStream);
|
|
|
|
is.init(aInputStream);
|
|
|
|
try {
|
|
|
|
is.read(1);
|
|
|
|
do_throw("should throw!");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
do_check_eq(e.result, Cr.NS_BASE_STREAM_CLOSED);
|
|
|
|
}
|
|
|
|
|
2009-10-18 18:29:31 -07:00
|
|
|
run_next_test();
|
2009-10-15 16:16:01 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:06 -08:00
|
|
|
function test_newChannel_no_specifier()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
NetUtil.newChannel();
|
|
|
|
do_throw("should throw!");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_newChannel_with_string()
|
|
|
|
{
|
|
|
|
const TEST_SPEC = "http://mozilla.org";
|
|
|
|
|
|
|
|
// Check that we get the same URI back from channel the IO service creates and
|
|
|
|
// the channel the utility method creates.
|
|
|
|
let ios = NetUtil.ioService;
|
|
|
|
let iosChannel = ios.newChannel(TEST_SPEC, null, null);
|
|
|
|
let NetUtilChannel = NetUtil.newChannel(TEST_SPEC);
|
|
|
|
do_check_true(iosChannel.URI.equals(NetUtilChannel.URI));
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_newChannel_with_nsIURI()
|
|
|
|
{
|
|
|
|
const TEST_SPEC = "http://mozilla.org";
|
|
|
|
|
|
|
|
// Check that we get the same URI back from channel the IO service creates and
|
|
|
|
// the channel the utility method creates.
|
|
|
|
let uri = NetUtil.newURI(TEST_SPEC);
|
|
|
|
let iosChannel = NetUtil.ioService.newChannelFromURI(uri);
|
|
|
|
let NetUtilChannel = NetUtil.newChannel(uri);
|
|
|
|
do_check_true(iosChannel.URI.equals(NetUtilChannel.URI));
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_newChannel_with_nsIFile()
|
|
|
|
{
|
|
|
|
let file = Cc["@mozilla.org/file/directory_service;1"].
|
|
|
|
getService(Ci.nsIProperties).
|
|
|
|
get("ProfD", Ci.nsIFile);
|
|
|
|
file.append("NetUtil-test-file.tmp");
|
|
|
|
|
|
|
|
// Check that we get the same URI back from channel the IO service creates and
|
|
|
|
// the channel the utility method creates.
|
|
|
|
let uri = NetUtil.newURI(file);
|
|
|
|
let iosChannel = NetUtil.ioService.newChannelFromURI(uri);
|
|
|
|
let NetUtilChannel = NetUtil.newChannel(uri);
|
|
|
|
do_check_true(iosChannel.URI.equals(NetUtilChannel.URI));
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
2010-08-27 12:42:51 -07:00
|
|
|
function test_readInputStreamToString()
|
|
|
|
{
|
|
|
|
const TEST_DATA = "this is a test string\0 with an embedded null";
|
|
|
|
let istream = Cc["@mozilla.org/io/string-input-stream;1"].
|
|
|
|
createInstance(Ci.nsISupportsCString);
|
|
|
|
istream.data = TEST_DATA;
|
|
|
|
|
|
|
|
do_check_eq(NetUtil.readInputStreamToString(istream, TEST_DATA.length),
|
|
|
|
TEST_DATA);
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_readInputStreamToString_no_input_stream()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
NetUtil.readInputStreamToString("hi", 2);
|
|
|
|
do_throw("should throw!");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_readInputStreamToString_no_bytes_arg()
|
|
|
|
{
|
|
|
|
const TEST_DATA = "this is a test string";
|
|
|
|
let istream = Cc["@mozilla.org/io/string-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIStringInputStream);
|
|
|
|
istream.setData(TEST_DATA, TEST_DATA.length);
|
|
|
|
|
|
|
|
try {
|
|
|
|
NetUtil.readInputStreamToString(istream);
|
|
|
|
do_throw("should throw!");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_readInputStreamToString_blocking_stream()
|
|
|
|
{
|
|
|
|
let pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe);
|
|
|
|
pipe.init(true, true, 0, 0, null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
NetUtil.readInputStreamToString(pipe.inputStream, 10);
|
|
|
|
do_throw("should throw!");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
do_check_eq(e.result, Cr.NS_BASE_STREAM_WOULD_BLOCK);
|
|
|
|
}
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_readInputStreamToString_too_many_bytes()
|
|
|
|
{
|
|
|
|
const TEST_DATA = "this is a test string";
|
|
|
|
let istream = Cc["@mozilla.org/io/string-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIStringInputStream);
|
|
|
|
istream.setData(TEST_DATA, TEST_DATA.length);
|
|
|
|
|
|
|
|
try {
|
|
|
|
NetUtil.readInputStreamToString(istream, TEST_DATA.length + 10);
|
|
|
|
do_throw("should throw!");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
do_check_eq(e.result, Cr.NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
2011-11-24 18:23:41 -08:00
|
|
|
function test_readInputStreamToString_with_charset()
|
|
|
|
{
|
|
|
|
const TEST_DATA = "\uff10\uff11\uff12\uff13";
|
|
|
|
const TEST_DATA_UTF8 = "\xef\xbc\x90\xef\xbc\x91\xef\xbc\x92\xef\xbc\x93";
|
|
|
|
const TEST_DATA_SJIS = "\x82\x4f\x82\x50\x82\x51\x82\x52";
|
|
|
|
|
|
|
|
let istream = Cc["@mozilla.org/io/string-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIStringInputStream);
|
|
|
|
|
|
|
|
istream.setData(TEST_DATA_UTF8, TEST_DATA_UTF8.length);
|
|
|
|
do_check_eq(NetUtil.readInputStreamToString(istream,
|
|
|
|
TEST_DATA_UTF8.length,
|
|
|
|
{ charset: "UTF-8"}),
|
|
|
|
TEST_DATA);
|
|
|
|
|
|
|
|
istream.setData(TEST_DATA_SJIS, TEST_DATA_SJIS.length);
|
|
|
|
do_check_eq(NetUtil.readInputStreamToString(istream,
|
|
|
|
TEST_DATA_SJIS.length,
|
|
|
|
{ charset: "Shift_JIS"}),
|
|
|
|
TEST_DATA);
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_readInputStreamToString_invalid_sequence()
|
|
|
|
{
|
|
|
|
const TEST_DATA = "\ufffd\ufffd\ufffd\ufffd";
|
|
|
|
const TEST_DATA_UTF8 = "\xaa\xaa\xaa\xaa";
|
|
|
|
|
|
|
|
let istream = Cc["@mozilla.org/io/string-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIStringInputStream);
|
|
|
|
|
|
|
|
istream.setData(TEST_DATA_UTF8, TEST_DATA_UTF8.length);
|
|
|
|
try {
|
|
|
|
NetUtil.readInputStreamToString(istream,
|
|
|
|
TEST_DATA_UTF8.length,
|
|
|
|
{ charset: "UTF-8" });
|
|
|
|
do_throw("should throw!");
|
|
|
|
} catch (e) {
|
|
|
|
do_check_eq(e.result, Cr.NS_ERROR_ILLEGAL_INPUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
istream.setData(TEST_DATA_UTF8, TEST_DATA_UTF8.length);
|
|
|
|
do_check_eq(NetUtil.readInputStreamToString(istream,
|
|
|
|
TEST_DATA_UTF8.length, {
|
|
|
|
charset: "UTF-8",
|
|
|
|
replacement: Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER}),
|
|
|
|
TEST_DATA);
|
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-05 13:19:01 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Test Runner
|
|
|
|
|
2011-04-18 13:31:20 -07:00
|
|
|
[
|
2014-04-10 01:00:08 -07:00
|
|
|
test_async_copy,
|
2009-08-05 13:19:01 -07:00
|
|
|
test_async_write_file,
|
2011-03-04 16:36:56 -08:00
|
|
|
test_async_write_file_deferred,
|
|
|
|
test_async_write_file_safe,
|
|
|
|
test_async_write_file_safe_deferred,
|
2009-09-02 13:24:49 -07:00
|
|
|
test_newURI_no_spec_throws,
|
|
|
|
test_newURI,
|
2009-12-15 23:33:03 -08:00
|
|
|
test_newURI_takes_nsIFile,
|
2009-10-02 22:11:20 -07:00
|
|
|
test_ioService,
|
2009-10-15 16:16:01 -07:00
|
|
|
test_asyncFetch_no_channel,
|
|
|
|
test_asyncFetch_no_callback,
|
2009-12-15 23:33:08 -08:00
|
|
|
test_asyncFetch_with_nsIChannel,
|
|
|
|
test_asyncFetch_with_nsIURI,
|
|
|
|
test_asyncFetch_with_string,
|
|
|
|
test_asyncFetch_with_nsIFile,
|
2011-03-31 10:24:42 -07:00
|
|
|
test_asyncFetch_with_nsIInputString,
|
2009-10-19 12:23:17 -07:00
|
|
|
test_asyncFetch_does_not_block,
|
2009-12-15 23:33:06 -08:00
|
|
|
test_newChannel_no_specifier,
|
|
|
|
test_newChannel_with_string,
|
|
|
|
test_newChannel_with_nsIURI,
|
|
|
|
test_newChannel_with_nsIFile,
|
2010-08-27 12:42:51 -07:00
|
|
|
test_readInputStreamToString,
|
|
|
|
test_readInputStreamToString_no_input_stream,
|
|
|
|
test_readInputStreamToString_no_bytes_arg,
|
|
|
|
test_readInputStreamToString_blocking_stream,
|
|
|
|
test_readInputStreamToString_too_many_bytes,
|
2011-11-24 18:23:41 -08:00
|
|
|
test_readInputStreamToString_with_charset,
|
|
|
|
test_readInputStreamToString_invalid_sequence,
|
2011-04-18 13:31:20 -07:00
|
|
|
].forEach(add_test);
|
2009-08-05 13:19:01 -07:00
|
|
|
let index = 0;
|
|
|
|
|
|
|
|
function run_test()
|
|
|
|
{
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|