summaryrefslogtreecommitdiff
path: root/spec/specHelper.js
blob: 893f8a487be36d9ef540709782aa849f7e5b6485 (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
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) {
    expect(this.requests.length).toBe(1);
    expect(this.requests[0].url).toBe(url);
    expect(decodeURI(this.requests[0].requestBody)).toBe(content);
  }

  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) {
    var request = this.requests.pop();
    header = { "Content-Type": "application/json;charset=utf-8" };
    body = JSON.stringify(object);
    request.respond(200, header, body);
  }

  return {
    setupFakeXHR:  setupFakeXHR,
  }

})();