summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_reply_without_content_type.js
blob: 439ffb64ac4103f9fa49287e8c160373fa10005f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// tests HTTP replies that lack content-type (where we try to sniff content-type).
//

// Note: sets Cc and Ci variables
"use strict";

const { HttpServer } = ChromeUtils.importESModule(
  "resource://testing-common/httpd.sys.mjs"
);

var httpserver = new HttpServer();
var testpath = "/simple_plainText";
var httpbody = "<html><body>omg hai</body></html>";
var testpathGZip = "/simple_gzip";
//this is compressed httpbody;
var httpbodyGZip = [
  "0x1f",
  "0x8b",
  "0x8",
  "0x0",
  "0x0",
  "0x0",
  "0x0",
  "0x0",
  "0x0",
  "0x3",
  "0xb3",
  "0xc9",
  "0x28",
  "0xc9",
  "0xcd",
  "0xb1",
  "0xb3",
  "0x49",
  "0xca",
  "0x4f",
  "0xa9",
  "0xb4",
  "0xcb",
  "0xcf",
  "0x4d",
  "0x57",
  "0xc8",
  "0x48",
  "0xcc",
  "0xb4",
  "0xd1",
  "0x7",
  "0xf3",
  "0x6c",
  "0xf4",
  "0xc1",
  "0x52",
  "0x0",
  "0x4",
  "0x99",
  "0x79",
  "0x2b",
  "0x21",
  "0x0",
  "0x0",
  "0x0",
];

var dbg = 0;
if (dbg) {
  print("============== START ==========");
}

add_test(function test_plainText() {
  if (dbg) {
    print("============== test_plainText: in");
  }
  httpserver.registerPathHandler(testpath, serverHandler_plainText);
  httpserver.start(-1);
  var channel = setupChannel(testpath);
  // ChannelListener defined in head_channels.js
  channel.asyncOpen(new ChannelListener(checkRequest, channel));
  do_test_pending();
  if (dbg) {
    print("============== test_plainText: out");
  }
});

add_test(function test_GZip() {
  if (dbg) {
    print("============== test_GZip: in");
  }
  httpserver.registerPathHandler(testpathGZip, serverHandler_GZip);
  httpserver.start(-1);
  var channel = setupChannel(testpathGZip);
  // ChannelListener defined in head_channels.js
  channel.asyncOpen(new ChannelListener(checkRequest, channel, CL_EXPECT_GZIP));
  do_test_pending();
  if (dbg) {
    print("============== test_GZip: out");
  }
});

function setupChannel(path) {
  var chan = NetUtil.newChannel({
    uri: "http://localhost:" + httpserver.identity.primaryPort + path,
    loadUsingSystemPrincipal: true,
  });
  chan.QueryInterface(Ci.nsIHttpChannel);
  chan.requestMethod = "GET";
  return chan;
}

function serverHandler_plainText(metadata, response) {
  if (dbg) {
    print("============== serverHandler plainText: in");
  }
  //  no content type set
  //  response.setHeader("Content-Type", "text/plain", false);
  response.bodyOutputStream.write(httpbody, httpbody.length);
  if (dbg) {
    print("============== serverHandler plainText: out");
  }
}

function serverHandler_GZip(metadata, response) {
  if (dbg) {
    print("============== serverHandler GZip: in");
  }
  //  no content type set
  //  response.setHeader("Content-Type", "text/plain", false);
  response.setHeader("Content-Encoding", "gzip", false);
  var bos = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(
    Ci.nsIBinaryOutputStream
  );
  bos.setOutputStream(response.bodyOutputStream);
  bos.writeByteArray(httpbodyGZip);
  if (dbg) {
    print("============== serverHandler GZip: out");
  }
}

function checkRequest(request, data) {
  if (dbg) {
    print("============== checkRequest: in");
  }
  Assert.equal(data, httpbody);
  Assert.equal(request.QueryInterface(Ci.nsIChannel).contentType, "text/html");
  httpserver.stop(do_test_finished);
  run_next_test();
  if (dbg) {
    print("============== checkRequest: out");
  }
}