cheerio-httpcli
Advanced tools
Comparing version 0.3.5 to 0.3.6
@@ -0,1 +1,5 @@ | ||
# 0.3.6 (2015-11-08) | ||
* 生DOM要素から作成したcheerioオブジェクトでclickやsubmitがエラーになっていたのを修正 | ||
# 0.3.5 (2015-11-03) | ||
@@ -2,0 +6,0 @@ |
@@ -48,4 +48,9 @@ /*eslint no-invalid-this:0*/ | ||
*/ | ||
var documentInfo = function () { | ||
// cheerio/lib/static.jsによると大元の_rootは_originalRootという名称で保持されているらしい | ||
return this._root[0]._documentInfo || this._originalRoot._documentInfo; | ||
}; | ||
cheerio.prototype.click = function (callback) { | ||
var doc = this._root[0]._documentInfo; | ||
var doc = documentInfo.call(this); | ||
var $ = cheerio; | ||
@@ -84,3 +89,3 @@ var $link = null; | ||
var doc = this._root[0]._documentInfo; | ||
var doc = documentInfo.call(this); | ||
var $ = cheerio; | ||
@@ -87,0 +92,0 @@ var $form = null; |
{ | ||
"name": "cheerio-httpcli", | ||
"version": "0.3.5", | ||
"version": "0.3.6", | ||
"description": "http client module with cheerio & iconv(-lite) & promise", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -109,2 +109,51 @@ /*eslint-env mocha*/ | ||
}); | ||
[ 0, 1, 2 ].forEach(function (idx) { | ||
it('生のa要素をclickしてもリンク先を取得できる(' + idx + '番目)', function (done) { | ||
cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) { | ||
$($('.rel')[idx]).click(function (err, $, res, body) { | ||
assert.deepEqual($.documentInfo(), { | ||
url: helper.url('auto', 'euc-jp'), | ||
encoding: 'euc-jp' | ||
}); | ||
assert(type(res) === 'object'); | ||
assert(type($) === 'function'); | ||
assert(type(body) === 'string'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('無から作成したa要素をclickしてもリンク先を取得できる(jQuery形式)', function (done) { | ||
cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) { | ||
var url = helper.url('auto', 'utf-8'); | ||
$('<a/>').attr('href', url).click(function (err, $, res, body) { | ||
assert.deepEqual($.documentInfo(), { | ||
url: url, | ||
encoding: 'utf-8' | ||
}); | ||
assert(type(res) === 'object'); | ||
assert(type($) === 'function'); | ||
assert(type(body) === 'string'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('無から作成したa要素をclickしてもリンク先を取得できる(HTML形式)', function (done) { | ||
cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) { | ||
var url = helper.url('auto', 'shift_jis'); | ||
$('<a href="' + url + '">link</a>').click(function (err, $, res, body) { | ||
assert.deepEqual($.documentInfo(), { | ||
url: url, | ||
encoding: 'shift_jis' | ||
}); | ||
assert(type(res) === 'object'); | ||
assert(type($) === 'function'); | ||
assert(type(body) === 'string'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -251,3 +300,3 @@ | ||
it('submit()時に指定するパラメータのvalueがnull/undefined/emptyの場合は"name="という形でURLに追加される', function (done) { | ||
it('submit時に指定するパラメータのvalueがnull/undefined/emptyの場合は"name="という形でURLに追加される', function (done) { | ||
cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) { | ||
@@ -269,3 +318,3 @@ $('form[name=post]').submit({ | ||
it('submit()時に指定するパラメータが数字の0の場合は"name=0"という形でURLに追加される', function (done) { | ||
it('submit時に指定するパラメータが数字の0の場合は"name=0"という形でURLに追加される', function (done) { | ||
cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) { | ||
@@ -284,2 +333,72 @@ $('form[name=post]').submit({ hoge: 0 }, function (err, $, res, body) { | ||
}); | ||
[ 0, 1, 2 ].forEach(function (idx) { | ||
it('生のform要素をsubmitしてもフォーム送信される(' + idx + '番目)', function (done) { | ||
cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) { | ||
$($('.form-group form')[idx]).submit(function (err, $, res, body) { | ||
assert($.documentInfo().url === helper.url('~info') + '?hoge=fuga'); | ||
var h = res.headers; | ||
assert(h['request-url'] === '/~info?hoge=fuga'); | ||
assert(h['request-method'] === 'GET'); | ||
assert(! h['post-data']); | ||
assert(type($) === 'function'); | ||
assert(type(body) === 'string'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('無から作成したform要素をsubmitしてもフォーム送信される(jQuery形式)', function (done) { | ||
cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) { | ||
var $form = $('<form/>').attr({ | ||
method: 'GET', | ||
action: '/~info' | ||
}) | ||
.append($('<input/>').attr({ | ||
type: 'hidden', | ||
name: 'hoge', | ||
value: 'fuga' | ||
})) | ||
.append($('<input/>').attr({ | ||
type: 'text', | ||
name: 'foo', | ||
value: 'あいうえお' | ||
})); | ||
$form.submit(function (err, $, res, body) { | ||
var param = 'hoge=fuga&foo=' + encodeURIComponent('あいうえお'); | ||
assert($.documentInfo().url === helper.url('~info') + '?' + param); | ||
var h = res.headers; | ||
assert(h['request-url'] === '/~info?' + param); | ||
assert(h['request-method'] === 'GET'); | ||
assert(! h['post-data']); | ||
assert(type($) === 'function'); | ||
assert(type(body) === 'string'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('無から作成したform要素をsubmitしてもフォーム送信される(HTML形式)', function (done) { | ||
cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) { | ||
var $form = $([ | ||
'<form method="POST" action="/~info">', | ||
'<input type="hidden" name="hoge" value="fuga" />', | ||
'<input type="text" name="foo" value="あいうえお" />', | ||
'</form>' | ||
].join('\n')); | ||
$form.submit({ foo: 'かきくけこ' }, function (err, $, res, body) { | ||
var param = 'hoge=fuga&foo=' + encodeURIComponent('かきくけこ'); | ||
assert($.documentInfo().url === helper.url('~info')); | ||
var h = res.headers; | ||
assert(h['request-url'] === '/~info'); | ||
assert(h['request-method'] === 'POST'); | ||
assert(h['post-data'] === param); | ||
assert(type($) === 'function'); | ||
assert(type(body) === 'string'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -286,0 +405,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
158579
62
2504