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