summaryrefslogtreecommitdiff
path: root/spec/helper.js
blob: 8bae2c62cde7d940cd5fb2849b1c78aa131720d5 (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
var specHelper = (function() {
  // HELPERS

  function setupFakeXHR() {
    this.xhr = sinon.useFakeXMLHttpRequest();
    var requests = this.requests = [];
    this.xhr.onCreate = function (xhr) {
      requests.push(xhr);
    };
    this.expectRequest = expectRequest;
    this.respondJSON = respondJSON;
    this.respondXML = respondXML;
  }

  // TODO: validate http verb
  function expectRequest(url, content, verb) {
    expect(this.requests.length).toBe(1);
    expect(this.requests[0].url).toBe(url);
    expect(decodeURI(this.requests[0].requestBody)).toBe(content);
    if (verb) {
      expect(this.requests[0].method).toBe(verb);
    }
  }

  function respondXML(content) {
    var request = this.requests.pop();
    header = { "Content-Type": "application/xml;charset=utf-8" };
    body = '<?xml version="1.0" encoding="UTF-8"?>\n';
    body += content;
    request.respond(200, header, body);
  }

  function respondJSON(object, responseCode) {
    var request = this.requests.pop();
    header = { "Content-Type": "application/json;charset=utf-8" };
    body = JSON.stringify(object);
    request.respond(responseCode || 200, header, body);
  }

  return {
    setupFakeXHR:  setupFakeXHR,
  }

})();