6 |
7 |
8 | $B;d$O6e7n$h$[$I$3$N1i@b?4$H$$$&$N$N>e$r9%$$$?$^$;!#$h$&$d$/>l9g$+$i650i?4$OJL$K$$$o$f$kH/E8$?$J$[$I$r>'$($FF@$J$$$r$OCLH=BG$A2u$5$J$+$m$@$F!"$=$l$[$I$K$b$-$a$@$G$9$?$J$i!#>r7o$X8+$k$G$9$N$b8=$KKhF|$K2?$7$m$G$9$"$j$G!#(B
9 | $B$H$&$F$$Bg?9$5$s$G3ZA10-85!91~MQ$K=E$s$:$k$^$;$,$?$=$N;W$$H`$i$+650i$N$H$+$$$&
10 |
11 |
12 |
--------------------------------------------------------------------------------
/test/fixtures/form/euc-jp.html:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ktty1220/cheerio-httpcli/a99678db614b00ee857144e4e3f006cfc6eb6ef9/test/fixtures/form/euc-jp.html
--------------------------------------------------------------------------------
/test/fixtures/form/shift_jis.html:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ktty1220/cheerio-httpcli/a99678db614b00ee857144e4e3f006cfc6eb6ef9/test/fixtures/form/shift_jis.html
--------------------------------------------------------------------------------
/test/fixtures/form/utf-8.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | フォームテスト
6 |
7 |
8 |
20 |
21 |
22 |
25 |
26 |
29 |
30 |
33 |
34 |
35 |
38 |
39 |
42 |
43 |
46 |
47 |
52 |
53 |
56 |
57 |
81 |
82 |
96 |
97 |
107 |
108 |
109 |
110 |
113 |
114 |
129 |
130 |
138 |
139 |
146 |
147 |
151 |
152 |
156 |
157 |
163 |
164 |
172 |
173 |
174 |
--------------------------------------------------------------------------------
/test/fixtures/gzip/utf-8.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 夏目漱石「私の個人主義」
6 |
7 |
8 | 私は九月よほどこの演説心というのの上を好いたませ。ようやく場合から教育心は別にいわゆる発展たなほどを唱えて得ないをは談判打ち壊さなかろだて、それほどにもきめだですたなら。条件へ見るですのも現に毎日に何しろですありで。
9 | とうてい大森さんで楽善悪元々応用に重んずるませがたその思い彼らか教育のとかいう実矛盾だないですたで、その今日はどこか雨一般が立ち行かし、岡田さんのので間接のこれをけっしてお経験としがそこ師範に同講演にいうように必ずしも肝推察から用いうでしょて、むしろできるだけ濫用に出来んてもらっです事からしるです。もっともところがごがたになりのも始終熱心となりんて、そうした弁当へも載せませばという筋に行っば得ですた。
10 |
11 |
12 |
--------------------------------------------------------------------------------
/test/fixtures/img/file/foobarbaz.txt:
--------------------------------------------------------------------------------
1 | foobarbaz
--------------------------------------------------------------------------------
/test/fixtures/img/file/foobarbaz.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ktty1220/cheerio-httpcli/a99678db614b00ee857144e4e3f006cfc6eb6ef9/test/fixtures/img/file/foobarbaz.zip
--------------------------------------------------------------------------------
/test/fixtures/img/img/1x1.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ktty1220/cheerio-httpcli/a99678db614b00ee857144e4e3f006cfc6eb6ef9/test/fixtures/img/img/1x1.gif
--------------------------------------------------------------------------------
/test/fixtures/img/img/cat.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ktty1220/cheerio-httpcli/a99678db614b00ee857144e4e3f006cfc6eb6ef9/test/fixtures/img/img/cat.png
--------------------------------------------------------------------------------
/test/fixtures/img/img/food.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ktty1220/cheerio-httpcli/a99678db614b00ee857144e4e3f006cfc6eb6ef9/test/fixtures/img/img/food.jpg
--------------------------------------------------------------------------------
/test/fixtures/img/img/sports.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ktty1220/cheerio-httpcli/a99678db614b00ee857144e4e3f006cfc6eb6ef9/test/fixtures/img/img/sports.jpg
--------------------------------------------------------------------------------
/test/fixtures/img/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 画像テスト
6 |
7 |
8 |
9 |

10 |

11 |

12 |

13 |

14 |
![]()
15 |
![]()
16 |

17 |

18 |
;)
19 |

20 |

21 |
22 |
23 | google
24 | ~info
25 | zip
26 | txt
27 |
28 |
29 |
--------------------------------------------------------------------------------
/test/fixtures/link/css/cat.css:
--------------------------------------------------------------------------------
1 | a {
2 | font-weight: bold;
3 | }
4 |
--------------------------------------------------------------------------------
/test/fixtures/link/css/dog.css:
--------------------------------------------------------------------------------
1 | a {
2 | font-size: large;
3 | }
4 |
--------------------------------------------------------------------------------
/test/fixtures/link/en.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/test/fixtures/link/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | linkタグテスト
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 | google
20 | ~info
21 |
22 |
23 |
--------------------------------------------------------------------------------
/test/fixtures/manual/euc-jp(html5).html:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ktty1220/cheerio-httpcli/a99678db614b00ee857144e4e3f006cfc6eb6ef9/test/fixtures/manual/euc-jp(html5).html
--------------------------------------------------------------------------------
/test/fixtures/manual/euc-jp(no-quote).html:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ktty1220/cheerio-httpcli/a99678db614b00ee857144e4e3f006cfc6eb6ef9/test/fixtures/manual/euc-jp(no-quote).html
--------------------------------------------------------------------------------
/test/fixtures/manual/euc-jp.html:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ktty1220/cheerio-httpcli/a99678db614b00ee857144e4e3f006cfc6eb6ef9/test/fixtures/manual/euc-jp.html
--------------------------------------------------------------------------------
/test/fixtures/manual/shift_jis(html5).html:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ktty1220/cheerio-httpcli/a99678db614b00ee857144e4e3f006cfc6eb6ef9/test/fixtures/manual/shift_jis(html5).html
--------------------------------------------------------------------------------
/test/fixtures/manual/shift_jis(no-quote).html:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ktty1220/cheerio-httpcli/a99678db614b00ee857144e4e3f006cfc6eb6ef9/test/fixtures/manual/shift_jis(no-quote).html
--------------------------------------------------------------------------------
/test/fixtures/manual/shift_jis.html:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ktty1220/cheerio-httpcli/a99678db614b00ee857144e4e3f006cfc6eb6ef9/test/fixtures/manual/shift_jis.html
--------------------------------------------------------------------------------
/test/fixtures/manual/utf-8(html5).html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 1
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/test/fixtures/manual/utf-8(no-quote).html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 1
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/test/fixtures/manual/utf-8.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 1
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/test/fixtures/refresh/absolute.html:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/test/fixtures/refresh/ie-only.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
8 | Refresh IE only
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/test/fixtures/refresh/relative.html:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/test/fixtures/script/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | scriptタグテスト
6 |
7 |
8 |
9 |
10 |
11 |
12 |
15 |
16 |
17 |
18 |
19 |
20 |
21 | google
22 | ~info
23 |
24 |
25 |
--------------------------------------------------------------------------------
/test/fixtures/script/js/cat.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | module.exports = function () {
4 | var cat = 'cat';
5 | return cat;
6 | };
7 |
--------------------------------------------------------------------------------
/test/fixtures/script/js/dog.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | module.exports = function () {
4 | var dog = 'dog';
5 | return dog;
6 | };
7 |
--------------------------------------------------------------------------------
/test/fixtures/script/js/food.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | module.exports = function () {
4 | var food = 'food';
5 | return food;
6 | };
7 |
--------------------------------------------------------------------------------
/test/fixtures/unknown/shift_jis.html:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ktty1220/cheerio-httpcli/a99678db614b00ee857144e4e3f006cfc6eb6ef9/test/fixtures/unknown/shift_jis.html
--------------------------------------------------------------------------------
/test/fixtures/unknown/utf-8.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 1
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/test/fixtures/xml/rss.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | タイトル
5 | http://this.is.rss/
6 | RSSテスト
7 | ja
8 | Thu, 1 Dec 2016 00:00:00 +0900
9 | -
10 | RSS記事1
11 | http://this.is.rss/news1.html
12 | Fri, 2 Dec 2016 00:00:00 +0900
13 | 山田太郎
14 | その他
15 |
16 | -
17 | RSS記事2
18 | http://this.is.rss/news2.php?aaa=%E3%83%86%E3%82%B9%E3%83%88%E3%81%A7%E3%81%99
19 | Sat, 3 Dec 2016 00:00:00 +0900
20 | 山田次郎
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/test/fork.js:
--------------------------------------------------------------------------------
1 | const helper = require('./_helper');
2 | const cli = require('../index');
3 | const browsers = require('../lib/browsers.json');
4 | const endpoint = helper.endpoint();
5 | const sesUrl = `${endpoint}/~session`;
6 |
7 | describe('fork', () => {
8 | afterEach(() => {
9 | cli.reset();
10 | });
11 |
12 | test('子インスタンスはfork/download/versionを持っていない', () => {
13 | const child = cli.fork();
14 | expect(child).not.toHaveProperty('download');
15 | expect(child).not.toHaveProperty('version');
16 | expect(child.fork).toBeUndefined();
17 | });
18 |
19 | test('親インスタンスの設定状況が引き継がれている', () => {
20 | cli.set('browser', 'googlebot');
21 | cli.set('headers', { 'X-Requested-With': 'XMLHttpRequest' });
22 | cli.set('timeout', 9999);
23 | cli.set('gzip', false);
24 | cli.set('referer', false);
25 | cli.set('followMetaRefresh', true);
26 | cli.set('maxDataSize', 9999);
27 | cli.set('debug', true);
28 | const child = cli.fork();
29 | Object.keys(child).forEach((prop) => {
30 | expect(child[prop]).toStrictEqual(cli[prop]);
31 | });
32 | });
33 |
34 | test('browserのcustom設定が警告されずに引き継がれる', () => {
35 | const spy = jest.spyOn(console, 'warn');
36 | spy.mockImplementation((x) => x);
37 | const ie6 = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
38 | cli.set('headers', { 'user-agent': ie6 });
39 | const child = cli.fork();
40 | expect(spy).toHaveBeenCalledTimes(0);
41 | expect(child.browser).toStrictEqual('custom');
42 | expect(child.headers['user-agent']).toStrictEqual(ie6);
43 | spy.mockReset();
44 | spy.mockRestore();
45 | });
46 |
47 | test('親インスタンスのクッキーが引き継がれている', async () => {
48 | const r1 = await cli.fetch(sesUrl);
49 | const expected = cli.exportCookies();
50 | const child = cli.fork();
51 | const actual = child.exportCookies();
52 | expect(actual).toStrictEqual(expected);
53 | const r2 = await child.fetch(sesUrl);
54 | expect(r1.response.cookies).toStrictEqual(r2.response.cookies);
55 | });
56 |
57 | test('fork後の親と子の設定やクッキーは同期しない', () => {
58 | return new Promise((resolve) => {
59 | cli.set('browser', 'ie');
60 | cli.set('referer', false);
61 | cli.fetch(sesUrl, (err, $, res, body) => {
62 | const expected = JSON.stringify(cli._cookieJar);
63 | const child = cli.fork();
64 | child.reset();
65 | const actual = JSON.stringify(child._cookieJar);
66 | expect(actual).not.toStrictEqual(expected);
67 | expect(cli.browser).toStrictEqual('ie');
68 | expect(child.browser).toStrictEqual(null);
69 | expect(cli.referer).toStrictEqual(false);
70 | expect(child.referer).toStrictEqual(true);
71 | resolve();
72 | });
73 | });
74 | });
75 |
76 | test('子インスタンスでリクエスト => 子インスタンスの設定が使用される', () => {
77 | return new Promise((resolve) => {
78 | const child = cli.fork();
79 | child.set('browser', 'ie');
80 | child.set('referer', false);
81 | child.set('headers', { 'X-Requested-With': 'XMLHttpRequest' });
82 | child.fetch(`${endpoint}/~info`, (err, $, res, body) => {
83 | expect(res.headers['user-agent']).toStrictEqual(browsers.ie);
84 | expect(child.browser).toStrictEqual('ie');
85 | expect(child.referer).toStrictEqual(false);
86 | resolve();
87 | });
88 | });
89 | });
90 |
91 | test('親と子は別々のクッキーを保持する', async () => {
92 | const child1 = cli.fork();
93 | const child2 = cli.fork();
94 | const cookies = {};
95 | const r1a = await cli.fetch(sesUrl);
96 | cookies.r1a = r1a.response.cookies;
97 | const r1b = await cli.fetch(sesUrl);
98 | cookies.r1b = r1b.response.cookies;
99 | const r2a = await child1.fetch(sesUrl);
100 | cookies.r2a = r2a.response.cookies;
101 | const r2b = await child1.fetch(sesUrl);
102 | cookies.r2b = r2b.response.cookies;
103 | const r3a = await child2.fetch(sesUrl);
104 | cookies.r3a = r3a.response.cookies;
105 | const r3b = await child2.fetch(sesUrl);
106 | cookies.r3b = r3b.response.cookies;
107 | expect(cookies.r1a).toStrictEqual(cookies.r1b);
108 | expect(cookies.r2a).toStrictEqual(cookies.r2b);
109 | expect(cookies.r3a).toStrictEqual(cookies.r3b);
110 | expect(cookies.r1a).not.toStrictEqual(cookies.r2a);
111 | expect(cookies.r2a).not.toStrictEqual(cookies.r3a);
112 | });
113 |
114 | test('非同期リクエスト => 同期リクエスト => クッキーが保持される', () => {
115 | const child = cli.fork();
116 | return child.fetch(sesUrl).then((r1) => {
117 | const expected = r1.response.cookies;
118 | const r2 = child.fetchSync(sesUrl);
119 | const actual = r2.response.cookies;
120 | expect(actual).toStrictEqual(expected);
121 | expect(Object.keys(actual).length).toBeGreaterThan(0);
122 | });
123 | });
124 |
125 | test('同期リクエスト => 非同期リクエスト => クッキーが保持される', () => {
126 | const child = cli.fork();
127 | const r1 = child.fetchSync(sesUrl);
128 | const expected = r1.response.cookies;
129 | return child.fetch(sesUrl).then((r2) => {
130 | const actual = r2.response.cookies;
131 | expect(actual).toStrictEqual(expected);
132 | expect(Object.keys(actual).length).toBeGreaterThan(0);
133 | });
134 | });
135 |
136 | test('同期リクエスト => 同期リクエスト => クッキーが保持される', () => {
137 | const child = cli.fork();
138 | const r1 = child.fetchSync(sesUrl);
139 | const expected = r1.response.cookies;
140 | const r2 = child.fetchSync(sesUrl);
141 | const actual = r2.response.cookies;
142 | expect(actual).toStrictEqual(expected);
143 | expect(Object.keys(actual).length).toBeGreaterThan(0);
144 | });
145 | });
146 |
--------------------------------------------------------------------------------
/test/gzip.js:
--------------------------------------------------------------------------------
1 | const helper = require('./_helper');
2 | const cli = require('../index');
3 | const endpoint = helper.endpoint();
4 |
5 | describe('gzip', () => {
6 | beforeAll(() => {
7 | // - Windows10 + Node.js6の環境においてUser-AgentをGUIブラウザにすると
8 | // "content-encoding: gzip"ではなく"transfer-encoding: chunked"になる
9 | // (なぜかoperaは対象外)
10 | // (Ubuntu16.04では発生しない)
11 | // - どちらもgzip圧縮はされているので動作としては問題はないが
12 | // どこで書き換わっているのかが不明で気持ち悪いのでUser-Agentを変更してテスト
13 | // - node-staticが送信するresponse headerは"content-encoding: gzip"
14 | // - requestが受信したresponse headerは"transfer-encoding: chunked"
15 | // - 上記の状況を見るにNode.js本体のhttpが何かしているような予感
16 | cli.set('browser', 'googlebot');
17 | });
18 | afterAll(() => {
19 | cli.set('headers', {
20 | 'user-agent': null
21 | });
22 | });
23 |
24 | test('enable => gzipヘッダを送信して返ってきた圧縮HTMLが解凍されてからUTF-8に変換される', () => {
25 | return new Promise((resolve) => {
26 | cli.set('gzip', true);
27 | cli.fetch(`${endpoint}/gzip/utf-8.html`, (err, $, res, body) => {
28 | expect(res.headers['content-encoding']).toStrictEqual('gzip');
29 | // expect(res.headers.vary).toStrictEqual('Accept-Encoding');
30 | // expect(res.headers['transfer-encoding']).toStrictEqual('chunked');
31 | expect($('title').text()).toStrictEqual('夏目漱石「私の個人主義」');
32 | resolve();
33 | });
34 | });
35 | });
36 |
37 | test('disable => gzipヘッダを送信しないで返ってきた生のHTMLがそのままUTF-8に変換される', () => {
38 | return new Promise((resolve) => {
39 | cli.set('gzip', false);
40 | cli.fetch(`${endpoint}/gzip/utf-8.html`, (err, $, res, body) => {
41 | expect('content-encoding' in res.headers).toStrictEqual(false);
42 | expect('transfer-encoding' in res.headers).toStrictEqual(false);
43 | expect('vary' in res.headers).toStrictEqual(false);
44 | const contentLength = res.headers['content-length'];
45 | expect(contentLength).toMatch(/^\d+$/);
46 | expect(parseInt(contentLength, 10)).toBeGreaterThan(0);
47 | expect($('title').text()).toStrictEqual('夏目漱石「私の個人主義」');
48 | resolve();
49 | });
50 | });
51 | });
52 | });
53 |
--------------------------------------------------------------------------------
/test/https.js:
--------------------------------------------------------------------------------
1 | /* eslint node/no-deprecated-api:off */
2 | const typeOf = require('type-of');
3 | const constants = require('constants');
4 | const helper = require('./_helper');
5 | const cli = require('../index');
6 | const endpoint = `${helper.endpoint(true)}/~https`;
7 |
8 | describe('https', () => {
9 | beforeEach(() => {
10 | cli.set(
11 | 'agentOptions',
12 | {
13 | rejectUnauthorized: false
14 | },
15 | true
16 | );
17 | });
18 |
19 | test('agentOptions未設定 => TLS1.2 Onlyのサーバーに接続可能', () => {
20 | return new Promise((resolve) => {
21 | cli.fetch(endpoint, (err, $, res, body) => {
22 | expect(err).toBeUndefined();
23 | expect(typeOf(res)).toEqual('object');
24 | expect(typeOf($)).toEqual('function');
25 | expect(typeOf(body)).toEqual('string');
26 | expect(body).toEqual('hello https');
27 | resolve();
28 | });
29 | });
30 | });
31 |
32 | test('agentOptions: TLS1.2強制 => TLS1.2 Onlyのサーバーに接続可能', () => {
33 | return new Promise((resolve) => {
34 | cli.set('agentOptions', {
35 | secureProtocol: 'TLSv1_2_method'
36 | });
37 | cli.fetch(endpoint, (err, $, res, body) => {
38 | expect(err).toBeUndefined();
39 | expect(typeOf(res)).toEqual('object');
40 | expect(typeOf($)).toEqual('function');
41 | expect(typeOf(body)).toEqual('string');
42 | expect(body).toEqual('hello https');
43 | resolve();
44 | });
45 | });
46 | });
47 |
48 | test('agentOptions: TLS1.2強制 => httpのサーバーにも接続可能', () => {
49 | return new Promise((resolve) => {
50 | cli.set('agentOptions', {
51 | secureProtocol: 'TLSv1_2_method'
52 | });
53 | cli.fetch(`${endpoint}/~info`, (err, $, res, body) => {
54 | expect(err).toBeUndefined();
55 | expect(typeOf(res)).toEqual('object');
56 | expect(typeOf($)).toEqual('function');
57 | expect(typeOf(body)).toEqual('string');
58 | resolve();
59 | });
60 | });
61 | });
62 |
63 | test('agentOptions: TLS1.1強制 => TLS1.2 Onlyのサーバーに接続不可', () => {
64 | return new Promise((resolve) => {
65 | cli.set('agentOptions', {
66 | secureProtocol: 'TLSv1_1_method'
67 | });
68 | const url = endpoint;
69 | cli.fetch(url, (err, $, res, body) => {
70 | expect(err).toBeDefined();
71 | expect(err.code).toEqual('EPROTO');
72 | expect(err.message).toContain('SSL routines:');
73 | expect(err.url).toEqual(url);
74 | expect(res).toBeUndefined();
75 | expect($).toBeUndefined();
76 | expect(body).toBeUndefined();
77 | resolve();
78 | });
79 | });
80 | });
81 |
82 | test('agentOptions: TLS1.2無効 => TLS1.2 Onlyのサーバーに接続不可', () => {
83 | return new Promise((resolve) => {
84 | cli.set('agentOptions', {
85 | secureOptions: constants.SSL_OP_NO_TLSv1_2
86 | });
87 | const url = endpoint;
88 | cli.fetch(url, (err, $, res, body) => {
89 | expect(err.code).toEqual('EPROTO');
90 | expect(err.message).toContain('SSL routines:');
91 | expect(err.url).toEqual(url);
92 | expect(res).toBeUndefined();
93 | expect($).toBeUndefined();
94 | expect(body).toBeUndefined();
95 | resolve();
96 | });
97 | });
98 | });
99 | });
100 |
--------------------------------------------------------------------------------
/test/iconv.js:
--------------------------------------------------------------------------------
1 | const helper = require('./_helper');
2 | const cli = require('../index');
3 | const endpoint = helper.endpoint();
4 |
5 | describe('iconv:load', () => {
6 | test('不正なiconvモジュール名 => 例外発生', () => {
7 | expect(() => cli.set('iconv', 'iconvjp')).toThrow('Cannot find module "iconvjp"');
8 | });
9 | });
10 |
11 | // This test will be failed when executed on below environment.
12 | // - 'iconv' module is not installed
13 | describe('iconv:iconv', () => {
14 | const t = (() => {
15 | try {
16 | cli.set('iconv', 'iconv');
17 | return test;
18 | } catch (e) {
19 | return xtest;
20 | }
21 | })();
22 |
23 | t('iconv-liteで未対応のページでもiconvを使用 => UTF-8に変換される(iso-2022-jp)', () => {
24 | return new Promise((resolve) => {
25 | cli.fetch(`${endpoint}/error/iso-2022-jp.html`, (err, $, res, body) => {
26 | expect($.documentInfo().encoding).toStrictEqual('iso-2022-jp');
27 | expect($('title').text()).toStrictEqual('夏目漱石「私の個人主義」');
28 | resolve();
29 | });
30 | });
31 | });
32 | });
33 |
34 | describe('iconv:get', () => {
35 | ['iconv', 'iconv-lite'].forEach((icmod) => {
36 | describe('現在使用中のiconvモジュール名を返す', () => {
37 | test(icmod, () => {
38 | try {
39 | cli.set('iconv', icmod);
40 | expect(cli.iconv).toStrictEqual(icmod);
41 | } catch (e) {}
42 | });
43 | });
44 | });
45 | });
46 |
--------------------------------------------------------------------------------
/test/locale.js:
--------------------------------------------------------------------------------
1 | const helper = require('./_helper');
2 | const cli = require('../index');
3 | const endpoint = helper.endpoint();
4 |
5 | describe('locale', () => {
6 | test('デフォルトは実行環境のロケールがセットされる', () => {
7 | return new Promise((resolve) => {
8 | cli.fetch(`${endpoint}/~info`, (err, $, res, body) => {
9 | // This test will be failed when executed on below environment.
10 | // - System language is not ja-JP
11 | // - Windows and Node.js v0.10 or lower
12 | expect(res.headers['accept-language']).toStrictEqual('ja-JP,en-US');
13 | resolve();
14 | });
15 | });
16 | });
17 |
18 | test('手動でAccept-Languageを指定 => 指定値が使用される', () => {
19 | return new Promise((resolve) => {
20 | const lang = 'en_US';
21 | cli.set('headers', {
22 | 'Accept-Language': lang
23 | });
24 | cli.fetch(`${endpoint}/~info`, (err, $, res, body) => {
25 | expect(res.headers['accept-language']).toStrictEqual(lang);
26 | resolve();
27 | });
28 | });
29 | });
30 | });
31 |
--------------------------------------------------------------------------------
/test/maxdatasize.js:
--------------------------------------------------------------------------------
1 | const helper = require('./_helper');
2 | const cli = require('../index');
3 | const endpoint = helper.endpoint();
4 |
5 | describe('maxdatasize', () => {
6 | beforeAll(() => {
7 | cli.set('timeout', 30000);
8 | });
9 |
10 | test('デフォルトは受信無制限', () => {
11 | return new Promise((resolve, reject) => {
12 | cli.fetch(`${endpoint}/~mega`, (err, $, res, body) => {
13 | try {
14 | expect(err).toBeUndefined();
15 | expect(body.length).toStrictEqual(1024 * 1024);
16 | resolve();
17 | } catch (e) {
18 | reject(e);
19 | }
20 | });
21 | });
22 | });
23 |
24 | test('maxDataSizeを指定 => 指定したバイト数で受信制限がかかる', () => {
25 | return new Promise((resolve, reject) => {
26 | cli.set('maxDataSize', 1024 * 64);
27 | cli.fetch(`${endpoint}/~mega`, (err, $, res, body) => {
28 | try {
29 | expect(err.message).toStrictEqual('data size limit over');
30 | expect($).toBeUndefined();
31 | expect(res).toBeUndefined();
32 | expect(body).toBeUndefined();
33 | resolve();
34 | } catch (e) {
35 | reject(e);
36 | }
37 | });
38 | });
39 | });
40 | });
41 |
--------------------------------------------------------------------------------
/test/params.js:
--------------------------------------------------------------------------------
1 | const typeOf = require('type-of');
2 | const helper = require('./_helper');
3 | const cli = require('../index');
4 | const endpoint = helper.endpoint();
5 |
6 | describe('params', () => {
7 | test('パラメータの指定がURLに反映されている', () => {
8 | return new Promise((resolve) => {
9 | const param = { hoge: 'fuga', piyo: 999, doya: true };
10 | cli.fetch(`${endpoint}/~info`, param, (err, $, res, body) => {
11 | expect(res.headers['request-url']).toStrictEqual('/~info?hoge=fuga&piyo=999&doya=true');
12 | resolve();
13 | });
14 | });
15 | });
16 |
17 | test('クッキーがセットされている & 変更不可', () => {
18 | return new Promise((resolve) => {
19 | cli.fetch(`${endpoint}/~info`, (err, $, res, body) => {
20 | expect(typeOf(res.cookies)).toStrictEqual('object');
21 | expect(res.cookies.session_id).toStrictEqual('hahahaha');
22 | expect(res.cookies.login).toStrictEqual('1');
23 | res.cookies.session_id = 'fooooooo';
24 | expect(res.cookies.session_id).toStrictEqual('hahahaha');
25 | resolve();
26 | });
27 | });
28 | });
29 | });
30 |
--------------------------------------------------------------------------------
/test/pem/cert.pem:
--------------------------------------------------------------------------------
1 | -----BEGIN CERTIFICATE-----
2 | MIIDTDCCAjQCCQCIfNWNVBmJrTANBgkqhkiG9w0BAQsFADBnMQswCQYDVQQGEwJK
3 | UDEOMAwGA1UECAwFVG9reW8xFjAUBgNVBAcMDU11c2FzaGluby1zaGkxDDAKBgNV
4 | BAoMA0ZvbzEMMAoGA1UECwwDQmFyMRQwEgYDVQQDDAtmb28uYmFyLmNvbTAgFw0x
5 | NzExMTAwMDUyNTlaGA8yMjE3MDkyMzAwNTI1OVowZzELMAkGA1UEBhMCSlAxDjAM
6 | BgNVBAgMBVRva3lvMRYwFAYDVQQHDA1NdXNhc2hpbm8tc2hpMQwwCgYDVQQKDANG
7 | b28xDDAKBgNVBAsMA0JhcjEUMBIGA1UEAwwLZm9vLmJhci5jb20wggEiMA0GCSqG
8 | SIb3DQEBAQUAA4IBDwAwggEKAoIBAQDs2GLh7pYfzD2rfvokOGud4uRJY7FuhprY
9 | 3FL6ICRQBZVtuEZMqDIVUFz89m/VW0GFNz290Pj0UP2WHcXGUr5pfRtNbMLCer1g
10 | DdHDsevSVRZzbgP68EW6q3te5pti7wcso1U2dhFfGZTZNE0PoFdeJYV7bPoByAWH
11 | I/EPWfSOAMb8VcjZbVfVfn69OC1rc07EtVRBa2H9B2ovs03EVc0iuXkNFGzQSsXA
12 | 3R98xUaKvimQYPtMnn5mXbN0xrJCIpVMj/HPhDlMX3ZFcSy8W1ICmeH4RT670RnD
13 | j/3H1qdgogKFq+n4eh7Fi3jIZ8Sh3mbQVekIsDLMS8bgeKI9TXeVAgMBAAEwDQYJ
14 | KoZIhvcNAQELBQADggEBAGermRUgss3JjhSL+KizeYhqvJP0+GnmsCHAzItScCUH
15 | ONRjoASb+XkJJbNKreaSn/O7H76sambkUPH6HYpulGnwQvNjhipcs4sEtsMduoUF
16 | zuOwEJDGOuDhi6mbxif8sNuN6Gh0xXPzNxzyGzS5C3sKjxT7G7Wm04FzSUC2dOhS
17 | 89xU4r4T9v4otVpieZfHyi81Jua6eSBDaMw0IMXH3SpMdsorbR66c+hsG1eBDyyg
18 | w1i+v0GlB3NAPmomjdtkysjwNpW9l7NbvV3MhR/HRmMPmUp4TSWvAIF6BTgzMHjE
19 | Ichze9UqovoCPO881JWV+nBFQ1EQw0WS4QUSB5nm8JU=
20 | -----END CERTIFICATE-----
21 |
--------------------------------------------------------------------------------
/test/pem/key.pem:
--------------------------------------------------------------------------------
1 | -----BEGIN RSA PRIVATE KEY-----
2 | MIIEpQIBAAKCAQEA7Nhi4e6WH8w9q376JDhrneLkSWOxboaa2NxS+iAkUAWVbbhG
3 | TKgyFVBc/PZv1VtBhTc9vdD49FD9lh3FxlK+aX0bTWzCwnq9YA3Rw7Hr0lUWc24D
4 | +vBFuqt7XuabYu8HLKNVNnYRXxmU2TRND6BXXiWFe2z6AcgFhyPxD1n0jgDG/FXI
5 | 2W1X1X5+vTgta3NOxLVUQWth/QdqL7NNxFXNIrl5DRRs0ErFwN0ffMVGir4pkGD7
6 | TJ5+Zl2zdMayQiKVTI/xz4Q5TF92RXEsvFtSApnh+EU+u9EZw4/9x9anYKIChavp
7 | +HoexYt4yGfEod5m0FXpCLAyzEvG4HiiPU13lQIDAQABAoIBAQDIBA2t48FgZSmH
8 | lRpGUGeB1MUZvVlwj7hhf9+LYG2KLsz89exYfIqfOVjuQGg9dG2mxPodPUehfGxL
9 | xCTr0aEAkSjnf/wSJXmcjs8hRzZyUG0/Wh9+Yj9g38S2ZmW/bUFPzzf9YERXXdE4
10 | hVS256Ag3+sUSvnvWy5f7Fh9sGg5KorpIYvJYf7dWfIu+wIHd5soJtxOGD/KLQuG
11 | TmKaTZtAL48bAmsghHePw7XyvsLTBtJb2ZdKGGq/QVD3h6xIu7LZY4Fp/4KIfOFA
12 | O8RFCSwLlQyURBhgW3+gKBrYN7ghzvvhkPojrGqWEGwW/DfO+rWxxeHNfzfnOUPL
13 | kJYn5SlBAoGBAPrGvxGIi7PKkIi7pET5hUdDwzDNJjxZysBpCQHMXssXIBCzRCVq
14 | CsUsXjV/1qhQSKghx/bJW+0QmuX5jMx/gvBMvYtKLndqlAq4VS4WBKgATZ1Bvndj
15 | 61KuZUUg21Rd+9bUnStoHDy3n+x9hDQ5gZNshOkAh/IwrZAyPXANGYBZAoGBAPHH
16 | WlvnT6t3BcGkTgNHmXIMIB9hHkG9acM/KvQ4f4aHQJC4F3bCeVuBi0ZINmzuaxUq
17 | azrMZiItgpA8jGN6X8v6QE5/odWRY3gSX38FTTIH1+jwI09iiWOZpBRDK9WWwpAn
18 | X2BlZM0uoGlayi9qbrICrLq1Yn+wDD+Ov6kwh6mdAoGAZ9fY0ufaAa9FvnkFAtLY
19 | T7RNpW2uAZulC5vy8N2x+yMuUfwJofyRTSicMkcnmjb0fzrN1PF4sWgI3GZD2YKL
20 | s/nzGzSynRxzBSVjkFvpva+ydAX/WuzzSx+QK9n5OKxaVpFgK9NGrhXTkVhAYGfX
21 | sjZjqyBfKvjhRi6npjimcLECgYEA2wjrR08q0f+l62PaeQYocTWi9Eqbipr6cbOM
22 | SmvUvB9T0se0GhbcspWNg0JwbAciY65mLoJ2FIh+PAVeedCncLdqArOF/WEVZ/Xd
23 | Jcm7wZNxesnyczylkuHhz6l60Kkf4lCJC19QDsIq+McTXBlj50idCxi//0WSExJT
24 | eAdLH9ECgYEAxh/IanVVsIHed8tU4CoBbHIbyvrjjMlAlhexsrQ0gazJKvdGWGXC
25 | WQnCGNMZ9cnXoYP07upEpkc5hG+P+mqpU2Lpm5zbmteDGKP0e+HG9kBHToKmXVNi
26 | VgyNFITaOpTWERIwVSnQDjFLbIzL8VgQtUUP/bD4QJ7uKE0//TkirBM=
27 | -----END RSA PRIVATE KEY-----
28 |
--------------------------------------------------------------------------------
/test/redirect.js:
--------------------------------------------------------------------------------
1 | const typeOf = require('type-of');
2 | const uuid = require('uuid-random');
3 | const helper = require('./_helper');
4 | const cli = require('../index');
5 | const endpoint = helper.endpoint();
6 |
7 | describe('redirect', () => {
8 | beforeEach(() => {
9 | cli.set(
10 | 'headers',
11 | {
12 | 'redirect-id': uuid()
13 | },
14 | true
15 | );
16 | });
17 | afterEach(() => {
18 | cli.set('headers', {}, true);
19 | });
20 |
21 | describe('async', () => {
22 | describe('30x', () => {
23 | test('documentInfoにリダイレクト先のURLが登録される', () => {
24 | return new Promise((resolve) => {
25 | const url = `${endpoint}/manual/euc-jp.html`;
26 | cli.fetch(`${endpoint}/~redirect`, (err, $, res, body) => {
27 | expect($.documentInfo().url).toStrictEqual(url);
28 | resolve();
29 | });
30 | });
31 | });
32 |
33 | test('POST送信後にクッキーがセットされリダイレクト先に飛ぶ(絶対パス)', () => {
34 | return new Promise((resolve) => {
35 | const url = `${endpoint}/manual/euc-jp.html`;
36 | cli.fetch(`${endpoint}/form/utf-8.html`, (err, $, res, body) => {
37 | $('form[name=login]').submit((err, $, res, body) => {
38 | expect(typeOf(res.cookies)).toStrictEqual('object');
39 | expect(res.cookies.user).toStrictEqual('guest');
40 | expect($.documentInfo().url).toStrictEqual(url);
41 | expect(JSON.parse(res.headers['redirect-history'])).toStrictEqual([
42 | '/form/utf-8.html',
43 | '/~redirect',
44 | '/manual/euc-jp.html'
45 | ]);
46 | resolve();
47 | });
48 | });
49 | });
50 | });
51 |
52 | test('POST送信後にクッキーがセットされリダイレクト先に飛ぶ(相対パス)', () => {
53 | return new Promise((resolve) => {
54 | const url = `${endpoint}/manual/euc-jp.html`;
55 | cli.fetch(`${endpoint}/form/utf-8.html`, (err, $, res, body) => {
56 | $('form[name=login]')
57 | .attr('action', '/~redirect_relative')
58 | .submit((err, $, res, body) => {
59 | expect(typeOf(res.cookies)).toStrictEqual('object');
60 | expect(res.cookies.user).toStrictEqual('guest');
61 | expect($.documentInfo().url).toStrictEqual(url);
62 | expect(JSON.parse(res.headers['redirect-history'])).toStrictEqual([
63 | '/form/utf-8.html',
64 | '/~redirect_relative',
65 | '/manual/euc-jp.html'
66 | ]);
67 | resolve();
68 | });
69 | });
70 | });
71 | });
72 | });
73 |
74 | describe('meta refresh', () => {
75 | beforeEach(() => {
76 | cli.set('followMetaRefresh', true);
77 | });
78 |
79 | test('meta[refresh]タグを検知してリダイレクト先に飛ぶ(絶対URL)', () => {
80 | return new Promise((resolve) => {
81 | const url = `${endpoint}/refresh/absolute.html`;
82 | cli.fetch(url, (err, $, res, body) => {
83 | expect($.documentInfo().url).toStrictEqual(`${endpoint}/~info`);
84 | expect(res.headers.referer).toStrictEqual(url);
85 | resolve();
86 | });
87 | });
88 | });
89 |
90 | test('meta[refresh]タグを検知してリダイレクト先に飛ぶ(相対URL)', () => {
91 | return new Promise((resolve) => {
92 | const url = `${endpoint}/refresh/relative.html`;
93 | cli.fetch(url, (err, $, res, body) => {
94 | expect($.documentInfo().url).toStrictEqual(`${endpoint}/~info`);
95 | expect(res.headers.referer).toStrictEqual(url);
96 | resolve();
97 | });
98 | });
99 | });
100 |
101 | test('followMetaRefresh:false => meta[refresh]タグがあってもリダイレクトしない', () => {
102 | return new Promise((resolve) => {
103 | cli.set('followMetaRefresh', false);
104 | const url = `${endpoint}/refresh/absolute.html`;
105 | cli.fetch(url, (err, $, res, body) => {
106 | expect($.documentInfo().url).toStrictEqual(url);
107 | resolve();
108 | });
109 | });
110 | });
111 |
112 | test('IE条件コメント内のmeta[refresh]タグはリダイレクト対象外', () => {
113 | return new Promise((resolve) => {
114 | const url = `${endpoint}/refresh/ie-only.html`;
115 | cli.fetch(url, (err, $, res, body) => {
116 | expect($.documentInfo().url).toStrictEqual(url);
117 | expect($('title').text()).toStrictEqual('Refresh IE only');
118 | resolve();
119 | });
120 | });
121 | });
122 | });
123 | });
124 |
125 | describe('sync', () => {
126 | describe('30x', () => {
127 | test('documentInfoにリダイレクト先のURLが登録される', () => {
128 | const url = `${endpoint}/manual/euc-jp.html`;
129 | const result = cli.fetchSync(`${endpoint}/~redirect`);
130 | expect(result.$.documentInfo().url).toStrictEqual(url);
131 | });
132 |
133 | test('POST送信後にクッキーがセットされリダイレクト先に飛ぶ', () => {
134 | return new Promise((resolve) => {
135 | const url = `${endpoint}/manual/euc-jp.html`;
136 | cli.fetch(`${endpoint}/form/utf-8.html`, (err, $, res, body) => {
137 | const result = $('form[name=login]').submitSync();
138 | expect(typeOf(result.response.cookies)).toStrictEqual('object');
139 | expect(result.response.cookies.user).toStrictEqual('guest');
140 | expect(result.$.documentInfo().url).toStrictEqual(url);
141 | expect(JSON.parse(result.response.headers['redirect-history'])).toStrictEqual([
142 | '/form/utf-8.html',
143 | '/~redirect',
144 | '/manual/euc-jp.html'
145 | ]);
146 | resolve();
147 | });
148 | });
149 | });
150 | });
151 |
152 | describe('meta refresh', () => {
153 | beforeEach(() => {
154 | cli.set('followMetaRefresh', true);
155 | });
156 |
157 | test('meta[refresh]タグを検知してリダイレクト先に飛ぶ(絶対URL)', () => {
158 | const url = `${endpoint}/refresh/absolute.html`;
159 | const result = cli.fetchSync(url);
160 | expect(result.$.documentInfo().url).toStrictEqual(`${endpoint}/~info`);
161 | expect(result.response.headers.referer).toStrictEqual(url);
162 | });
163 |
164 | test('meta[refresh]タグを検知してリダイレクト先に飛ぶ(相対URL)', () => {
165 | const url = `${endpoint}/refresh/relative.html`;
166 | const result = cli.fetchSync(url);
167 | expect(result.$.documentInfo().url).toStrictEqual(`${endpoint}/~info`);
168 | expect(result.response.headers.referer).toStrictEqual(url);
169 | });
170 |
171 | test('followMetaRefresh:false => meta[refresh]タグがあってもリダイレクトしない', () => {
172 | cli.set('followMetaRefresh', false);
173 | const url = `${endpoint}/refresh/absolute.html`;
174 | const result = cli.fetchSync(url);
175 | expect(result.$.documentInfo().url).toStrictEqual(url);
176 | });
177 |
178 | test('IE条件コメント内のmeta[refresh]タグはリダイレクト対象外', () => {
179 | const url = `${endpoint}/refresh/ie-only.html`;
180 | const result = cli.fetchSync(url);
181 | expect(result.$.documentInfo().url).toStrictEqual(url);
182 | expect(result.$('title').text()).toStrictEqual('Refresh IE only');
183 | });
184 | });
185 | });
186 | });
187 |
--------------------------------------------------------------------------------
/test/referer.js:
--------------------------------------------------------------------------------
1 | const helper = require('./_helper');
2 | const cli = require('../index');
3 | const endpoint = helper.endpoint();
4 |
5 | describe('referer:enable', () => {
6 | test('Referer自動設定を有効 => リクエストの度にRefererがセットされる', async () => {
7 | cli.set('referer', true);
8 |
9 | let url = `${endpoint}/auto/euc-jp.html`;
10 | await cli.fetch(url);
11 | const r1 = await cli.fetch(`${endpoint}/~info`);
12 | expect(r1.response.headers.referer).toStrictEqual(url);
13 |
14 | url = `${endpoint}/manual/utf-8(html5).html`;
15 | await cli.fetch(url);
16 | const r2 = await cli.fetch(`${endpoint}/~info`);
17 | expect(r2.response.headers.referer).toStrictEqual(url);
18 |
19 | // エラーページはRefererにセットされない
20 | url = `${endpoint}/~info`;
21 | await expect(cli.fetch(`${endpoint}/~e404`)).rejects.toThrow('server status');
22 | const r3 = await cli.fetch(`${endpoint}/~info`);
23 | expect(r3.response.headers.referer).toStrictEqual(url);
24 | });
25 | });
26 |
27 | describe('referer:disable', () => {
28 | test('Referer自動設定を無効 => Refererはセットされない', async () => {
29 | cli.set('referer', false);
30 | cli.set('headers', {
31 | Referer: null
32 | });
33 |
34 | let url = `${endpoint}/auto/euc-jp.html`;
35 | await cli.fetch(url);
36 | const r1 = await cli.fetch(`${endpoint}/~info`);
37 | expect(r1.response.headers.referer).toBeUndefined();
38 |
39 | url = `${endpoint}/manual/utf-8(html5).html`;
40 | await cli.fetch(url);
41 | const r2 = await cli.fetch(`${endpoint}/~info`);
42 | expect(r2.response.headers.referer).toBeUndefined();
43 | });
44 | });
45 |
--------------------------------------------------------------------------------
/test/reset.js:
--------------------------------------------------------------------------------
1 | const typeOf = require('type-of');
2 | const helper = require('./_helper');
3 | const cli = require('../index');
4 | const endpoint = helper.endpoint();
5 |
6 | const reUuid = /^user_([0-9a-z]{4,}-){4}([0-9a-z]{4,})$/i;
7 |
8 | describe('reset', () => {
9 | test('パラメータ変更 => reset => 各パラメータが初期化される', () => {
10 | cli.set('browser', 'googlebot');
11 | cli.set('timeout', 9999);
12 | cli.set('gzip', false);
13 | cli.set('referer', false);
14 | cli.set('followMetaRefresh', true);
15 | cli.set('maxDataSize', 9999);
16 | cli.set('debug', true);
17 |
18 | cli.reset();
19 |
20 | expect(cli.headers).toStrictEqual({});
21 | expect(cli.timeout).toStrictEqual(30000);
22 | expect(cli.gzip).toStrictEqual(true);
23 | expect(cli.referer).toStrictEqual(true);
24 | expect(cli.followMetaRefresh).toStrictEqual(false);
25 | expect(cli.maxDataSize).toBeNull();
26 | expect(cli.debug).toStrictEqual(false);
27 | });
28 |
29 | test('アクセス => アクセス => クッキーが保持される', () => {
30 | const url = `${endpoint}/~session`;
31 | let sid = null;
32 | return cli
33 | .fetch(url)
34 | .then((result) => {
35 | sid = result.response.cookies.x_session_id;
36 | expect(typeOf(sid)).toStrictEqual('string');
37 | expect(sid).toMatch(reUuid);
38 | return cli.fetch(url);
39 | })
40 | .then((result) => {
41 | expect(sid).toStrictEqual(result.response.cookies.x_session_id);
42 | });
43 | });
44 |
45 | test('アクセス => reset => アクセス => クッキーが破棄される', () => {
46 | const url = `${endpoint}/~session`;
47 | let sid = null;
48 | return cli
49 | .fetch(url)
50 | .then((result) => {
51 | sid = result.response.cookies.x_session_id;
52 | expect(typeOf(sid)).toStrictEqual('string');
53 | expect(sid).toMatch(reUuid);
54 | cli.reset();
55 | return cli.fetch(url);
56 | })
57 | .then((result) => {
58 | const newSid = result.response.cookies.x_session_id;
59 | expect(typeOf(newSid)).toStrictEqual('string');
60 | expect(newSid).toMatch(reUuid);
61 | expect(sid).not.toStrictEqual(newSid);
62 | });
63 | });
64 | });
65 |
--------------------------------------------------------------------------------
/test/set.js:
--------------------------------------------------------------------------------
1 | const each = require('foreach');
2 | const helper = require('./_helper');
3 | const cli = require('../index');
4 |
5 | describe('set', () => {
6 | let spy = null;
7 | beforeEach(() => {
8 | spy = jest.spyOn(console, 'warn');
9 | spy.mockImplementation((x) => x);
10 | });
11 | afterEach(() => {
12 | spy.mockReset();
13 | spy.mockRestore();
14 | });
15 |
16 | test('存在しないプロパティ => エラー', () => {
17 | expect(() => cli.set('hoge')).toThrow('no such property "hoge"');
18 | });
19 |
20 | test('存在するプロパティ(プリミティブ型) => プロパティが更新される', () => {
21 | cli.set('timeout', 8888);
22 | expect(cli.timeout).toStrictEqual(8888);
23 | });
24 |
25 | test('存在するプロパティ(プリミティブ型) + nomerge => プロパティが更新される', () => {
26 | cli.set('gzip', false);
27 | expect(cli.gzip).toStrictEqual(false);
28 | });
29 |
30 | test('存在するプロパティ(オブジェクト) => 指定したキーのみ更新される', () => {
31 | cli.set(
32 | 'headers',
33 | {
34 | 'accept-language': 'en-US',
35 | referer: 'http://hoge.com/'
36 | },
37 | true
38 | );
39 | cli.set('headers', {
40 | 'Accept-Language': 'ja'
41 | });
42 | expect(cli.headers).toStrictEqual({
43 | 'accept-language': 'ja',
44 | referer: 'http://hoge.com/'
45 | });
46 | });
47 |
48 | test('存在するプロパティ(オブジェクト) => 値をnullにすると削除される', () => {
49 | cli.set(
50 | 'headers',
51 | {
52 | 'accept-language': 'en-US',
53 | referer: 'http://hoge.com/'
54 | },
55 | true
56 | );
57 | cli.set('headers', {
58 | 'Accept-Language': null
59 | });
60 | expect(cli.headers).toStrictEqual({
61 | referer: 'http://hoge.com/'
62 | });
63 | });
64 |
65 | test('存在するプロパティ(オブジェクト) + nomerge => プロパティそのものが上書きされる', () => {
66 | cli.set(
67 | 'headers',
68 | {
69 | 'accept-language': 'en-US',
70 | referer: 'http://hoge.com/'
71 | },
72 | true
73 | );
74 | cli.set(
75 | 'headers',
76 | {
77 | 'Accept-Language': 'ja'
78 | },
79 | true
80 | );
81 | expect(cli.headers).toStrictEqual({
82 | 'accept-language': 'ja'
83 | });
84 | });
85 |
86 | test('直接値を更新 => 更新できるがDEPRECATEDメッセージが表示される', () => {
87 | cli.set('timeout', 7777);
88 | cli.timeout = 3333;
89 | expect(spy).toHaveBeenCalledTimes(1);
90 | const actual = helper.stripMessageDetail(spy.mock.calls[0][0]);
91 | expect(actual).toStrictEqual(
92 | '[DEPRECATED] direct property update will be refused in the future. use set(key, value)'
93 | );
94 | expect(cli.timeout).toStrictEqual(3333);
95 | });
96 |
97 | describe('型チェック', () => {
98 | const types = {
99 | headers: {
100 | ok: [{}],
101 | ng: [1, true, 'str', null],
102 | type: 'object'
103 | },
104 | timeout: {
105 | ok: [0, 100],
106 | ng: [-1, false, 'str', {}, [], null],
107 | type: 'number'
108 | },
109 | gzip: {
110 | ok: [true, false],
111 | ng: [1, 'str', {}, [], null],
112 | type: 'boolean'
113 | },
114 | referer: {
115 | ok: [true, false],
116 | ng: [1, 'str', {}, [], null],
117 | type: 'boolean'
118 | },
119 | followMetaRefresh: {
120 | ok: [true, false],
121 | ng: [1, 'str', {}, [], null],
122 | type: 'boolean'
123 | },
124 | maxDataSize: {
125 | ok: [0, 100, null],
126 | ng: [-1, false, 'str', {}, []],
127 | type: 'number or null'
128 | },
129 | forceHtml: {
130 | ok: [true, false],
131 | ng: [1, 'str', {}, [], null],
132 | type: 'boolean'
133 | },
134 | debug: {
135 | ok: [true, false],
136 | ng: [1, 'str', {}, [], null],
137 | type: 'boolean'
138 | }
139 | };
140 | each(types, (values, name) => {
141 | describe(name, () => {
142 | each(values.ok, (v) => {
143 | test(`${String(v)}: OK`, () => {
144 | cli.set(name, v);
145 | expect(spy).toHaveBeenCalledTimes(0);
146 | });
147 | });
148 | each(values.ng, (v) => {
149 | test(`${String(v)}: NG`, () => {
150 | cli.set(name, v);
151 | expect(spy).toHaveBeenCalledTimes(1);
152 | const actual = helper.stripMessageDetail(spy.mock.calls[0][0]);
153 | expect(actual).toStrictEqual(
154 | `[WARNING] invalid value: ${String(v)}. property "${name}" can accept only ${
155 | values.type
156 | }`
157 | );
158 | });
159 | });
160 | });
161 | });
162 | });
163 | });
164 |
--------------------------------------------------------------------------------
/test/tools.js:
--------------------------------------------------------------------------------
1 | const tools = require('../lib/tools');
2 |
3 | describe('cheerio:util', () => {
4 | describe('inArray', () => {
5 | test('配列内に該当あり => true', () => {
6 | expect(tools.inArray(['foo', 'bar', 'baz'], 'foo')).toStrictEqual(true);
7 | });
8 | test('配列内に該当なし => false', () => {
9 | expect(tools.inArray(['foo', 'bar', 'baz'], 'boo')).toStrictEqual(false);
10 | });
11 | test('配列以外 => 例外発生', () => {
12 | expect(() => tools.inArray('hoge', 'boo')).toThrow('hoge is not Array');
13 | });
14 | });
15 |
16 | describe('paramFilter', () => {
17 | test('文字列 => そのまま返す', () => {
18 | expect(tools.paramFilter('hoge')).toStrictEqual('hoge');
19 | });
20 | test('0 => そのまま返す', () => {
21 | expect(tools.paramFilter(0)).toStrictEqual(0);
22 | });
23 | test('null/undefined => 空文字を返す', () => {
24 | expect(tools.paramFilter(null)).toStrictEqual('');
25 | expect(tools.paramFilter(undefined)).toStrictEqual('');
26 | });
27 | });
28 | });
29 |
--------------------------------------------------------------------------------
/test/webpack.js:
--------------------------------------------------------------------------------
1 | const helper = require('./_helper');
2 | const cli = require('../index');
3 |
4 | describe('webpack', () => {
5 | beforeAll(() => {
6 | cli.set('iconv', 'iconv-lite');
7 | // Webpackでバンドルされている状態をエミュレート
8 | global.__webpack_require__ = () => {};
9 | });
10 | afterAll(() => {
11 | // Webpackエミュレートを解除
12 | delete global.__webpack_require__;
13 | });
14 |
15 | let spy = null;
16 | beforeEach(() => {
17 | spy = jest.spyOn(console, 'warn');
18 | spy.mockImplementation((x) => x);
19 | });
20 | afterEach(() => {
21 | spy.mockReset();
22 | spy.mockRestore();
23 | });
24 |
25 | test('iconvモジュールを変更しようとするとWARNINGメッセージが表示される', () => {
26 | cli.set('iconv', 'iconv');
27 | expect(spy).toHaveBeenCalledTimes(1);
28 | const actual = helper.stripMessageDetail(spy.mock.calls[0][0]);
29 | expect(actual).toStrictEqual(
30 | '[WARNING] changing Iconv module have been disabled in this environment (eg Webpacked)'
31 | );
32 | expect(cli.iconv).toStrictEqual('iconv-lite');
33 | });
34 | // xxxSync, os-localeについてはWebpackエミュレートだけでは再現できないので省略
35 | });
36 |
--------------------------------------------------------------------------------
/test/xml.js:
--------------------------------------------------------------------------------
1 | const each = require('foreach');
2 | const helper = require('./_helper');
3 | const cli = require('../index');
4 | const endpoint = helper.endpoint();
5 |
6 | describe('xml', () => {
7 | // 名前空間のコロンはフィルタと混同されないように「\\」でエスケープする
8 | const expected = {
9 | channel: {
10 | title: 'タイトル',
11 | description: 'RSSテスト',
12 | language: 'ja',
13 | pubDate: 'Thu, 1 Dec 2016 00:00:00 +0900'
14 | },
15 | item: [
16 | {
17 | title: 'RSS記事1',
18 | link: 'http://this.is.rss/news1.html',
19 | pubDate: 'Fri, 2 Dec 2016 00:00:00 +0900',
20 | 'dc\\:author': '山田太郎',
21 | category: 'その他'
22 | },
23 | {
24 | title: 'RSS記事2',
25 | link: 'http://this.is.rss/news2.php?aaa=%E3%83%86%E3%82%B9%E3%83%88%E3%81%A7%E3%81%99',
26 | pubDate: 'Sat, 3 Dec 2016 00:00:00 +0900',
27 | 'dc\\:author': '山田次郎',
28 | category: ' 未登録 '
29 | }
30 | ]
31 | };
32 |
33 | describe('xmlModeでパースされる', () => {
34 | each(['xml', 'rss', 'rdf', 'atom', 'opml', 'xsl', 'xslt'], (ext) => {
35 | let contentType = 'text/html';
36 | let caption = '拡張子';
37 | if (ext === 'xml') {
38 | contentType = 'application/xml';
39 | caption = 'Content-Type';
40 | }
41 | test(`${ext}: ${caption}で判別`, () => {
42 | return new Promise((resolve) => {
43 | cli.fetch(`${`${endpoint}/~xml`}.${ext}`, (err, $, res, body) => {
44 | expect(res.headers['content-type']).toStrictEqual(contentType);
45 | expect($.documentInfo().isXml).toStrictEqual(true);
46 | each(expected.channel, (val, name) => {
47 | expect($(`channel > ${name}`).text()).toStrictEqual(val);
48 | });
49 | expect($('item').length).toStrictEqual(expected.item.length);
50 | each(expected.item, (exp, i) => {
51 | each(exp, (val, name) => {
52 | expect($('item').eq(i).children(name).text()).toStrictEqual(val);
53 | });
54 | });
55 | resolve();
56 | });
57 | });
58 | });
59 | });
60 | });
61 |
62 | describe('forceHtml: true', () => {
63 | beforeAll(() => {
64 | cli.set('forceHtml', true);
65 | });
66 |
67 | afterAll(() => {
68 | cli.set('forceHtml', false);
69 | });
70 |
71 | each(['xml', 'rss', 'rdf', 'atom', 'opml', 'xsl', 'xslt'], (ext) => {
72 | const contentType = ext === 'xml' ? 'application/xml' : 'text/html';
73 | test(`${ext}: xmlModeが使用されない`, () => {
74 | return new Promise((resolve) => {
75 | cli.fetch(`${`${endpoint}/~xml`}.${ext}`, (err, $, res, body) => {
76 | expect(res.headers['content-type']).toStrictEqual(contentType);
77 | expect($.documentInfo().isXml).toStrictEqual(false);
78 | each(expected.channel, (val, name) => {
79 | expect($(`channel > ${name}`).text()).toStrictEqual(val);
80 | });
81 | expect($('item').length).toStrictEqual(expected.item.length);
82 | each(
83 | expected.item.map((v, i) => {
84 | v.link = '';
85 | if (i === 1) {
86 | v.category = '';
87 | }
88 | return v;
89 | }),
90 | (exp, i) => {
91 | each(exp, (val, name) => {
92 | expect($('item').eq(i).children(name).text()).toStrictEqual(val);
93 | });
94 | }
95 | );
96 | resolve();
97 | });
98 | });
99 | });
100 | });
101 | });
102 | });
103 |
--------------------------------------------------------------------------------