Bug 1857386 [wpt PR 42383] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / test / unit / test_307_redirect.js
blob983884c13454d560196dd9af2acdbcfc2f7bf0ff
1 "use strict";
3 const { HttpServer } = ChromeUtils.importESModule(
4   "resource://testing-common/httpd.sys.mjs"
5 );
7 ChromeUtils.defineLazyGetter(this, "URL", function () {
8   return "http://localhost:" + httpserver.identity.primaryPort;
9 });
11 ChromeUtils.defineLazyGetter(this, "uri", function () {
12   return URL + "/redirect";
13 });
15 ChromeUtils.defineLazyGetter(this, "noRedirectURI", function () {
16   return URL + "/content";
17 });
19 var httpserver = null;
21 function make_channel(url) {
22   return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
25 const requestBody = "request body";
27 function redirectHandler(metadata, response) {
28   response.setStatusLine(metadata.httpVersion, 307, "Moved Temporarily");
29   response.setHeader("Location", noRedirectURI, false);
32 function contentHandler(metadata, response) {
33   response.setHeader("Content-Type", "text/plain");
34   response.bodyOutputStream.writeFrom(
35     metadata.bodyInputStream,
36     metadata.bodyInputStream.available()
37   );
40 function noRedirectStreamObserver(request, buffer) {
41   Assert.equal(buffer, requestBody);
42   var chan = make_channel(uri);
43   var uploadStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
44     Ci.nsIStringInputStream
45   );
46   uploadStream.setData(requestBody, requestBody.length);
47   chan
48     .QueryInterface(Ci.nsIUploadChannel)
49     .setUploadStream(uploadStream, "text/plain", -1);
50   chan.asyncOpen(new ChannelListener(noHeaderStreamObserver, null));
53 function noHeaderStreamObserver(request, buffer) {
54   Assert.equal(buffer, requestBody);
55   var chan = make_channel(uri);
56   var uploadStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
57     Ci.nsIStringInputStream
58   );
59   var streamBody =
60     "Content-Type: text/plain\r\n" +
61     "Content-Length: " +
62     requestBody.length +
63     "\r\n\r\n" +
64     requestBody;
65   uploadStream.setData(streamBody, streamBody.length);
66   chan
67     .QueryInterface(Ci.nsIUploadChannel)
68     .setUploadStream(uploadStream, "", -1);
69   chan.asyncOpen(new ChannelListener(headerStreamObserver, null));
72 function headerStreamObserver(request, buffer) {
73   Assert.equal(buffer, requestBody);
74   httpserver.stop(do_test_finished);
77 function run_test() {
78   httpserver = new HttpServer();
79   httpserver.registerPathHandler("/redirect", redirectHandler);
80   httpserver.registerPathHandler("/content", contentHandler);
81   httpserver.start(-1);
83   Services.prefs.setBoolPref("network.http.prompt-temp-redirect", false);
85   var chan = make_channel(noRedirectURI);
86   var uploadStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
87     Ci.nsIStringInputStream
88   );
89   uploadStream.setData(requestBody, requestBody.length);
90   chan
91     .QueryInterface(Ci.nsIUploadChannel)
92     .setUploadStream(uploadStream, "text/plain", -1);
93   chan.asyncOpen(new ChannelListener(noRedirectStreamObserver, null));
94   do_test_pending();