From 99561bb412bab8de0bc2fbbd23939178a06dcb65 Mon Sep 17 00:00:00 2001 From: lichengyin Date: Sun, 27 Dec 2015 21:20:32 +0800 Subject: [PATCH] move subdomain_deploy middleware to src/middleware path --- src/bootstrap/middleware.js | 19 -------- src/middleware/subdomain_deploy.js | 24 ++++++++++ test/bootstrap/middleware.js | 43 +---------------- test/middleware/subdomain.js | 75 ++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 61 deletions(-) create mode 100644 src/middleware/subdomain_deploy.js create mode 100644 test/middleware/subdomain.js diff --git a/src/bootstrap/middleware.js b/src/bootstrap/middleware.js index ee8d2836..028b62c7 100644 --- a/src/bootstrap/middleware.js +++ b/src/bootstrap/middleware.js @@ -86,23 +86,4 @@ think.middleware('rewrite_pathname', http => { pathname = pathname.substr(0, pathname.length - suffix.length); } http.pathname = pathname; -}); - - -/** - * sub domain deploy - * @param {Object} http){} - * @return {} - */ -think.middleware('subdomain_deploy', http => { - let subdomain = http.config('subdomain'); - if (think.isEmpty(subdomain)) { - return; - } - let hostname = http.hostname.split('.')[0]; - let value = subdomain[hostname]; - if (!value) { - return; - } - http.pathname = value + '/' + http.pathname; }); \ No newline at end of file diff --git a/src/middleware/subdomain_deploy.js b/src/middleware/subdomain_deploy.js new file mode 100644 index 00000000..57ce165f --- /dev/null +++ b/src/middleware/subdomain_deploy.js @@ -0,0 +1,24 @@ +'use strict'; +/** + * subdomain + * @type {} + */ +export default class extends think.middleware.base { + /** + * run + * @return {} [] + */ + run(){ + let subdomain = this.config('subdomain'); + if (think.isEmpty(subdomain)) { + return; + } + let http = this.http; + let hostname = http.hostname.split('.')[0]; + let value = subdomain[hostname]; + if (!value) { + return; + } + http.pathname = value + '/' + http.pathname; + } +} \ No newline at end of file diff --git a/test/bootstrap/middleware.js b/test/bootstrap/middleware.js index 6a02b34c..289a382f 100644 --- a/test/bootstrap/middleware.js +++ b/test/bootstrap/middleware.js @@ -287,48 +287,7 @@ describe('bootstrap/middleware.js', function(){ }) }) }) - it('subdomain_deploy, subdomain emtpy', function(done){ - getHttp({ - subdomain: {}, - }, { - pathname: '/test/welefen.text' - }).then(function(http){ - think.middleware('subdomain_deploy', http).then(function(data){ - assert.equal(http.pathname, '/test/welefen.text'); - done(); - }) - }) - }) - it('subdomain_deploy, subdomain', function(done){ - getHttp({ - subdomain: { - test: 'test' - }, - }, { - hostname: 'www.thinkjs.org', - pathname: '/test/welefen.text' - }).then(function(http){ - think.middleware('subdomain_deploy', http).then(function(data){ - assert.equal(http.pathname, '/test/welefen.text'); - done(); - }) - }) - }) - it('subdomain_deploy, subdomain 1', function(done){ - getHttp({ - subdomain: { - test: 'test' - }, - }, { - hostname: 'test.thinkjs.org', - pathname: 'welefen.text' - }).then(function(http){ - think.middleware('subdomain_deploy', http).then(function(data){ - assert.equal(http.pathname, 'test/welefen.text'); - done(); - }) - }) - }) + diff --git a/test/middleware/subdomain.js b/test/middleware/subdomain.js new file mode 100644 index 00000000..9ef49755 --- /dev/null +++ b/test/middleware/subdomain.js @@ -0,0 +1,75 @@ +var assert = require('assert'); +var path = require('path'); +var fs = require('fs'); +var muk = require('muk'); + +var _http = require('../_http.js'); + +function getHttp(config, options){ + think.APP_PATH = path.dirname(__dirname) + think.sep + 'testApp'; + var req = think.extend({}, _http.req); + var res = think.extend({}, _http.res); + return think.http(req, res).then(function(http){ + if(config){ + http._config = config; + } + if(options){ + for(var key in options){ + http[key] = options[key]; + } + } + return http; + }) +} + +function execMiddleware(middleware, config, options, data){ + return getHttp(config, options).then(function(http){ + return think.middleware(middleware, http, data); + }) +} + + +describe('middleware/subdomain', function(){ + it('subdomain_deploy, subdomain emtpy', function(done){ + getHttp({ + subdomain: {}, + }, { + pathname: '/test/welefen.text' + }).then(function(http){ + think.middleware('subdomain_deploy', http).then(function(data){ + assert.equal(http.pathname, '/test/welefen.text'); + done(); + }) + }) + }) + it('subdomain_deploy, subdomain', function(done){ + getHttp({ + subdomain: { + test: 'test' + }, + }, { + hostname: 'www.thinkjs.org', + pathname: '/test/welefen.text' + }).then(function(http){ + think.middleware('subdomain_deploy', http).then(function(data){ + assert.equal(http.pathname, '/test/welefen.text'); + done(); + }) + }) + }) + it('subdomain_deploy, subdomain 1', function(done){ + getHttp({ + subdomain: { + test: 'test' + }, + }, { + hostname: 'test.thinkjs.org', + pathname: 'welefen.text' + }).then(function(http){ + think.middleware('subdomain_deploy', http).then(function(data){ + assert.equal(http.pathname, 'test/welefen.text'); + done(); + }) + }) + }) +}) \ No newline at end of file