From 15cb3e66da8bce36837bf66bc5166f0b59fb2d25 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Fri, 21 Nov 2014 23:18:26 +0800 Subject: [PATCH] feat(mock): use koa-mock for front end dev demo: ![cnpm-pagemock](https://cloud.githubusercontent.com/assets/156269/5144334/808ddb62-71d4-11e4-9af7-506b17b96d18.gif) Closes #509 --- .node-dev.json | 3 + .npmignore | 1 + Makefile | 2 +- package.json | 1 + servers/web.js | 6 + test/mocks/package/koa/default.js | 118 ++++++++++++++++++++ test/mocks/package/koa/deprecated.js | 6 + test/mocks/package/koa/no-bugs.js | 7 ++ test/mocks/package/koa/no-license.js | 7 ++ test/mocks/package/koa/no-readme.js | 6 + test/mocks/package/koa/no-repository.js | 7 ++ test/mocks/package/koa/preferGlobal-true.js | 6 + test/mocks/package/koa/private-package.js | 7 ++ test/mocks/package/koa/readme.md | 99 ++++++++++++++++ test/mocks/package/koa/very-long-name.js | 6 + 15 files changed, 281 insertions(+), 1 deletion(-) create mode 100644 .node-dev.json create mode 100644 test/mocks/package/koa/default.js create mode 100644 test/mocks/package/koa/deprecated.js create mode 100644 test/mocks/package/koa/no-bugs.js create mode 100644 test/mocks/package/koa/no-license.js create mode 100644 test/mocks/package/koa/no-readme.js create mode 100644 test/mocks/package/koa/no-repository.js create mode 100644 test/mocks/package/koa/preferGlobal-true.js create mode 100644 test/mocks/package/koa/private-package.js create mode 100644 test/mocks/package/koa/readme.md create mode 100644 test/mocks/package/koa/very-long-name.js diff --git a/.node-dev.json b/.node-dev.json new file mode 100644 index 0000000..5c2556c --- /dev/null +++ b/.node-dev.json @@ -0,0 +1,3 @@ +{ + "notify": false +} diff --git a/.npmignore b/.npmignore index 78deaaa..da60d58 100644 --- a/.npmignore +++ b/.npmignore @@ -21,3 +21,4 @@ config/web_readme.md .dist/ config/config.js *.sqlite +.node-dev.json diff --git a/Makefile b/Makefile index 59bd893..4a9a5b2 100644 --- a/Makefile +++ b/Makefile @@ -87,7 +87,7 @@ test-travis-mysql: init-mysql test-travis-all: test-travis-sqlite test-travis-mysql dev: - @node_modules/.bin/node-dev --harmony dispatch.js + @NODE_ENV=development node_modules/.bin/node-dev --harmony dispatch.js contributors: install @node_modules/.bin/contributors -f plain -o AUTHORS diff --git a/package.json b/package.json index 73f8537..d4830a6 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "contributors": "*", "istanbul-harmony": "*", "jshint": "*", + "koa-mock": "^1.0.2", "mm": "*", "mocha": "*", "node-dev": "*", diff --git a/servers/web.js b/servers/web.js index 7cb15ba..99bb001 100644 --- a/servers/web.js +++ b/servers/web.js @@ -39,6 +39,12 @@ app.use(middlewares.rt({headerName: 'X-ReadTime'})); app.use(middlewares.rewrite('/favicon.ico', '/favicon.png')); app.use(staticCache); +if (process.env.NODE_ENV === 'development') { + app.use(require('koa-mock')({ + datadir: path.join(rootdir, 'test', 'mocks') + })); +} + app.use(opensearch); app.keys = ['todokey', config.sessionSecret]; app.proxy = true; diff --git a/test/mocks/package/koa/default.js b/test/mocks/package/koa/default.js new file mode 100644 index 0000000..499620e --- /dev/null +++ b/test/mocks/package/koa/default.js @@ -0,0 +1,118 @@ +module.exports = { + title: 'Package - koa', + package: { + name: 'koa', + version: '0.13.0', + description: 'description: Koa web app framework', + registryUrl: 'https://registry.npm.taobao.org/koa', + engines: { + node: { + version: '>= 0.11.13', + title: 'node: >=0.11.13', + badgeURL: 'https://img.shields.io/badge/node-%3E%3D%200.11.13-red.svg?style=flat-square', + } + }, + _publish_on_cnpm: false, + preferGlobal: false, + fromNow: 'a month ago', + lastPublishedUser: { + name: 'dead-horse' + }, + license: { + name: 'MIT', + }, + repository: { + weburl: 'http://npm.taobao.org/package/koa', + }, + bugs: { + url: 'https://github.com/koajs/koa/issues' + }, + dependencies: { + accepts: '^1.1.0', + co: '^3.1.0', + 'content-disposition': '~0.3.0', + cookies: '~0.5.0', + debug: '*', + delegates: '0.0.3', + destroy: '^1.0.3', + 'error-inject': '~1.0.0', + 'escape-html': '~1.0.1', + fresh: '~0.2.1', + 'http-assert': '^1.0.1', + 'http-errors': '^1.2.0', + 'koa-compose': '^2.3.0', + 'koa-is-json': '^1.0.0', + 'media-typer': '~0.3.0', + 'mime-types': '^2.0.0', + 'on-finished': '^2.1.0', + only: '0.0.2', + parseurl: '^1.3.0', + statuses: '^1.1.0', + 'type-is': '^1.5.0', + vary: '^1.0.0' + }, + devDependencies: { + 'istanbul-harmony': '~0.3.0', + 'make-lint': '^1.0.1', + mocha: '^1.17.0', + should: '^3.1.0', + supertest: '~0.13.0', + 'test-console': '^0.7.1' + }, + maintainers: [ + { + name: 'fengmk2', + gravatar: 'https://secure.gravatar.com/avatar/95b9d41231617a05ced5604d242c9670?s=50&d=retro', + }, + { + name: 'jongleberry', + gravatar: 'https://secure.gravatar.com/avatar/6e33cc0412b61cc01daac23c8989003c?s=50&d=retro', + }, + { + name: 'dead_horse', + gravatar: 'https://secure.gravatar.com/avatar/2e7cc21dde5f4e944eefff64bde07136?s=50&d=retro', + }, + { + name: 'fengmk2', + gravatar: 'https://secure.gravatar.com/avatar/95b9d41231617a05ced5604d242c9670?s=50&d=retro', + }, + { + name: 'jongleberry', + gravatar: 'https://secure.gravatar.com/avatar/6e33cc0412b61cc01daac23c8989003c?s=50&d=retro', + }, + { + name: 'dead_horse', + gravatar: 'https://secure.gravatar.com/avatar/2e7cc21dde5f4e944eefff64bde07136?s=50&d=retro', + }, + { + name: 'fengmk2', + gravatar: 'https://secure.gravatar.com/avatar/95b9d41231617a05ced5604d242c9670?s=50&d=retro', + }, + { + name: 'jongleberry', + gravatar: 'https://secure.gravatar.com/avatar/6e33cc0412b61cc01daac23c8989003c?s=50&d=retro', + }, + { + name: 'dead_horse', + gravatar: 'https://secure.gravatar.com/avatar/2e7cc21dde5f4e944eefff64bde07136?s=50&d=retro', + }, + ], + readme: readme(), + }, + download: { + today: 100, + thisweek: 700, + thismonth: 3000, + lastday: 100, + lastweek: 800, + lastmonth: 4000, + }, + __view: 'package' +}; + +function readme() { + var marked = require('marked'); + var fs = require('fs'); + var path = require('path'); + return marked(fs.readFileSync(path.join(__dirname, 'readme.md'), 'utf8')); +} diff --git a/test/mocks/package/koa/deprecated.js b/test/mocks/package/koa/deprecated.js new file mode 100644 index 0000000..cb3cb6d --- /dev/null +++ b/test/mocks/package/koa/deprecated.js @@ -0,0 +1,6 @@ +module.exports = { + package: { + deprecated: 'This package has been deprecated.', + }, + __requires: ['./default'] +}; diff --git a/test/mocks/package/koa/no-bugs.js b/test/mocks/package/koa/no-bugs.js new file mode 100644 index 0000000..577279d --- /dev/null +++ b/test/mocks/package/koa/no-bugs.js @@ -0,0 +1,7 @@ +module.exports = { + package: { + name: 'no-bugs', + bugs: null, + }, + __requires: ['./default'] +}; diff --git a/test/mocks/package/koa/no-license.js b/test/mocks/package/koa/no-license.js new file mode 100644 index 0000000..4466976 --- /dev/null +++ b/test/mocks/package/koa/no-license.js @@ -0,0 +1,7 @@ +module.exports = { + package: { + name: 'no-license', + license: null, + }, + __requires: ['./default'] +}; diff --git a/test/mocks/package/koa/no-readme.js b/test/mocks/package/koa/no-readme.js new file mode 100644 index 0000000..0238ae8 --- /dev/null +++ b/test/mocks/package/koa/no-readme.js @@ -0,0 +1,6 @@ +module.exports = { + package: { + readme: '', + }, + __requires: ['./default'] +}; diff --git a/test/mocks/package/koa/no-repository.js b/test/mocks/package/koa/no-repository.js new file mode 100644 index 0000000..379d977 --- /dev/null +++ b/test/mocks/package/koa/no-repository.js @@ -0,0 +1,7 @@ +module.exports = { + package: { + name: 'koa', + repository: null, + }, + __requires: ['./default'] +}; diff --git a/test/mocks/package/koa/preferGlobal-true.js b/test/mocks/package/koa/preferGlobal-true.js new file mode 100644 index 0000000..ef3a529 --- /dev/null +++ b/test/mocks/package/koa/preferGlobal-true.js @@ -0,0 +1,6 @@ +module.exports = { + package: { + preferGlobal: true, + }, + __requires: ['./default'] +}; diff --git a/test/mocks/package/koa/private-package.js b/test/mocks/package/koa/private-package.js new file mode 100644 index 0000000..7817508 --- /dev/null +++ b/test/mocks/package/koa/private-package.js @@ -0,0 +1,7 @@ +module.exports = { + package: { + name: '@fengmk2/fs', + _publish_on_cnpm: true, + }, + __requires: ['./default'] +}; diff --git a/test/mocks/package/koa/readme.md b/test/mocks/package/koa/readme.md new file mode 100644 index 0000000..85e47ef --- /dev/null +++ b/test/mocks/package/koa/readme.md @@ -0,0 +1,99 @@ +koa middleware framework for nodejs + + [![NPM version][npm-image]][npm-url] + [![build status][travis-image]][travis-url] + [![Test coverage][coveralls-image]][coveralls-url] + + [![gitter][gitter-image]][gitter-url] + + Expressive middleware for node.js using generators via [co](https://github.com/visionmedia/co) + to make web applications and APIs more enjoyable to write. Koa's middleware flow in a stack-like manner allowing you to perform actions downstream, then filter and manipulate the response upstream. Koa's use of generators also greatly increases the readability and robustness of your application. + + Only methods that are common to nearly all HTTP servers are integrated directly into Koa's small ~550 SLOC codebase. This + includes things like content-negotiation, normalization of node inconsistencies, redirection, and a few others. + + No middleware are bundled with koa. If you prefer to only define a single dependency for common middleware, much like Connect, you may use + [koa-common](https://github.com/koajs/common). + +## Installation + +``` +$ npm install koa +``` + + To use Koa you must be running __node 0.11.9__ or higher for generator support, and must run node(1) + with the `--harmony` flag. If you don't like typing this, add an alias to your shell profile: + +``` +alias node='node --harmony' +``` + +## Community + + - [API](docs/api/index.md) documentation + - [Badgeboard](https://koajs.github.io/badgeboard) and list of official modules + - [Examples](https://github.com/koajs/examples) + - [Middleware](https://github.com/koajs/koa/wiki) list + - [Wiki](https://github.com/koajs/koa/wiki) + - [G+ Community](https://plus.google.com/communities/101845768320796750641) + - [Reddit Community](http://reddit.com/r/koajs) + - [Mailing list](https://groups.google.com/forum/#!forum/koajs) + - [Guide](docs/guide.md) + - [FAQ](docs/faq.md) + - [中文文档](http://koajs.cn/) + - __#koajs__ on freenode + +## Getting started + + - [kick-off-koa](https://github.com/koajs/kick-off-koa) - An intro to koa via a set of self-guided workshops. + - [workshop](https://github.com/koajs/workshop) - A workshop to learn the basics of koa, Express' spiritual successor. + +## Example + +```js +var koa = require('koa'); +var app = koa(); + +// logger + +app.use(function *(next){ + var start = new Date; + yield next; + var ms = new Date - start; + console.log('%s %s - %s', this.method, this.url, ms); +}); + +// response + +app.use(function *(){ + this.body = 'Hello World'; +}); + +app.listen(3000); +``` + +## Running tests + +``` +$ make test +``` + +## Authors + + - [TJ Holowaychuk](https://github.com/tj) + - [Jonathan Ong](https://github.com/jonathanong) + - [Julian Gruber](https://github.com/juliangruber) + - [Yiyu He](https://github.com/dead-horse) + +# License + + MIT + +[npm-image]: https://img.shields.io/npm/v/koa.svg?style=flat-square +[npm-url]: https://npmjs.org/package/koa +[travis-image]: https://img.shields.io/travis/koajs/koa/master.svg?style=flat-square +[travis-url]: https://travis-ci.org/koajs/koa +[coveralls-image]: https://img.shields.io/coveralls/koajs/koa/master.svg?style=flat-square +[coveralls-url]: https://coveralls.io/r/koajs/koa?branch=master +[gitter-image]: https://badges.gitter.im/Join%20Chat.svg +[gitter-url]: https://gitter.im/koajs/koa?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge diff --git a/test/mocks/package/koa/very-long-name.js b/test/mocks/package/koa/very-long-name.js new file mode 100644 index 0000000..a4264bf --- /dev/null +++ b/test/mocks/package/koa/very-long-name.js @@ -0,0 +1,6 @@ +module.exports = { + package: { + name: 'inch-plugin-socket-behaviour-desktop', + }, + __requires: ['./default'] +};