-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsitemap.test.js
91 lines (71 loc) · 2.67 KB
/
sitemap.test.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
const Fs = require('fs');
const Path = require('path');
const Hapi = require('@hapi/hapi');
const test = require('tape');
const file = require('path').relative(process.cwd(), __filename) + ' > ';
const createMockDatabase = require('./helpers/mock-database');
const createServer = require('../server');
const config = require('../config');
const testWithServer = require('./helpers/test-with-server');
test('setup', async () => {
const sitemapPath = Path.join(__dirname, '..', 'public', 'sitemap.xml');
const sitemapXml = Fs.readFileSync(sitemapPath, 'utf8');
const upstream = new Hapi.Server();
await upstream.register(require('@hapi/inert'));
upstream.route({
method: 'GET',
path: '/sitemap.xml',
handler: (request, h) => h.file(sitemapPath)
});
await upstream.start();
// Set sitemap URL to upstream
const testConfig = Object.assign({}, config, {
sitemapUrl: `http://localhost:${upstream.info.port}`
});
testWithServer(file + 'Should proxy to sitemap', { config: testConfig }, async (t, ctx) => {
t.plan(2);
const request = {
method: 'GET',
url: '/sitemap.xml'
};
const res = await ctx.server.inject(request);
t.equal(res.statusCode, 200, 'Status was OK');
t.equal(res.payload, sitemapXml, 'Sitemap XML was sent');
await upstream.stop();
t.end();
});
});
test(file + 'Should serve local sitemap if no sitemap URL configured', (t) => {
t.plan(3);
const sitemapPath = Path.join(__dirname, '..', 'public', 'sitemap.xml');
const sitemapXml = Fs.readFileSync(sitemapPath, 'utf8');
const testConfig = Object.assign({}, config, { sitemapUrl: '' });
createServer(createMockDatabase(), testConfig, async (err, ctx) => {
t.ifError(err, 'No error creating server');
const request = {
method: 'GET',
url: '/sitemap.xml'
};
const res = await ctx.server.inject(request);
t.equal(res.statusCode, 200, 'Status was OK');
t.equal(res.payload, sitemapXml, 'Sitemap XML was sent');
t.end();
});
});
test(file + 'Should serve local sitemap if sitemap URL is same as base URL', (t) => {
t.plan(3);
const sitemapPath = Path.join(__dirname, '..', 'public', 'sitemap.xml');
const sitemapXml = Fs.readFileSync(sitemapPath, 'utf8');
const testConfig = Object.assign({}, config, { sitemapUrl: config.baseUrl });
createServer(createMockDatabase(), testConfig, async (err, ctx) => {
t.ifError(err, 'No error creating server');
const request = {
method: 'GET',
url: '/sitemap.xml'
};
const res = await ctx.server.inject(request);
t.equal(res.statusCode, 200, 'Status was OK');
t.equal(res.payload, sitemapXml, 'Sitemap XML was sent');
t.end();
});
});