Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // |
michael@0 | 2 | // POST test |
michael@0 | 3 | // |
michael@0 | 4 | |
michael@0 | 5 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 6 | |
michael@0 | 7 | XPCOMUtils.defineLazyGetter(this, "URL", function() { |
michael@0 | 8 | return "http://localhost:" + httpserver.identity.primaryPort; |
michael@0 | 9 | }); |
michael@0 | 10 | |
michael@0 | 11 | var httpserver = new HttpServer(); |
michael@0 | 12 | var testpath = "/simple"; |
michael@0 | 13 | |
michael@0 | 14 | var testfile = do_get_file("../unit/data/test_readline6.txt"); |
michael@0 | 15 | |
michael@0 | 16 | const BOUNDARY = "AaB03x"; |
michael@0 | 17 | var teststring1 = "--" + BOUNDARY + "\r\n" |
michael@0 | 18 | + "Content-Disposition: form-data; name=\"body\"\r\n\r\n" |
michael@0 | 19 | + "0123456789\r\n" |
michael@0 | 20 | + "--" + BOUNDARY + "\r\n" |
michael@0 | 21 | + "Content-Disposition: form-data; name=\"files\"; filename=\"" + testfile.leafName + "\"\r\n" |
michael@0 | 22 | + "Content-Type: application/octet-stream\r\n" |
michael@0 | 23 | + "Content-Length: " + testfile.fileSize + "\r\n\r\n"; |
michael@0 | 24 | var teststring2 = "--" + BOUNDARY + "--\r\n"; |
michael@0 | 25 | |
michael@0 | 26 | const BUFFERSIZE = 4096; |
michael@0 | 27 | |
michael@0 | 28 | function run_test() { |
michael@0 | 29 | var sstream1 = Cc["@mozilla.org/io/string-input-stream;1"]. |
michael@0 | 30 | createInstance(Ci.nsIStringInputStream); |
michael@0 | 31 | sstream1.data = teststring1; |
michael@0 | 32 | |
michael@0 | 33 | var fstream = Cc["@mozilla.org/network/file-input-stream;1"]. |
michael@0 | 34 | createInstance(Ci.nsIFileInputStream); |
michael@0 | 35 | fstream.init(testfile, -1, -1, 0); |
michael@0 | 36 | |
michael@0 | 37 | var buffered = Cc["@mozilla.org/network/buffered-input-stream;1"]. |
michael@0 | 38 | createInstance(Ci.nsIBufferedInputStream); |
michael@0 | 39 | buffered.init(fstream, BUFFERSIZE); |
michael@0 | 40 | |
michael@0 | 41 | var sstream2 = Cc["@mozilla.org/io/string-input-stream;1"]. |
michael@0 | 42 | createInstance(Ci.nsIStringInputStream); |
michael@0 | 43 | sstream2.data = teststring2; |
michael@0 | 44 | |
michael@0 | 45 | var multi = Cc["@mozilla.org/io/multiplex-input-stream;1"]. |
michael@0 | 46 | createInstance(Ci.nsIMultiplexInputStream); |
michael@0 | 47 | multi.appendStream(sstream1); |
michael@0 | 48 | multi.appendStream(buffered); |
michael@0 | 49 | multi.appendStream(sstream2); |
michael@0 | 50 | |
michael@0 | 51 | var mime = Cc["@mozilla.org/network/mime-input-stream;1"]. |
michael@0 | 52 | createInstance(Ci.nsIMIMEInputStream); |
michael@0 | 53 | mime.addHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY); |
michael@0 | 54 | mime.setData(multi); |
michael@0 | 55 | mime.addContentLength = true; |
michael@0 | 56 | |
michael@0 | 57 | httpserver.registerPathHandler(testpath, serverHandler); |
michael@0 | 58 | httpserver.start(-1); |
michael@0 | 59 | |
michael@0 | 60 | var channel = setupChannel(testpath); |
michael@0 | 61 | |
michael@0 | 62 | channel.QueryInterface(Ci.nsIUploadChannel) |
michael@0 | 63 | .setUploadStream(mime, "", mime.available()); |
michael@0 | 64 | channel.requestMethod = "POST"; |
michael@0 | 65 | |
michael@0 | 66 | channel.asyncOpen(new ChannelListener(checkRequest, channel), null); |
michael@0 | 67 | |
michael@0 | 68 | do_test_pending(); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | function setupChannel(path) { |
michael@0 | 72 | var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); |
michael@0 | 73 | return chan = ios.newChannel(URL + path, "", null) |
michael@0 | 74 | .QueryInterface(Ci.nsIHttpChannel); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | function serverHandler(metadata, response) { |
michael@0 | 78 | do_check_eq(metadata.method, "POST"); |
michael@0 | 79 | |
michael@0 | 80 | var data = read_stream(metadata.bodyInputStream, |
michael@0 | 81 | metadata.bodyInputStream.available()); |
michael@0 | 82 | |
michael@0 | 83 | var testfile_stream = Cc["@mozilla.org/network/file-input-stream;1"]. |
michael@0 | 84 | createInstance(Ci.nsIFileInputStream); |
michael@0 | 85 | testfile_stream.init(testfile, -1, -1, 0); |
michael@0 | 86 | |
michael@0 | 87 | do_check_eq(teststring1 + |
michael@0 | 88 | read_stream(testfile_stream, testfile_stream.available()) + |
michael@0 | 89 | teststring2, |
michael@0 | 90 | data); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | function checkRequest(request, data, context) { |
michael@0 | 94 | httpserver.stop(do_test_finished); |
michael@0 | 95 | } |