-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtests.js
234 lines (194 loc) · 7.97 KB
/
tests.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
var parseString = Meteor.wrapAsync(Npm.require('xml2js').parseString);
var zlib = Npm.require('zlib');
var http = Npm.require('http');
// Expects a "url" relative to ROOT_URL.
// Inherently validates XML and throws an error if invalid
function fetch(url) {
var res = HTTP.get(Meteor.absoluteUrl() + url);
return parseString(res.content);
}
function addAndFetch(data) {
var url = Random.id();
sitemaps.config('gzip', false);
sitemaps.add(url, data);
return fetch(url);
}
function addAndFetchGzip(data, cb) {
var route = Meteor.absoluteUrl();
var url = Random.id();
sitemaps.config('gzip', true);
sitemaps.add(url, data);
http.get(route + url, Meteor.bindEnvironment(function(res) {
var re = [];
res.on('data', function(chunk) {
re.push(Buffer.from(chunk));
});
res.on('end', Meteor.bindEnvironment(function() {
zlib.gunzip(Buffer.concat(re), Meteor.bindEnvironment(function(err, buff) {
var xml = parseString(buff.toString());
cb && cb(xml);
}));
}));
}));
}
/* urls */
Tinytest.add('sitemaps - config - sitemaps.config(key, value);', function(test) {
sitemaps.config('testkey', 123);
test.equal(sitemaps._config.testkey, 123);
});
Tinytest.add('sitemaps - config - sitemaps.config({key1: value1, key2: value2})', function(test) {
sitemaps.config({testkey1: 1, testkey2: 2});
test.equal(sitemaps._config.testkey1, 1);
test.equal(sitemaps._config.testkey2, 2);
});
Tinytest.add('sitemaps - urls - relative with leading /', function(test) {
test.equal(sitemaps._prepareUrl('/test'),
Meteor.absoluteUrl() + 'test');
});
Tinytest.add('sitemaps - urls - relative without leading /', function(test) {
test.equal(sitemaps._prepareUrl('test'),
Meteor.absoluteUrl() + 'test');
});
Tinytest.add('sitemaps - urls - uri escaped', function(test) {
test.equal(sitemaps._prepareUrl('space space.html'),
Meteor.absoluteUrl() + 'space%20space.html');
});
Tinytest.add('sitemaps - urls - & must be escaped', function(test) {
test.equal(sitemaps._prepareUrl('?arg1=val1&arg2=val2&arg3=val3'),
Meteor.absoluteUrl() + '?arg1=val1&arg2=val2&arg3=val3');
});
Tinytest.add('sitemaps - urls - custom root', function(test) {
var rootUrl = 'http://custom.com/';
sitemaps.config('rootUrl', rootUrl);
test.equal(sitemaps._prepareUrl('custom'), rootUrl+'custom');
sitemaps.config('rootUrl', undefined);
});
Tinytest.add('sitemaps - urls - absolute url', function(test) {
test.equal(sitemaps._prepareUrl('http://cdn.com/test'),
'http://cdn.com/test');
});
Tinytest.add('sitemaps - urls - used by loc/xhtml/image/video', function(test) {
var sitemapUrl = 'usedBy.xml';
var unescapedUrl = ' ';
var escapedUrl = Meteor.absoluteUrl() + '%20';
sitemaps.config('gzip', false);
sitemaps.add(sitemapUrl, [
{
page: unescapedUrl,
images: [ { loc: unescapedUrl } ],
videos: [ { loc: unescapedUrl } ],
xhtmlLinks: [ { href: unescapedUrl } ]
}
]);
var sitemap = fetch(sitemapUrl);
var page = sitemap.urlset.url[0];
test.equal(page.loc[0], escapedUrl);
test.equal(page['xhtml:link'][0].$.href, escapedUrl);
test.equal(page['image:image'][0]['image:loc'][0], escapedUrl);
test.equal(page['video:video'][0]['video:loc'][0], escapedUrl);
});
/* general */
Tinytest.add('sitemaps - README example generates valid sitemap', function(test) {
sitemaps.config('gzip', false);
sitemaps.add('/readme.xml', function() {
// required: page
// optional: lastmod, changefreq, priority, xhtmlLinks, images, videos
return [
{ page: '/x', lastmod: new Date() },
{ page: '/y', lastmod: new Date(), changefreq: 'monthly' },
{ page: '/z', lastmod: new Date().getTime(), changefreq: 'monthly', priority: 0.8 },
// https://support.google.com/webmasters/answer/178636?hl=en
{ page: '/pageWithViedeoAndImages',
images: [
{ loc: '/myImg.jpg', }, // Only loc is required
{ loc: '/myOtherImg.jpg', // Below properties are optional
caption: "..", geo_location: "..", title: "..", license: ".."}
],
videos: [
{ loc: '/myVideo.jpg', }, // Only loc is required
{ loc: '/myOtherVideo.jpg', // Below properties are optional
thumbnail_loc: "..", title: "..", description: "..", etc: '..' }
]
},
// https://support.google.com/webmasters/answer/2620865?hl=en
{ page: 'lang/english', xhtmlLinks: [
{ rel: 'alternate', hreflang: 'de', href: '/lang/deutsch' },
{ rel: 'alternate', hreflang: 'de-ch', href: '/lang/schweiz-deutsch' },
{ rel: 'alternate', hreflang: 'en', href: '/lang/english' }
]}
];
});
test.ok(fetch('readme.xml'));
});
Tinytest.add('sitemaps - sitemap+page relative URL with leading /', function(test) {
sitemaps.config('gzip', false);
sitemaps.add('/sitemap1.xml', [ { page: '/page2' } ]);
var sitemap = fetch('sitemap1.xml'); // throws error if /test1.xml isn't served
var url = sitemap.urlset.url[0].loc[0];
test.equal(url, Meteor.absoluteUrl('page2'));
});
Tinytest.add('sitemaps - sitemap+page relative URL without leading /', function(test) {
sitemaps.config('gzip', false);
sitemaps.add('sitemap2.xml', [ { page: 'page2' } ]);
var sitemap = fetch('sitemap2.xml'); // throws error if /test1.xml isn't served
var url = sitemap.urlset.url[0].loc[0];
test.equal(url, Meteor.absoluteUrl('page2'));
});
/* namespaces */
Tinytest.add('sitemaps - namespaces - xmlns always', function(test) {
var sitemap = addAndFetch([ { page: 'x' } ]);
test.equal(sitemap.urlset.$.xmlns, 'http://www.sitemaps.org/schemas/sitemap/0.9');
test.equal(Object.keys(sitemap.urlset.$).length, 1);
});
Tinytest.add('sitemaps - namespaces - xmlns:xhtml on xhtmlLinks', function(test) {
var sitemap = addAndFetch([ { page: 'x',
xhtmlLinks: [ { rel: 'alternate', hreflang: 'en', href: 'xen' } ] } ]);
test.equal(sitemap.urlset.$['xmlns:xhtml'], 'http://www.w3.org/1999/xhtml');
test.equal(Object.keys(sitemap.urlset.$).length, 2);
});
Tinytest.add('sitemaps - namespaces - xmlns:image on image', function(test) {
var sitemap = addAndFetch([ { page: 'x', images: [ { loc: 'ximg' } ] } ]);
test.equal(sitemap.urlset.$['xmlns:image'],
'http://www.google.com/schemas/sitemap-image/1.1');
test.equal(Object.keys(sitemap.urlset.$).length, 2);
});
Tinytest.add('sitemaps - namespaces - xmlns:video on video', function(test) {
var sitemap = addAndFetch([ { page: 'x', videos: [ { loc: 'xvid' } ] } ]);
test.equal(sitemap.urlset.$['xmlns:video'],
'http://www.google.com/schemas/sitemap-video/1.1');
test.equal(Object.keys(sitemap.urlset.$).length, 2);
});
/* gzip-namespaces */
Tinytest.addAsync('sitemaps-gzip - namespaces - xmlns always', function(test, next) {
addAndFetchGzip([ { page: 'x' } ], function(xml) {
test.equal(xml.urlset.$.xmlns, 'http://www.sitemaps.org/schemas/sitemap/0.9');
test.equal(Object.keys(xml.urlset.$).length, 1);
next();
});
});
Tinytest.addAsync('sitemaps-gzip - namespaces - xmlns:xhtml on xhtmlLinks', function(test, next) {
addAndFetchGzip([{
page: 'x',
xhtmlLinks: [ { rel: 'alternate', hreflang: 'en', href: 'xen' } ]
}], function(xml) {
test.equal(xml.urlset.$['xmlns:xhtml'], 'http://www.w3.org/1999/xhtml');
test.equal(Object.keys(xml.urlset.$).length, 2);
next();
});
});
Tinytest.addAsync('sitemaps-gzip - namespaces - xmlns:image on image', function(test, next) {
addAndFetchGzip([ { page: 'x', images: [ { loc: 'ximg' } ] } ], function(xml) {
test.equal(xml.urlset.$['xmlns:image'],
'http://www.google.com/schemas/sitemap-image/1.1');
test.equal(Object.keys(xml.urlset.$).length, 2);
next();
});
});
Tinytest.addAsync('sitemaps-gzip - namespaces - xmlns:video on video', function(test, next) {
addAndFetchGzip([ { page: 'x', videos: [ { loc: 'xvid' } ] } ], function(xml) {
test.equal(xml.urlset.$['xmlns:video'],
'http://www.google.com/schemas/sitemap-video/1.1');
test.equal(Object.keys(xml.urlset.$).length, 2);
next();
});
});