Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
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 = "--boundary\r\n"+ |
michael@0 | 16 | "Content-type: text/plain\r\n"+ |
michael@0 | 17 | "Content-range: bytes 0-2/10\r\n"+ |
michael@0 | 18 | "\r\n"+ |
michael@0 | 19 | "aaa\r\n"+ |
michael@0 | 20 | "--boundary\r\n"+ |
michael@0 | 21 | "Content-type: text/plain\r\n"+ |
michael@0 | 22 | "Content-range: bytes 3-7/10\r\n"+ |
michael@0 | 23 | "\r\n"+ |
michael@0 | 24 | "bbbbb"+ |
michael@0 | 25 | "\r\n"+ |
michael@0 | 26 | "--boundary\r\n"+ |
michael@0 | 27 | "Content-type: text/plain\r\n"+ |
michael@0 | 28 | "Content-range: bytes 8-9/10\r\n"+ |
michael@0 | 29 | "\r\n"+ |
michael@0 | 30 | "cc"+ |
michael@0 | 31 | "\r\n"+ |
michael@0 | 32 | "--boundary--"; |
michael@0 | 33 | |
michael@0 | 34 | function make_channel(url) { |
michael@0 | 35 | var ios = Cc["@mozilla.org/network/io-service;1"]. |
michael@0 | 36 | getService(Ci.nsIIOService); |
michael@0 | 37 | return ios.newChannel(url, "", null); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | function contentHandler(metadata, response) |
michael@0 | 41 | { |
michael@0 | 42 | response.setHeader("Content-Type", 'multipart/byteranges; boundary="boundary"'); |
michael@0 | 43 | response.bodyOutputStream.write(multipartBody, multipartBody.length); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | var numTests = 2; |
michael@0 | 47 | var testNum = 0; |
michael@0 | 48 | |
michael@0 | 49 | var testData = |
michael@0 | 50 | [ |
michael@0 | 51 | { data: "aaa", type: "text/plain", isByteRangeRequest: true, startRange: 0, endRange: 2 }, |
michael@0 | 52 | { data: "bbbbb", type: "text/plain", isByteRangeRequest: true, startRange: 3, endRange: 7 }, |
michael@0 | 53 | { data: "cc", type: "text/plain", isByteRangeRequest: true, startRange: 8, endRange: 9 } |
michael@0 | 54 | ]; |
michael@0 | 55 | |
michael@0 | 56 | function responseHandler(request, buffer) |
michael@0 | 57 | { |
michael@0 | 58 | do_check_eq(buffer, testData[testNum].data); |
michael@0 | 59 | do_check_eq(request.QueryInterface(Ci.nsIChannel).contentType, |
michael@0 | 60 | testData[testNum].type); |
michael@0 | 61 | do_check_eq(request.QueryInterface(Ci.nsIByteRangeRequest).isByteRangeRequest, |
michael@0 | 62 | testData[testNum].isByteRangeRequest); |
michael@0 | 63 | do_check_eq(request.QueryInterface(Ci.nsIByteRangeRequest).startRange, |
michael@0 | 64 | testData[testNum].startRange); |
michael@0 | 65 | do_check_eq(request.QueryInterface(Ci.nsIByteRangeRequest).endRange, |
michael@0 | 66 | testData[testNum].endRange); |
michael@0 | 67 | if (++testNum == numTests) |
michael@0 | 68 | httpserver.stop(do_test_finished); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | var multipartListener = { |
michael@0 | 72 | _buffer: "", |
michael@0 | 73 | |
michael@0 | 74 | QueryInterface: function(iid) { |
michael@0 | 75 | if (iid.equals(Components.interfaces.nsIStreamListener) || |
michael@0 | 76 | iid.equals(Components.interfaces.nsIRequestObserver) || |
michael@0 | 77 | iid.equals(Components.interfaces.nsISupports)) |
michael@0 | 78 | return this; |
michael@0 | 79 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 80 | }, |
michael@0 | 81 | |
michael@0 | 82 | onStartRequest: function(request, context) { |
michael@0 | 83 | this._buffer = ""; |
michael@0 | 84 | }, |
michael@0 | 85 | |
michael@0 | 86 | onDataAvailable: function(request, context, stream, offset, count) { |
michael@0 | 87 | try { |
michael@0 | 88 | this._buffer = this._buffer.concat(read_stream(stream, count)); |
michael@0 | 89 | dump("BUFFEEE: " + this._buffer + "\n\n"); |
michael@0 | 90 | } catch (ex) { |
michael@0 | 91 | do_throw("Error in onDataAvailable: " + ex); |
michael@0 | 92 | } |
michael@0 | 93 | }, |
michael@0 | 94 | |
michael@0 | 95 | onStopRequest: function(request, context, status) { |
michael@0 | 96 | try { |
michael@0 | 97 | responseHandler(request, this._buffer); |
michael@0 | 98 | } catch (ex) { |
michael@0 | 99 | do_throw("Error in closure function: " + ex); |
michael@0 | 100 | } |
michael@0 | 101 | } |
michael@0 | 102 | }; |
michael@0 | 103 | |
michael@0 | 104 | function run_test() |
michael@0 | 105 | { |
michael@0 | 106 | httpserver = new HttpServer(); |
michael@0 | 107 | httpserver.registerPathHandler("/multipart", contentHandler); |
michael@0 | 108 | httpserver.start(-1); |
michael@0 | 109 | |
michael@0 | 110 | var streamConv = Cc["@mozilla.org/streamConverters;1"] |
michael@0 | 111 | .getService(Ci.nsIStreamConverterService); |
michael@0 | 112 | var conv = streamConv.asyncConvertData("multipart/byteranges", |
michael@0 | 113 | "*/*", |
michael@0 | 114 | multipartListener, |
michael@0 | 115 | null); |
michael@0 | 116 | |
michael@0 | 117 | var chan = make_channel(uri); |
michael@0 | 118 | chan.asyncOpen(conv, null); |
michael@0 | 119 | do_test_pending(); |
michael@0 | 120 | } |