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