-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
145 lines (125 loc) · 3.68 KB
/
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
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
'use strict';
const express = require('express');
const httpMocks = require('node-mocks-http');
const sitemap = require('./index');
test('add sitemap middleware to express application', () => {
const app = express();
app.use(sitemap());
});
test('skip sitemap middleware', () => {
const request = httpMocks.createRequest({
method: 'GET',
url: '/',
});
const response = httpMocks.createResponse();
sitemap()(request, response, () => {});
expect(response._getData()).toEqual('');
expect(response.statusCode).toEqual(200);
});
test('get empty sitemap from request', () => {
const request = httpMocks.createRequest({
method: 'GET',
url: '/sitemap.xml',
});
const response = httpMocks.createResponse();
sitemap()(request, response, () => {});
expect(response.getHeader('Content-Type')).toEqual('application/xml');
});
test('get filled sitemap from request', () => {
const request = httpMocks.createRequest({
method: 'GET',
url: '/site-map.xml',
});
const response = httpMocks.createResponse();
sitemap({
sitemapUrl: '/site-map.xml',
urls: [
{
url: 'https://test.com/',
hreflang: [{lang: 'ru', url: 'https://test.com/ru/'}],
lastmod: '2018-12-08',
changefreq: 'daily',
priority: 1.0,
},
],
})(request, response, () => {});
expect(response.getHeader('Content-Type')).toEqual('application/xml');
expect(response._getData()).toBe(
`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>https://test,com</loc>
<xhtml:link rel="alternate" hreflang="ru" href ="https://test.com/ru/" />
<lastmod>2018-12-08</lastmod>
<changefreq>daily</changefreq>
<priority>1</priority>
</url>
</urlset>`
);
});
test('get filled sitemap with partly url objects from request', () => {
const request = httpMocks.createRequest({
method: 'GET',
url: '/sitemap.xml',
});
const response = httpMocks.createResponse();
sitemap({
urls: [
{
url: 'https://test.com/',
},
{
url: 'https://test.com/',
lastmod: '2018-12-08',
},
{
url: 'https://test.com/',
lastmod: '2018-12-08',
changefreq: 'always',
},
{
url: 'https://test.com/',
priority: 0.5,
},
{
url: 'https://test.com/',
lastmod: '2018-12-08',
changefreq: 'daily',
priority: 0,
},
{
lastmod: '2018-12-08',
changefreq: 'daily',
priority: 0,
},
],
})(request, response, () => {});
expect(response.getHeader('Content-Type')).toEqual('application/xml');
expect(response._getData()).toBe(
`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>https://test.com/</loc>
</url>
<url>
<loc>https://test.com/</loc>
<lastmod>2018-12-08</lastmod>
</url>
<url>
<loc>https://test.com/</loc>
<lastmod>2018-12-08</lastmod>
<changefreq>always</changefreq>
</url>
<url>
<loc>https://test.com/</loc>
<priority>0.5</priority>
</url>
<url>
<loc>https://test.com/</loc>
<lastmod>2018-12-08</lastmod>
<changefreq>daily</changefreq>
<priority>0</priority>
</url>
</urlset>`
);
});