summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/xpcshell/test_Http.js
blob: 3dfd769b71e61973523d24aae86a5c615764c34b (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

Components.utils.import("resource://gre/modules/Http.jsm");
Components.utils.import("resource://testing-common/httpd.js");

const BinaryInputStream = Components.Constructor("@mozilla.org/binaryinputstream;1",
  "nsIBinaryInputStream", "setInputStream");

var server;

const kDefaultServerPort = 9000;
const kSuccessPath = "/success";
const kBaseUrl = "http://localhost:" + kDefaultServerPort;
const kSuccessUrl = kBaseUrl + kSuccessPath;

const kPostPath = "/post";
const kPostUrl = kBaseUrl + kPostPath;
const kPostDataSent = [["foo", "bar"], ["complex", "!*()@"]];
const kPostDataReceived = "foo=bar&complex=%21%2A%28%29%40";
const kPostMimeTypeReceived = "application/x-www-form-urlencoded; charset=utf-8";

const kJsonPostPath = "/json_post";
const kJsonPostUrl = kBaseUrl + kJsonPostPath;
const kJsonPostData = JSON.stringify(kPostDataSent);
const kJsonPostMimeType = "application/json";

const kPutPath = "/put";
const kPutUrl = kBaseUrl + kPutPath;
const kPutDataSent = [["P", "NP"]];
const kPutDataReceived = "P=NP";

const kGetPath = "/get";
const kGetUrl = kBaseUrl + kGetPath;

function successResult(aRequest, aResponse) {
  aResponse.setStatusLine(null, 200, "OK");
  aResponse.setHeader("Content-Type", "application/json");
  aResponse.write("Success!");
}

function getDataChecker(aExpectedMethod, aExpectedData, aExpectedMimeType = null) {
  return function(aRequest, aResponse) {
    let body = new BinaryInputStream(aRequest.bodyInputStream);
    let bytes = [];
    let avail;
    while ((avail = body.available()) > 0)
      Array.prototype.push.apply(bytes, body.readByteArray(avail));

    do_check_eq(aRequest.method, aExpectedMethod);

    // Checking if the Content-Type is as expected.
    if (aExpectedMimeType) {
      let contentType = aRequest.getHeader("Content-Type");
      do_check_eq(contentType, aExpectedMimeType);
    }

    var data = String.fromCharCode.apply(null, bytes);

    do_check_eq(data, aExpectedData);

    aResponse.setStatusLine(null, 200, "OK");
    aResponse.setHeader("Content-Type", "application/json");
    aResponse.write("Success!");
  }
}

add_test(function test_successCallback() {
  do_test_pending();
  let options = {
    onLoad: function(aResponse) {
      do_check_eq(aResponse, "Success!");
      do_test_finished();
      run_next_test();
    },
    onError: function(e) {
      do_check_true(false);
      do_test_finished();
      run_next_test();
    }
  }
  httpRequest(kSuccessUrl, options);
});

add_test(function test_errorCallback() {
  do_test_pending();
  let options = {
    onSuccess: function(aResponse) {
      do_check_true(false);
      do_test_finished();
      run_next_test();
    },
    onError: function(e, aResponse) {
      do_check_eq(e, "404 - Not Found");
      do_test_finished();
      run_next_test();
    }
  }
  httpRequest(kBaseUrl + "/failure", options);
});

add_test(function test_PostData() {
  do_test_pending();
  let options = {
    onLoad: function(aResponse) {
      do_check_eq(aResponse, "Success!");
      do_test_finished();
      run_next_test();
    },
    onError: function(e) {
      do_check_true(false);
      do_test_finished();
      run_next_test();
    },
    postData: kPostDataSent
  }
  httpRequest(kPostUrl, options);
});

add_test(function test_PutData() {
  do_test_pending();
  let options = {
    method: "PUT",
    onLoad: function(aResponse) {
      do_check_eq(aResponse, "Success!");
      do_test_finished();
      run_next_test();
    },
    onError: function(e) {
      do_check_true(false);
      do_test_finished();
      run_next_test();
    },
    postData: kPutDataSent
  }
  httpRequest(kPutUrl, options);
});

add_test(function test_GetData() {
  do_test_pending();
  let options = {
    onLoad: function(aResponse) {
      do_check_eq(aResponse, "Success!");
      do_test_finished();
      run_next_test();
    },
    onError: function(e) {
      do_check_true(false);
      do_test_finished();
      run_next_test();
    },
    postData: null
  }
  httpRequest(kGetUrl, options);
});

add_test(function test_OptionalParameters() {
  let options = {
    onLoad: null,
    onError: null,
    logger: null
  };
  // Just make sure that nothing throws when doing this (i.e. httpRequest
  // doesn't try to access null options).
  httpRequest(kGetUrl, options);
  run_next_test();
});

/**
 * Makes sure that httpRequest API allows setting a custom Content-Type header
 * for POST requests when data is a string.
 */
add_test(function test_CustomContentTypeOnPost() {
  do_test_pending();

  // Preparing the request parameters.
  let options = {
    onLoad: function(aResponse) {
      do_check_eq(aResponse, "Success!");
      do_test_finished();
      run_next_test();
    },
    onError: function(e) {
      do_check_true(false);
      do_test_finished();
      run_next_test();
    },
    postData: kJsonPostData,
    // Setting a custom Content-Type header.
    headers: [['Content-Type', "application/json"]]
  }

  // Firing the request.
  httpRequest(kJsonPostUrl, options);
});

/**
 * Ensures that the httpRequest API provides a way to override the response
 * MIME type.
 */
add_test(function test_OverrideMimeType() {
  do_test_pending();

  // Preparing the request parameters.
  const kMimeType = 'text/xml; charset=UTF-8';
  let options = {
    onLoad: function(aResponse, xhr) {
      do_check_eq(aResponse, "Success!");

      // Set the expected MIME-type.
      let reportedMimeType = xhr.getResponseHeader("Content-Type");
      do_check_neq(reportedMimeType, kMimeType);

      // responseXML should not be not null if overriding mime type succeeded.
      do_check_true(xhr.responseXML != null);

      do_test_finished();
      run_next_test();
    },
    onError: function(e) {
      do_check_true(false);
      do_test_finished();
      run_next_test();
    }
  };

  // Firing the request.
  let xhr = httpRequest(kGetUrl, options);

  // Override the response MIME type.
  xhr.overrideMimeType(kMimeType);
});

function run_test() {
  // Set up a mock HTTP server to serve a success page.
  server = new HttpServer();
  server.registerPathHandler(kSuccessPath, successResult);
  server.registerPathHandler(kPostPath,
                             getDataChecker("POST", kPostDataReceived,
                                            kPostMimeTypeReceived));
  server.registerPathHandler(kPutPath,
                             getDataChecker("PUT", kPutDataReceived));
  server.registerPathHandler(kGetPath, getDataChecker("GET", ""));
  server.registerPathHandler(kJsonPostPath,
                             getDataChecker("POST", kJsonPostData,
                                            kJsonPostMimeType));

  server.start(kDefaultServerPort);

  run_next_test();

  // Teardown.
  do_register_cleanup(function() {
    server.stop(function() { });
  });
}