Skip to content

Commit 402d7ff

Browse files
author
Carlos Atencio
committed
Simple function plugins for match
1 parent 7b3ffa0 commit 402d7ff

File tree

4 files changed

+289
-275
lines changed

4 files changed

+289
-275
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = function(grunt) {
5050
test: {
5151
src: 'tests/bundle/httpMock.js',
5252
options: {
53-
specs: 'tests/*test.js',
53+
specs: 'tests/plugin.test.js',
5454
vendor: [
5555
'http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.js'
5656
],

lib/httpMock.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var queryString = require('query-string');
55
function mockTemplate() {
66
var queryStringParse = '<place_query_string_parse_here>';
77
var expectations = '<place_content_here>';
8+
var plugins = '<place_plugins_here>';
89

910
var newModule = angular.module('httpMock', []);
1011

@@ -153,13 +154,26 @@ function mockTemplate() {
153154
return expectationRequest.method.toLowerCase() === configMethod;
154155
}
155156

157+
function matchByPlugins(expectationRequest, config){
158+
var match = true;
159+
160+
if(plugins.length > 0){
161+
match = plugins.reduce(function(value, plugin){
162+
return plugin(expectationRequest, config) && value;
163+
}, true);
164+
}
165+
166+
return match;
167+
}
168+
156169
function match(config, expectationRequest){
157170
return matchMethod(expectationRequest, config) &&
158171
endsWith(config.url, expectationRequest.path) &&
159172
matchParams(expectationRequest, config) &&
160173
matchData(expectationRequest, config) &&
161174
matchQueryString(expectationRequest, config) &&
162-
matchHeaders(expectationRequest, config);
175+
matchHeaders(expectationRequest, config) &&
176+
matchByPlugins(expectationRequest, config);
163177
}
164178

165179
function matchExpectation(config){
@@ -353,11 +367,30 @@ function getExpectationsString(expectations){
353367
return printExpectations.toString();
354368
}
355369

370+
function getPluginsString(plugins){
371+
if(plugins){
372+
var pluginStrings = [];
373+
374+
plugins.forEach(function(plugin){
375+
pluginStrings.push(plugin.match.toString());
376+
});
377+
378+
return pluginStrings.join();
379+
} else{
380+
return '';
381+
}
382+
}
383+
356384
module.exports = function(expectations, plugins){
357385
var templateString = mockTemplate.toString();
358386
var template = templateString.substring(templateString.indexOf('{') + 1, templateString.lastIndexOf('}'));
359-
var newFunc = template.replace(/'<place_content_here>'/, '[' + getExpectationsString(expectations) + ']');
360-
newFunc = newFunc.replace(/'<place_query_string_parse_here>'/, queryString.parse.toString());
387+
var pluginsString = getPluginsString(plugins);
388+
389+
var newFunc =
390+
template
391+
.replace(/'<place_content_here>'/, '[' + getExpectationsString(expectations) + ']')
392+
.replace(/'<place_query_string_parse_here>'/, queryString.parse.toString())
393+
.replace(/'<place_plugins_here>'/, '[' + pluginsString + ']');
361394

362395
/*jslint evil: true */
363396
return new Function(newFunc);

tests/plugin.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
describe('plugins', function(){
2+
var module = window.__module, http;
3+
4+
beforeAll(function(){
5+
http = window.__getHttp();
6+
});
7+
8+
it('plugins can match', function(done){
9+
http({
10+
method: 'GET',
11+
url: '/plugin'
12+
}).success(function(data, status){
13+
expect(data).toBe('plugin match works!');
14+
expect(status).toBe(200);
15+
done();
16+
});
17+
});
18+
});

0 commit comments

Comments
 (0)