mirror of
https://github.com/zhigang1992/grunt-interfake.git
synced 2026-01-12 22:47:42 +08:00
real initial commit
This commit is contained in:
59
Gruntfile.js
59
Gruntfile.js
@@ -28,22 +28,59 @@ module.exports = function(grunt) {
|
||||
tests: ['tmp'],
|
||||
},
|
||||
|
||||
concurrent: {
|
||||
testWithInterfake: [
|
||||
'interfake:fixture1',
|
||||
'connect:server:keepalive'
|
||||
]
|
||||
},
|
||||
|
||||
// Configuration to be run (and then tested).
|
||||
interfake: {
|
||||
default_options: {
|
||||
fixture1: {
|
||||
options: {
|
||||
port: 9000,
|
||||
endpoints: [{
|
||||
"request": {
|
||||
"url": "/whattimeisit",
|
||||
"method": "get"
|
||||
},
|
||||
files: {
|
||||
'tmp/default_options': ['test/fixtures/testing', 'test/fixtures/123'],
|
||||
"response": {
|
||||
"code": 200,
|
||||
"body": {
|
||||
"theTime": "Adventure Time!",
|
||||
"starring": [
|
||||
"Finn",
|
||||
"Jake"
|
||||
],
|
||||
"location": "ooo"
|
||||
}
|
||||
}
|
||||
}]
|
||||
},
|
||||
},
|
||||
custom_options: {
|
||||
fixture2: {
|
||||
options: {
|
||||
separator: ': ',
|
||||
punctuation: ' !!!',
|
||||
port: 9001,
|
||||
},
|
||||
src: ['tmp/endpoints.json'],
|
||||
},
|
||||
},
|
||||
|
||||
//
|
||||
connect: {
|
||||
server: {
|
||||
options: {
|
||||
port: 8088,
|
||||
hostname: 'localhost',
|
||||
middleware: function(connect, options, middlewares) {
|
||||
// inject a custom middleware into the array of default middlewares
|
||||
middlewares.push(function(req, res, next) {
|
||||
res.end('Hello, world from port #' + options.port + '!');
|
||||
});
|
||||
|
||||
return middlewares;
|
||||
},
|
||||
files: {
|
||||
'tmp/custom_options': ['test/fixtures/testing', 'test/fixtures/123'],
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -62,6 +99,8 @@ module.exports = function(grunt) {
|
||||
grunt.loadNpmTasks('grunt-contrib-jshint');
|
||||
grunt.loadNpmTasks('grunt-contrib-clean');
|
||||
grunt.loadNpmTasks('grunt-contrib-nodeunit');
|
||||
grunt.loadNpmTasks('grunt-contrib-connect');
|
||||
grunt.loadNpmTasks('grunt-concurrent');
|
||||
|
||||
// Whenever the "test" task is run, first clean the "tmp" dir, then run this
|
||||
// plugin's task(s), then test the result.
|
||||
@@ -70,4 +109,8 @@ module.exports = function(grunt) {
|
||||
// By default, lint and run all tests.
|
||||
grunt.registerTask('default', ['jshint', 'test']);
|
||||
|
||||
// By default, lint and run all tests.
|
||||
grunt.registerTask('ggg', ['concurrent:testWithInterfake']);
|
||||
grunt.registerTask('fff', ['connect:server:keepalive']);
|
||||
|
||||
};
|
||||
|
||||
116
README.md
116
README.md
@@ -25,11 +25,33 @@ In your project's Gruntfile, add a section named `interfake` to the data object
|
||||
```js
|
||||
grunt.initConfig({
|
||||
interfake: {
|
||||
fixture1: {
|
||||
options: {
|
||||
// Task-specific options go here.
|
||||
port: 9000, // default: 3000
|
||||
endpoints: [{ // endpoints
|
||||
"request": {
|
||||
"url": "/whattimeisit",
|
||||
"method": "get"
|
||||
},
|
||||
your_target: {
|
||||
// Target-specific file lists and/or options go here.
|
||||
"response": {
|
||||
"code": 200,
|
||||
"body": {
|
||||
"theTime": "Adventure Time!",
|
||||
"starring": [
|
||||
"Finn",
|
||||
"Jake"
|
||||
],
|
||||
"location": "ooo"
|
||||
}
|
||||
}
|
||||
}]
|
||||
},
|
||||
},
|
||||
fixture2: {
|
||||
options: {
|
||||
port: 9000, // default: 3000
|
||||
},
|
||||
src: ['endpoints.json'] // has to be a JSON file
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -37,49 +59,79 @@ grunt.initConfig({
|
||||
|
||||
### Options
|
||||
|
||||
#### options.separator
|
||||
Type: `String`
|
||||
Default value: `', '`
|
||||
#### options.port
|
||||
Type: `Integer`
|
||||
Default value: `9000`
|
||||
|
||||
A string value that is used to do something with whatever.
|
||||
The port that the interfake server should be started at.
|
||||
|
||||
#### options.punctuation
|
||||
Type: `String`
|
||||
Default value: `'.'`
|
||||
#### options.endpoints
|
||||
Type: `Object`
|
||||
Default value: see [example endpoints](https://github.com/basicallydan/interfake#example-more-examples-1)
|
||||
|
||||
A string value that is used to do something else with whatever else.
|
||||
|
||||
### Usage Examples
|
||||
|
||||
#### Default Options
|
||||
In this example, the default options are used to do something with whatever. So if the `testing` file has the content `Testing` and the `123` file had the content `1 2 3`, the generated result would be `Testing, 1 2 3.`
|
||||
### Usage example as fake backend server
|
||||
In this example, interfake will be used as a backend server in a test setup. A proxy will forward all requests matching a regex pattern to the interfake server.
|
||||
|
||||
```js
|
||||
grunt.initConfig({
|
||||
interfake: {
|
||||
options: {},
|
||||
files: {
|
||||
'dest/default_options': ['src/testing', 'src/123'],
|
||||
},
|
||||
},
|
||||
});
|
||||
```
|
||||
var httpProxy = require('http-proxy');
|
||||
|
||||
#### Custom Options
|
||||
In this example, custom options are used to do something else with whatever else. So if the `testing` file has the content `Testing` and the `123` file had the content `1 2 3`, the generated result in this case would be `Testing: 1 2 3 !!!`
|
||||
var proxy = new httpProxy.RoutingProxy();
|
||||
|
||||
// forward requests to fake backend
|
||||
var proxyFunction = function (req, res, next) {
|
||||
var match = req.url.match(/.*\/some-part-of-my-url\/.*/);
|
||||
if (match) {
|
||||
proxy.proxyRequest(req, res, {
|
||||
host: 'localhost',
|
||||
port: 9000
|
||||
});
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
};
|
||||
|
||||
```js
|
||||
grunt.initConfig({
|
||||
|
||||
// fake backend
|
||||
interfake: {
|
||||
options: {
|
||||
separator: ': ',
|
||||
punctuation: ' !!!',
|
||||
port: 9000
|
||||
},
|
||||
files: {
|
||||
'dest/default_options': ['src/testing', 'src/123'],
|
||||
src: ['endpoints.json'],
|
||||
},
|
||||
|
||||
// web server for test environment
|
||||
connect: {
|
||||
options: {
|
||||
port: 8080,
|
||||
hostname: 'localhost'
|
||||
},
|
||||
test: {
|
||||
options: {
|
||||
middleware: function (connect) {
|
||||
return [
|
||||
proxyFunction,
|
||||
mountFolder(connect, 'static')
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// starts the interfake server and a web server
|
||||
concurrent: {
|
||||
testWithInterfake: [
|
||||
'interfake',
|
||||
'connect:test:keepalive'
|
||||
]
|
||||
},
|
||||
});
|
||||
|
||||
grunt.loadNpmTasks('grunt-interfake');
|
||||
grunt.loadNpmTasks('grunt-contrib-connect');
|
||||
grunt.loadNpmTasks('grunt-concurrent');
|
||||
|
||||
grunt.registerTask('integrate', ['concurrent:testWithInterfake']);
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
@@ -30,6 +30,8 @@
|
||||
"devDependencies": {
|
||||
"grunt-contrib-jshint": "~0.6.0",
|
||||
"grunt-contrib-clean": "~0.4.0",
|
||||
"grunt-contrib-connect": "~0.7.1",
|
||||
"grunt-concurrent": "~0.5.0",
|
||||
"grunt-contrib-nodeunit": "~0.2.0",
|
||||
"grunt": "~0.4.4"
|
||||
},
|
||||
@@ -38,5 +40,8 @@
|
||||
},
|
||||
"keywords": [
|
||||
"gruntplugin"
|
||||
]
|
||||
],
|
||||
"dependencies": {
|
||||
"interfake": "~1.2.0"
|
||||
}
|
||||
}
|
||||
@@ -14,16 +14,19 @@ module.exports = function(grunt) {
|
||||
// creation: http://gruntjs.com/creating-tasks
|
||||
|
||||
grunt.registerMultiTask('interfake', 'Starting an interfake server with grunt.', function() {
|
||||
|
||||
// Merge task-specific and/or target-specific options with these defaults.
|
||||
var options = this.options({
|
||||
punctuation: '.',
|
||||
separator: ', '
|
||||
port: 3000
|
||||
});
|
||||
|
||||
var Interfake = require('interfake');
|
||||
var interfake = new Interfake();
|
||||
|
||||
// Iterate over all specified file groups.
|
||||
this.files.forEach(function(f) {
|
||||
// Concat specified files.
|
||||
var src = f.src.filter(function(filepath) {
|
||||
|
||||
f.src.filter(function(filepath) {
|
||||
// Warn on and remove invalid source files (if nonull was set).
|
||||
if (!grunt.file.exists(filepath)) {
|
||||
grunt.log.warn('Source file "' + filepath + '" not found.');
|
||||
@@ -32,19 +35,19 @@ module.exports = function(grunt) {
|
||||
return true;
|
||||
}
|
||||
}).map(function(filepath) {
|
||||
// Read file source.
|
||||
return grunt.file.read(filepath);
|
||||
}).join(grunt.util.normalizelf(options.separator));
|
||||
|
||||
// Handle options.
|
||||
src += options.punctuation;
|
||||
|
||||
// Write the destination file.
|
||||
grunt.file.write(f.dest, src);
|
||||
|
||||
// Print a success message.
|
||||
grunt.log.writeln('File "' + f.dest + '" created.');
|
||||
interfake.loadFile(filepath);
|
||||
});
|
||||
});
|
||||
|
||||
if(options.endpoints)
|
||||
for(var i = 0, len = options.endpoints.length; i < len; i++)
|
||||
interfake.createRoute(options.endpoints[i]);
|
||||
|
||||
grunt.log.ok('Starting interfake server at port ' + options.port);
|
||||
|
||||
interfake.listen(options.port);
|
||||
|
||||
var done = this.async();
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
Testing: 1 2 3 !!!
|
||||
@@ -1 +0,0 @@
|
||||
Testing, 1 2 3.
|
||||
1
test/fixtures/123
vendored
1
test/fixtures/123
vendored
@@ -1 +0,0 @@
|
||||
1 2 3
|
||||
1
test/fixtures/testing
vendored
1
test/fixtures/testing
vendored
@@ -1 +0,0 @@
|
||||
Testing
|
||||
@@ -30,18 +30,7 @@ exports.interfake = {
|
||||
default_options: function(test) {
|
||||
test.expect(1);
|
||||
|
||||
var actual = grunt.file.read('tmp/default_options');
|
||||
var expected = grunt.file.read('test/expected/default_options');
|
||||
test.equal(actual, expected, 'should describe what the default behavior is.');
|
||||
|
||||
test.done();
|
||||
},
|
||||
custom_options: function(test) {
|
||||
test.expect(1);
|
||||
|
||||
var actual = grunt.file.read('tmp/custom_options');
|
||||
var expected = grunt.file.read('test/expected/custom_options');
|
||||
test.equal(actual, expected, 'should describe what the custom option(s) behavior is.');
|
||||
test.equal(1, 2, 'should pass.');
|
||||
|
||||
test.done();
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user