Skip to content

Commit

Permalink
allow recording and playback of 'error' ajax calls
Browse files Browse the repository at this point in the history
  • Loading branch information
edgifyjp committed Dec 17, 2014
1 parent 2b1df26 commit f8e86f6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = function(options) {
method: request.method,
url: request.url,
statusCode: request.statusCode,
statusText: request.statusText,
headers: request.headers
};
addFixture(options, JSON.stringify(request.body));
Expand Down Expand Up @@ -75,6 +76,7 @@ module.exports = function(options) {
method: res.req.method.toLowerCase(),
url: res.req.url,
statusCode: res.statusCode,
statusText: res.statusText,
headers: {}
};

Expand All @@ -98,6 +100,7 @@ module.exports = function(options) {
if (moduleName && testName) {
var fixture = {
statusCode: options.statusCode,
statusText: options.statusText,
headers: options.headers,
body: body
};
Expand Down
8 changes: 8 additions & 0 deletions lib/qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ ProxyFixtures.prototype.testStart = function(details) {

Ember.$(document).on('ajaxSuccess',
Ember.run.bind(instance, instance.cacheRequest));
Ember.$(document).on('ajaxError',
Ember.run.bind(instance, instance.cacheRequest));

var proxyFixtures = window[instance.name];

Expand Down Expand Up @@ -96,6 +98,8 @@ ProxyFixtures.prototype.testStart = function(details) {
fixture.headers['x-mockjax-response'] = 'true';

return {
status: fixture.statusCode,
statusText: fixture.statusText,
responseTime: 0,
method: settingsMethod,
headers: fixture.headers,
Expand All @@ -119,6 +123,7 @@ ProxyFixtures.prototype.testDone = function() {
});

Ember.$(document).off('ajaxSuccess', Ember.run.bind(this, this.cacheRequest));
Ember.$(document).off('ajaxError', Ember.run.bind(this, this.cacheRequest));
Ember.$.mockjax.clear();
};

Expand Down Expand Up @@ -146,6 +151,9 @@ ProxyFixtures.prototype.cacheRequest = function(e, xhr, settings) {
headers: headers,
body: JSON.parse(xhr.responseText.length > 1 ? xhr.responseText : '{}')
};
if (xhr.statusText) {
cachedRequest.statusText = xhr.statusText;
}

this.cachedRequests.push(cachedRequest);
};
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/middleware-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ describe('Middleware', function() {
statusCode: 200,
headers: {},
body: { users: [] }
},
{
reqHeaders: { 'x-module-name': 'test', 'x-test-name': 'test-get' },
method: 'GET',
url: '/private-users',
statusCode: 401,
statusText: 'Unauthorized',
headers: {},
body: { users: [] }
}
]);

Expand Down Expand Up @@ -103,6 +112,17 @@ describe('Middleware', function() {
assert.deepEqual(fixture.headers, {});

});

it ('records an errored response', function () {
var fixture = this.fixture['/private-users']['get'][''].fixtures[0];

assert.deepEqual(Object.keys(fixture),
['statusCode', 'statusText', 'headers', 'body']);
assert.equal(fixture.statusCode, 401);
assert.equal(fixture.statusText, "Unauthorized");
assert.equal(fixture.body, '{"users":[]}');
assert.deepEqual(fixture.headers, {});
});
});

describe('POST', function() {
Expand Down
48 changes: 47 additions & 1 deletion tests/unit/qunit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ describe('ProxyFixtures', function() {
});

assert.deepEqual(res, {
status: 200,
statusText: undefined,
responseTime: 0,
method: 'GET',
headers: {
Expand All @@ -244,6 +246,8 @@ describe('ProxyFixtures', function() {
});

assert.deepEqual(res, {
status: 200,
statusText: undefined,
responseTime: 0,
method: 'GET',
headers: {
Expand Down Expand Up @@ -275,6 +279,8 @@ describe('ProxyFixtures', function() {
});

assert.deepEqual(res, {
status: 200,
statusText: undefined,
responseTime: 0,
method: 'GET',
headers: {
Expand Down Expand Up @@ -420,6 +426,46 @@ describe('ProxyFixtures', function() {
}
}
});
});

it('adds erroring request to cachedRequests', function() {
assert.equal(proxyFixtures.cachedRequests.length, 0);

proxyFixtures.cacheRequest(null, {
getAllResponseHeaders: function() {
return 'x-random-header:false';
},
responseText: '{"user":{"name":"Jake"}}',
status: 401,
statusText: 'Unauthorized'
}, {
url: 'http://localhost:4000/api/v1/private-users/1',
type: 'GET',
headers: {
'x-module-name': 'test',
'x-test-name': 'test'
}
});

assert.equal(proxyFixtures.cachedRequests.length, 1);
assert.deepEqual(proxyFixtures.cachedRequests[0], {
url: 'http://localhost:4000/api/v1/private-users/1',
statusCode: 401,
statusText: 'Unauthorized',
method: 'GET',
reqHeaders: {
'x-module-name': 'test',
'x-test-name': 'test'
},
headers: {
'x-random-header': 'false'
},
body: {
user: {
name: 'Jake'
}
}
});
});
});

Expand All @@ -445,7 +491,7 @@ describe('ProxyFixtures', function() {

assert.equal(parsed.path, url);
});
})
});

describe('query', function() {
it('is present', function() {
Expand Down

0 comments on commit f8e86f6

Please sign in to comment.