mock-routes.js -> mocks.js
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 45cd102 · Browse files · 3 files·+150 added−150 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+150 added−150 removed
- tests:+149 added−149 removed
- other:+1 added−1 removed
Provenance
- Commit
271e982- Tree
9883836- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
45cd102
3 files changed+150 added−150 removed
deleted:lib/express/spec/mock-routes.js-149 removed
1-2-// Express - MockRequest - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3-4-// --- MockRequest5-6-var MockRequest = Class({7- 8- /**9- * Default uri properties.10- */11- 12- uri: {13- queryString: '',14- fragment: '',15- params: {},16- },17- 18- /**19- * Default HTTP version.20- */21- 22- httpVersion: '1.1',23- 24- /**25- * Connection defaults.26- */27- 28- connection: {29- remoteAddress: '127.0.0.1'30- },31- 32- /**33- * Request header defaults.34- */35- 36- headers: {37- 'host': 'localhost',38- 'user-agent': 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/530.19.2 (KHTML, like Gecko) Version/4.0.2 Safari/530.19',39- 'accept': 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',40- 'accept-language': 'en-us',41- 'connection': 'keep-alive'42- },43- 44- /**45- * Initialize with _method_, _path_ and _options_.46- *47- * @param {string} method48- * @param {string} path49- * @param {hash} options50- * @api private51- */52- 53- init: function(method, path, options) {54- this.method = method55- this.uri.path = this.uri.full = path56- process.mixin(true, this, options)57- }58-})59-60-// --- MockResponse61-62-var MockResponse = Class({63- 64- /**65- * Store _code_ and _headers_.66- */67- 68- sendHeader: function(code, headers) {69- this.status = code70- this.headers = headers71- },72- 73- /**74- * Store _body_.75- */76- 77- sendBody: function(body) {78- this.body = body79- },80- 81- /**82- * Flag response as finished.83- */84- 85- finish: function() {86- this.finished = true87- }88-})89-90-/**91- * Mock a request with the given _method_, _path_,92- * and _options_. Returns the response for assertions.93- *94- * All _options_ are passed to MockRequest which95- * performs a deep copy so you may alter headers etc.96- *97- * @param {string} method98- * @param {string} path99- * @param {hash} options100- * @return {MockResponse}101- * @api private102- */103-104-function request(method, path, options, fn) {105- var response = new MockResponse106- var request = new MockRequest(method, path, options)107- Express.server.route(request, response)108- return response109-}110-111-/**112- * Request Express data which may have been113- * manipulated by the mock request.114- *115- * @api public116- */117-118-reset = function() {119- Express.routes = []120- Express.plugins = []121- Express.settings = {}122- configure('test')123-}124-125-/**126- * Return a mock routing function for _method_.127- *128- * @param {string} method129- * @return {function}130- * @api private131- */132-133-function route(method) {134- return function(path, options, fn){135- if (options instanceof Function)136- fn = options, options = {}137- if (fn instanceof Function)138- Express.routes.push(new Route(method, path, fn, options))139- else140- return request(method, path, options, fn)141- }142-}143-144-// --- Mock routing API145-146-get = view = route('get')147-post = create = route('post')148-del = destroy = route('delete')149-put = update = route('put')added:lib/express/spec/mocks.js+149 added
1+2+// Express - MockRequest - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3+4+// --- MockRequest5+6+var MockRequest = Class({7+ 8+ /**9+ * Default uri properties.10+ */11+ 12+ uri: {13+ queryString: '',14+ fragment: '',15+ params: {},16+ },17+ 18+ /**19+ * Default HTTP version.20+ */21+ 22+ httpVersion: '1.1',23+ 24+ /**25+ * Connection defaults.26+ */27+ 28+ connection: {29+ remoteAddress: '127.0.0.1'30+ },31+ 32+ /**33+ * Request header defaults.34+ */35+ 36+ headers: {37+ 'host': 'localhost',38+ 'user-agent': 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/530.19.2 (KHTML, like Gecko) Version/4.0.2 Safari/530.19',39+ 'accept': 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',40+ 'accept-language': 'en-us',41+ 'connection': 'keep-alive'42+ },43+ 44+ /**45+ * Initialize with _method_, _path_ and _options_.46+ *47+ * @param {string} method48+ * @param {string} path49+ * @param {hash} options50+ * @api private51+ */52+ 53+ init: function(method, path, options) {54+ this.method = method55+ this.uri.path = this.uri.full = path56+ process.mixin(true, this, options)57+ }58+})59+60+// --- MockResponse61+62+var MockResponse = Class({63+ 64+ /**65+ * Store _code_ and _headers_.66+ */67+ 68+ sendHeader: function(code, headers) {69+ this.status = code70+ this.headers = headers71+ },72+ 73+ /**74+ * Store _body_.75+ */76+ 77+ sendBody: function(body) {78+ this.body = body79+ },80+ 81+ /**82+ * Flag response as finished.83+ */84+ 85+ finish: function() {86+ this.finished = true87+ }88+})89+90+/**91+ * Mock a request with the given _method_, _path_,92+ * and _options_. Returns the response for assertions.93+ *94+ * All _options_ are passed to MockRequest which95+ * performs a deep copy so you may alter headers etc.96+ *97+ * @param {string} method98+ * @param {string} path99+ * @param {hash} options100+ * @return {MockResponse}101+ * @api private102+ */103+104+function request(method, path, options, fn) {105+ var response = new MockResponse106+ var request = new MockRequest(method, path, options)107+ Express.server.route(request, response)108+ return response109+}110+111+/**112+ * Request Express data which may have been113+ * manipulated by the mock request.114+ *115+ * @api public116+ */117+118+reset = function() {119+ Express.routes = []120+ Express.plugins = []121+ Express.settings = {}122+ configure('test')123+}124+125+/**126+ * Return a mock routing function for _method_.127+ *128+ * @param {string} method129+ * @return {function}130+ * @api private131+ */132+133+function route(method) {134+ return function(path, options, fn){135+ if (options instanceof Function)136+ fn = options, options = {}137+ if (fn instanceof Function)138+ Express.routes.push(new Route(method, path, fn, options))139+ else140+ return request(method, path, options, fn)141+ }142+}143+144+// --- Mock routing API145+146+get = view = route('get')147+post = create = route('post')148+del = destroy = route('delete')149+put = update = route('put')modified:lib/express/spec.js+1 added-1 removed
122
require('express/spec/mock-routes')No newline at end of filerequire('express/spec/mocks')No newline at end of file