Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
feat(mock): support for JSON in HttpBackend
Browse files Browse the repository at this point in the history
Closes #236
  • Loading branch information
mhevery committed Jan 22, 2014
1 parent 3d6ed04 commit 9d09a16
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/mock/http_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,23 @@ class MockHttpBackend implements HttpBackend {
return c.future;
}

_createResponse(status, data, headers) {
if (status is Function) return status;
_createResponse(statusOrDataOrFunction, dataOrHeaders, headersOrNone) {
if (statusOrDataOrFunction is Function) return statusOrDataOrFunction;
var status, data, headers;
if (statusOrDataOrFunction is num) {
status = statusOrDataOrFunction;
data = dataOrHeaders;
headers = headersOrNone;
} else {
status = 200;
data = statusOrDataOrFunction;
headers = dataOrHeaders;
}
if (data is Map || data is List) {
data = JSON.encode(data);
}

return ([a,b,c,d,e]) {
return status is num
? [status, data, headers]
: [200, status, data];
};
return ([a,b,c,d,e]) => [status, data, headers];
}


Expand Down
17 changes: 17 additions & 0 deletions test/mock/http_backend_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ main() => describe('MockHttpBackend', () {
});


it('should respond with JSON', inject((Logger logger) {
hb.when('GET', '/url1').respond(200, ['abc'], {});
hb.when('GET', '/url2').respond(200, {'key': 'value'}, {});


callback.andCallFake((status, response) {
expect(status).toBe(200);
logger(response);
});

hb('GET', '/url1', null, callback);
hb('GET', '/url2', null, callback);
hb.flush();
expect(logger).toEqual(['["abc"]', '{"key":"value"}']);
}));


it('should throw error when unexpected request', () {
hb.when('GET', '/url1').respond(200, 'content');
expect(() {
Expand Down

0 comments on commit 9d09a16

Please sign in to comment.