1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 * Test Chunked-Encoded response parsing.
9 ////////////////////////////////////////////////////////////////////////////////
10 // Test infrastructure
14 const { HttpServer } = ChromeUtils.importESModule(
15 "resource://testing-common/httpd.sys.mjs"
18 ChromeUtils.defineLazyGetter(this, "URL", function () {
19 return "http://localhost:" + httpserver.identity.primaryPort;
22 var httpserver = new HttpServer();
24 var testPathBase = "/chunked_hdrs";
33 function run_test_number(num) {
34 var testPath = testPathBase + num;
35 // eslint-disable-next-line no-eval
36 httpserver.registerPathHandler(testPath, eval("handler" + num));
38 var channel = setupChannel(testPath);
39 var flags = test_flags[num]; // OK if flags undefined for test
41 // eslint-disable-next-line no-eval
42 new ChannelListener(eval("completeTest" + num), channel, flags)
46 function setupChannel(url) {
47 var chan = NetUtil.newChannel({
49 loadUsingSystemPrincipal: true,
51 var httpChan = chan.QueryInterface(Ci.nsIHttpChannel);
56 httpserver.stop(do_test_finished);
59 ////////////////////////////////////////////////////////////////////////////////
60 // Test 1: FAIL because of overflowed chunked size. The parser uses long so
61 // the test case uses >64bit to fail on all platforms.
62 test_flags[1] = CL_EXPECT_LATE_FAILURE | CL_ALLOW_UNKNOWN_CL;
64 // eslint-disable-next-line no-unused-vars
65 function handler1(metadata, response) {
66 var body = "12345678123456789\r\ndata never reached";
68 response.seizePower();
69 response.write("HTTP/1.1 200 OK\r\n");
70 response.write("Content-Type: text/plain\r\n");
71 response.write("Transfer-Encoding: chunked\r\n");
72 response.write("\r\n");
77 // eslint-disable-next-line no-unused-vars
78 function completeTest1(request, data, ctx) {
79 Assert.equal(request.status, Cr.NS_ERROR_UNEXPECTED);
84 ////////////////////////////////////////////////////////////////////////////////
85 // Test 2: FAIL because of non-hex in chunked length
87 test_flags[2] = CL_EXPECT_LATE_FAILURE | CL_ALLOW_UNKNOWN_CL;
89 // eslint-disable-next-line no-unused-vars
90 function handler2(metadata, response) {
91 var body = "junkintheway 123\r\ndata never reached";
93 response.seizePower();
94 response.write("HTTP/1.1 200 OK\r\n");
95 response.write("Content-Type: text/plain\r\n");
96 response.write("Transfer-Encoding: chunked\r\n");
97 response.write("\r\n");
102 // eslint-disable-next-line no-unused-vars
103 function completeTest2(request, data, ctx) {
104 Assert.equal(request.status, Cr.NS_ERROR_UNEXPECTED);
108 ////////////////////////////////////////////////////////////////////////////////
109 // Test 3: OK in spite of non-hex digits after size in the length field
111 test_flags[3] = CL_ALLOW_UNKNOWN_CL;
113 // eslint-disable-next-line no-unused-vars
114 function handler3(metadata, response) {
115 var body = "c junkafter\r\ndata reached\r\n0\r\n\r\n";
117 response.seizePower();
118 response.write("HTTP/1.1 200 OK\r\n");
119 response.write("Content-Type: text/plain\r\n");
120 response.write("Transfer-Encoding: chunked\r\n");
121 response.write("\r\n");
122 response.write(body);
126 // eslint-disable-next-line no-unused-vars
127 function completeTest3(request, data, ctx) {
128 Assert.equal(request.status, 0);
132 ////////////////////////////////////////////////////////////////////////////////
133 // Test 4: Verify a fully compliant chunked response.
135 test_flags[4] = CL_ALLOW_UNKNOWN_CL;
137 // eslint-disable-next-line no-unused-vars
138 function handler4(metadata, response) {
139 var body = "c\r\ndata reached\r\n3\r\nhej\r\n0\r\n\r\n";
141 response.seizePower();
142 response.write("HTTP/1.1 200 OK\r\n");
143 response.write("Content-Type: text/plain\r\n");
144 response.write("Transfer-Encoding: chunked\r\n");
145 response.write("\r\n");
146 response.write(body);
150 // eslint-disable-next-line no-unused-vars
151 function completeTest4(request, data, ctx) {
152 Assert.equal(request.status, 0);
156 ////////////////////////////////////////////////////////////////////////////////
157 // Test 5: A chunk size larger than 32 bit but smaller than 64bit also fails
158 // This is probabaly subject to get improved at some point.
160 test_flags[5] = CL_EXPECT_LATE_FAILURE | CL_ALLOW_UNKNOWN_CL;
162 // eslint-disable-next-line no-unused-vars
163 function handler5(metadata, response) {
164 var body = "123456781\r\ndata never reached";
166 response.seizePower();
167 response.write("HTTP/1.1 200 OK\r\n");
168 response.write("Content-Type: text/plain\r\n");
169 response.write("Transfer-Encoding: chunked\r\n");
170 response.write("\r\n");
171 response.write(body);
175 // eslint-disable-next-line no-unused-vars
176 function completeTest5(request, data, ctx) {
177 Assert.equal(request.status, Cr.NS_ERROR_UNEXPECTED);
179 // run_test_number(6);