xml.js
3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*eslint-env mocha*/
/*eslint max-nested-callbacks:[1, 7]*/
/*jshint -W100*/
var assert = require('power-assert');
var each = require('foreach');
var helper = require('./_helper');
var cli = require('../index');
describe('xml', function () {
// 名前空間のコロンはフィルタと混同されないように「\\」でエスケープする
before(function () {
//eslint-disable-next-line no-invalid-this
this.expected = {
channel: {
title: 'タイトル',
description: 'RSSテスト',
language: 'ja',
pubDate: 'Thu, 1 Dec 2016 00:00:00 +0900'
},
item: [{
title: 'RSS記事1',
link: 'http://this.is.rss/news1.html',
pubDate: 'Fri, 2 Dec 2016 00:00:00 +0900',
'dc\\:author': '山田太郎',
category: 'その他'
}, {
title: 'RSS記事2',
link: 'http://this.is.rss/news2.php?aaa=%E3%83%86%E3%82%B9%E3%83%88%E3%81%A7%E3%81%99',
pubDate: 'Sat, 3 Dec 2016 00:00:00 +0900',
'dc\\:author': '山田次郎',
category: ' 未登録 '
}]
};
});
describe('xmlModeでパースされる', function () {
each([ 'xml', 'rss', 'rdf', 'atom', 'opml', 'xsl', 'xslt' ], function (ext) {
var contentType = 'text/html';
var caption = '拡張子';
if (ext === 'xml') {
contentType = 'application/xml';
caption = 'Content-Type';
}
it(ext + ': ' + caption + 'で判別', function (done) {
//eslint-disable-next-line no-invalid-this
var _this = this;
cli.fetch(helper.url('~xml') + '.' + ext, function (err, $, res, body) {
assert(res.headers['content-type'] === contentType);
assert($.documentInfo().isXml === true);
var expected = _this.expected;
each(expected.channel, function (val, name) {
assert($('channel > ' + name).text() === val);
});
assert($('item').length === expected.item.length);
each(expected.item, function (exp, i) {
each(exp, function (val, name) {
assert($('item').eq(i).children(name).text() === val);
});
});
done();
});
});
});
});
describe('forceHtml: true', function () {
before(function () {
cli.set('forceHtml', true);
});
after(function () {
cli.set('forceHtml', false);
});
each([ 'xml', 'rss', 'rdf', 'atom', 'opml', 'xsl', 'xslt' ], function (ext) {
var contentType = (ext === 'xml') ? 'application/xml' : 'text/html';
it(ext + ': xmlModeが使用されない', function (done) {
//eslint-disable-next-line no-invalid-this
var _this = this;
cli.fetch(helper.url('~xml') + '.' + ext, function (err, $, res, body) {
assert(res.headers['content-type'] === contentType);
assert($.documentInfo().isXml === false);
var expected = _this.expected;
each(expected.channel, function (val, name) {
assert($('channel > ' + name).text() === val);
});
assert($('item').length === expected.item.length);
each(expected.item.map(function (v, i) {
v.link = '';
if (i === 1) {
v.category = '';
}
return v;
}), function (exp, i) {
each(exp, function (val, name) {
assert($('item').eq(i).children(name).text() === val);
});
});
done();
});
});
});
});
});