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 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 2 | |
michael@0 | 3 | var httpserver = null; |
michael@0 | 4 | |
michael@0 | 5 | XPCOMUtils.defineLazyGetter(this, "uri", function() { |
michael@0 | 6 | return "http://localhost:" + httpserver.identity.primaryPort + "/multipart"; |
michael@0 | 7 | }); |
michael@0 | 8 | |
michael@0 | 9 | function make_channel(url) { |
michael@0 | 10 | var ios = Cc["@mozilla.org/network/io-service;1"]. |
michael@0 | 11 | getService(Ci.nsIIOService); |
michael@0 | 12 | return ios.newChannel(url, "", null); |
michael@0 | 13 | } |
michael@0 | 14 | |
michael@0 | 15 | var multipartBody = "\r\nSome text\r\n--boundary\r\n\r\n<?xml version='1.0'?><root/>\r\n--boundary--"; |
michael@0 | 16 | |
michael@0 | 17 | function make_channel(url) { |
michael@0 | 18 | var ios = Cc["@mozilla.org/network/io-service;1"]. |
michael@0 | 19 | getService(Ci.nsIIOService); |
michael@0 | 20 | return ios.newChannel(url, "", null); |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | function contentHandler(metadata, response) |
michael@0 | 24 | { |
michael@0 | 25 | response.setHeader("Content-Type", 'multipart/mixed; boundary="boundary"'); |
michael@0 | 26 | response.bodyOutputStream.write(multipartBody, multipartBody.length); |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | var numTests = 2; |
michael@0 | 30 | var testNum = 0; |
michael@0 | 31 | |
michael@0 | 32 | var testData = |
michael@0 | 33 | [ |
michael@0 | 34 | { data: "Some text", type: "text/plain" }, |
michael@0 | 35 | { data: "<?xml version='1.0'?><root/>", type: "text/xml" } |
michael@0 | 36 | ]; |
michael@0 | 37 | |
michael@0 | 38 | function responseHandler(request, buffer) |
michael@0 | 39 | { |
michael@0 | 40 | do_check_eq(buffer, testData[testNum].data); |
michael@0 | 41 | do_check_eq(request.QueryInterface(Ci.nsIChannel).contentType, |
michael@0 | 42 | testData[testNum].type); |
michael@0 | 43 | if (++testNum == numTests) |
michael@0 | 44 | httpserver.stop(do_test_finished); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | var multipartListener = { |
michael@0 | 48 | _buffer: "", |
michael@0 | 49 | |
michael@0 | 50 | QueryInterface: function(iid) { |
michael@0 | 51 | if (iid.equals(Components.interfaces.nsIStreamListener) || |
michael@0 | 52 | iid.equals(Components.interfaces.nsIRequestObserver) || |
michael@0 | 53 | iid.equals(Components.interfaces.nsISupports)) |
michael@0 | 54 | return this; |
michael@0 | 55 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 56 | }, |
michael@0 | 57 | |
michael@0 | 58 | onStartRequest: function(request, context) { |
michael@0 | 59 | this._buffer = ""; |
michael@0 | 60 | }, |
michael@0 | 61 | |
michael@0 | 62 | onDataAvailable: function(request, context, stream, offset, count) { |
michael@0 | 63 | try { |
michael@0 | 64 | this._buffer = this._buffer.concat(read_stream(stream, count)); |
michael@0 | 65 | dump("BUFFEEE: " + this._buffer + "\n\n"); |
michael@0 | 66 | } catch (ex) { |
michael@0 | 67 | do_throw("Error in onDataAvailable: " + ex); |
michael@0 | 68 | } |
michael@0 | 69 | }, |
michael@0 | 70 | |
michael@0 | 71 | onStopRequest: function(request, context, status) { |
michael@0 | 72 | try { |
michael@0 | 73 | responseHandler(request, this._buffer); |
michael@0 | 74 | } catch (ex) { |
michael@0 | 75 | do_throw("Error in closure function: " + ex); |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | }; |
michael@0 | 79 | |
michael@0 | 80 | function run_test() |
michael@0 | 81 | { |
michael@0 | 82 | httpserver = new HttpServer(); |
michael@0 | 83 | httpserver.registerPathHandler("/multipart", contentHandler); |
michael@0 | 84 | httpserver.start(-1); |
michael@0 | 85 | |
michael@0 | 86 | var streamConv = Cc["@mozilla.org/streamConverters;1"] |
michael@0 | 87 | .getService(Ci.nsIStreamConverterService); |
michael@0 | 88 | var conv = streamConv.asyncConvertData("multipart/mixed", |
michael@0 | 89 | "*/*", |
michael@0 | 90 | multipartListener, |
michael@0 | 91 | null); |
michael@0 | 92 | |
michael@0 | 93 | var chan = make_channel(uri); |
michael@0 | 94 | chan.asyncOpen(conv, null); |
michael@0 | 95 | do_test_pending(); |
michael@0 | 96 | } |