Skip to content

Commit

Permalink
Fix: fix get post data when item value is false or 0, fix thinkjs#266
Browse files Browse the repository at this point in the history
  • Loading branch information
welefen committed Feb 22, 2016
1 parent 7ac963b commit 0f6b53f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/core/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,12 @@ export default class {
if (name === undefined) {
return this._get;
}else if (think.isString(name)) {
return this._get[name] || '';
//may be value is false or 0
value = this._get[name];
if(value === undefined){
value = '';
}
return value;
}
this._get = name;
}else{
Expand All @@ -309,7 +314,12 @@ export default class {
if (name === undefined) {
return this._post;
}else if (think.isString(name)) {
return this._post[name] || '';
//may be value is false or 0
value = this._post[name];
if(value === undefined){
value = '';
}
return value;
}
this._post = name;
}else {
Expand Down
52 changes: 51 additions & 1 deletion test/core/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function getDefaultHttp(data) {
};
}

think.APP_PATH = path.dirname(path.dirname(__dirname)) + '/testApp';
think.APP_PATH = path.dirname(path.dirname(__dirname)) + think.sep + 'testApp';

describe('core/http.js', function() {
it('is EventEmitter instance, false', function(done) {
Expand Down Expand Up @@ -791,6 +791,56 @@ describe('core/http.js', function() {
http.sendTime('TEST');
});
});
it('get post data, all', function(done) {
var defaultHttp = getDefaultHttp('/index/index?name=maxzhang&37');
var instance = new Http(defaultHttp.req, defaultHttp.res);
instance.run().then(function(http) {
http._post = {};
var data = http.post();
assert.deepEqual(data, {});
done();
});
});
it('get post data, name', function(done) {
var defaultHttp = getDefaultHttp('/index/index?name=maxzhang&37');
var instance = new Http(defaultHttp.req, defaultHttp.res);
instance.run().then(function(http) {
http._post = {name: 'test'};
var data = http.post('name');
assert.deepEqual(data, 'test');
done();
});
});
it('get post data, 0', function(done) {
var defaultHttp = getDefaultHttp('/index/index?name=maxzhang&37');
var instance = new Http(defaultHttp.req, defaultHttp.res);
instance.run().then(function(http) {
http._post = {name: 0};
var data = http.post('name');
assert.deepEqual(data, 0);
done();
});
});
it('get post data, false', function(done) {
var defaultHttp = getDefaultHttp('/index/index?name=maxzhang&37');
var instance = new Http(defaultHttp.req, defaultHttp.res);
instance.run().then(function(http) {
http._post = {name: false};
var data = http.post('name');
assert.deepEqual(data, false);
done();
});
});
it('get post data, undefined', function(done) {
var defaultHttp = getDefaultHttp('/index/index?name=maxzhang&37');
var instance = new Http(defaultHttp.req, defaultHttp.res);
instance.run().then(function(http) {
http._post = {name: 0};
var data = http.post('name111');
assert.deepEqual(data, '');
done();
});
});

it('write empty', function(done) {
var defaultHttp = getDefaultHttp('/index/index?name=maxzhang&38');
Expand Down

0 comments on commit 0f6b53f

Please sign in to comment.