Skip to content

Commit

Permalink
fix(apimock): correctly detect URL commands
Browse files Browse the repository at this point in the history
Fixes #37
  • Loading branch information
seriema committed Sep 13, 2015
1 parent 4719f42 commit ccf22f2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/index.html
Expand Up @@ -33,7 +33,7 @@ <h2>Debug info</h2>

app.config(function (apiMockProvider) {
apiMockProvider.config({
mockDataPath: 'mock_data',
mockDataPath: '/mock_data',
apiPath: '/api'
});
});
Expand Down
33 changes: 12 additions & 21 deletions app/scripts/angular-apimock.js
Expand Up @@ -171,27 +171,18 @@ angular.module('apiMock', [])
}

function getCommand(mockValue) {
switch (typeof mockValue) {
case 'number':
if (mockValue !== 0 && !isNaN(mockValue)) {
return { type: 'respond', value: mockValue };
}
break;

case 'string':
switch(mockValue.toLowerCase()) {
case 'auto':
return { type: 'recover' };
case 'true':
return { type: 'reroute' };
}
break;

case 'boolean':
if (mockValue === true) {
return { type: 'reroute' };
}
break;
// Depending how we got mockValue it might've been parsed into a type or not.
switch ((mockValue || '').toString().toLowerCase()) {
case '200':
case '404':
case '500':
return { type: 'respond', value: parseInt(mockValue, 10) };

case 'auto':
return { type: 'recover' };

case 'true':
return { type: 'reroute' };
}

return { type: 'ignore' };
Expand Down
30 changes: 30 additions & 0 deletions test/spec/services/angular-apimock.js
Expand Up @@ -151,6 +151,36 @@ describe('Service: apiMock', function () {
});
});

it('should detect HTTP verb command as string', function () {
$location.url('/page?apiMock=200');

// Cannot use $httpBackend.expect() because HTTP status doesn't do a request
$http(defaultRequest)
.success(fail)
.error(function(data, status) {
expect(apiMock._countFallbacks()).toEqual(0);
expect(status).toEqual(200);
});

$rootScope.$digest();
$timeout.flush();
});

it('should detect HTTP verb command as number', function () {
$location.search('apimock', 200);

// Cannot use $httpBackend.expect() because HTTP status doesn't do a request
$http(defaultRequest)
.success(fail)
.error(function(data, status) {
expect(apiMock._countFallbacks()).toEqual(0);
expect(status).toEqual(200);
});

$rootScope.$digest();
$timeout.flush();
});

it('should detect in search queries', function () {
$location.url('/page?apiMock=true');

Expand Down

0 comments on commit ccf22f2

Please sign in to comment.