summaryrefslogtreecommitdiff
path: root/node_modules/then-request/test/browser.js
blob: cd586c29b7e0127f48cfbfe6ba66f98f6567d182 (plain) (blame)
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
'use strict';

var assert = require('assert');
var getResponse = require('./get-mock-response.js');
var window = (global.window = {});

window.XMLHttpRequest = XMLHttpRequest;
function XMLHttpRequest() {
  assert(arguments.length === 0);
  this._headers = {};
  this._responseHeaders = '';
};
// XMLHttpRequest.prototype.onreadystatechange gets assigned by user
XMLHttpRequest.prototype.open = function (method, url, async) {
  assert(async === true, 'All requests must be async');
  this._method = method;
  this._url = url;
};
XMLHttpRequest.prototype.setRequestHeader = function (name, value) {
  this._headers[name] = value;
};
XMLHttpRequest.prototype.send = function (body) {
  assert(typeof body === 'string' || body === null, 'body must be a string or null');
  this.readyState = 3;
  this.onreadystatechange();
  this.readyState = 4;
  var res = getResponse(this._method, this._url, this._headers, body, {isClient: true});
  this.status = res.statusCode;
  this._responseHeaders = Object.keys(res.headers).map(function (header) {
    return header + ': ' + res.headers[header];
  }).join('\r\n');
  this.responseText = res.body;
  this.onreadystatechange();
};
XMLHttpRequest.prototype.getAllResponseHeaders = function () {
  return this._responseHeaders;
}
window.location = {};
window.location.host = 'http://example.com';


/*
if (xhr.readyState === 4) {
  var headers = {};
  xhr.getAllResponseHeaders().split('\r\n').forEach(function (header) {
    var h = header.split(':');
    if (h.length > 1) {
      headers[h[0].toLowerCase()] = h.slice(1).join(':').trim();
    }
  });
  resolve(new Response(xhr.status, headers, xhr.responseText));
}
*/