move subdomain_deploy middleware to src/middleware path

This commit is contained in:
lichengyin 2015-12-27 21:20:32 +08:00
parent 01347dcc0d
commit 99561bb412
4 changed files with 100 additions and 61 deletions

View File

@ -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;
});

View File

@ -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;
}
}

View File

@ -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();
})
})
})

View File

@ -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();
})
})
})
})