-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.js
181 lines (148 loc) · 5.71 KB
/
index.spec.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
/* eslint-env mocha */
const nock = require('nock')
const path = require('path')
/**
* Code under test.
* @type {any}
*/
const T = require('./index.js')
describe('the "w3c-xml-validator" module', function () {
it('must return a function', function () {
const expected = 'function'
const actual = typeof T
expect(actual).to.equal(expected)
})
describe('the value returned by the function', function () {
it('must be a Promise', function () {
const returnValue = T()
expect(returnValue).to.be.a('promise')
})
})
describe('the returned promise', function () {
const ERR_MESSAGE = 'The XML input is required and must be a non-empty string value (or buffer).'
it('must be rejected if the input parameter is missing', function () {
const promise = T()
return expect(promise).to.be.rejectedWith(ERR_MESSAGE)
})
it('must be rejected if the input parameter is `null`', function () {
const promise = T(null)
return expect(promise).to.be.rejectedWith(ERR_MESSAGE)
})
it('must be rejected if the input parameter is an empty string', function () {
const promise = T('')
return expect(promise).to.be.rejectedWith(ERR_MESSAGE)
})
it('must be rejected if the input parameter is not a string', function () {
const promise = T(new Date())
return expect(promise).to.be.rejectedWith(ERR_MESSAGE)
})
it.skip('must be rejected if the remote server is unreachable', function () {
const promise = T('%%TIMEOUT%%')
return expect(promise).to.be.rejectedWith(/ECONNREFUSED/)
})
it('must be rejected if the remote server replies with a 3xx status code', function () {
nock('https://validator.w3.org')
.post('/check')
.reply(302)
const promise = T('<?xml version="1.0" encoding="utf-8"?>')
return expect(promise).to.be.rejectedWith('The W3C server replied with a 302 status code.')
})
it('must be rejected if the remote server replies with a 4xx status code', function () {
nock('https://validator.w3.org')
.post('/check')
.reply(400, 'Bad request')
const promise = T('<?xml version="1.0" encoding="utf-8"?>')
return expect(promise).to.be.rejectedWith('The W3C server replied with a 400 status code.')
})
it('must be rejected if the remote server replies with a 5xx status code', function () {
nock('https://validator.w3.org')
.post('/check')
.reply(503)
const promise = T('<?xml version="1.0" encoding="utf-8"?>')
return expect(promise).to.be.rejectedWith('The W3C server replied with a 503 status code.')
})
describe('the fulfilled value', function () {
context('for a successful validation', function () {
/**
* The value returned by the exported function.
* @type {Promise}
*/
let actual = null
/**
* The expected fulfillment value.
* @type {Object}
*/
const expected = Object.freeze({
doctype: 'http://xml.cxml.org/schemas/cXML/1.2.014/cXML.dtd',
isValid: true,
warnings: [
'No Character encoding declared at document level',
'Using Direct Input mode: UTF-8 character encoding assumed'
],
errors: []
})
before(() => {
nock('https://validator.w3.org')
.post('/check')
.replyWithFile(200, path.join(__dirname, './test/samples/success.html'))
.persist()
})
after(() => {
nock.cleanAll()
})
context('of a string', function () {
before(async function () {
// the value passed to T() can be anything that resembles XML, since
// the response is mocked (and therefore not dependent on the input)
actual = await T('<?xml version="1.0" encoding="utf-8"?>')
})
it('must return the expected value', function () {
expect(actual).to.deep.equal(expected)
})
})
context('of a buffer', function () {
before(async function () {
// the value passed to T() can be anything that resembles XML, since
// the response is mocked (and therefore not dependent on the input)
actual = await T(Buffer.from('<?xml version="1.0" encoding="utf-8"?>'))
})
it('must return the expected value', function () {
expect(actual).to.deep.equal(expected)
})
})
})
context('for a validation with a single error', function () {
/**
* The value returned by the exported function.
* @type {Promise}
*/
let actual = null
/**
* The expected fulfillment value.
* @type {Object}
*/
const expected = Object.freeze({
doctype: 'http://xml.cxml.org/schemas/cXML/1.2.015/cXML.dtd',
isValid: false,
warnings: [
'Using Direct Input mode: UTF-8 character encoding assumed'
],
errors: [
'Line 46: end tag for "PunchOutOrderMessage" omitted, but OMITTAG NO was specified'
]
})
before(async function () {
nock('https://validator.w3.org')
.post('/check')
.replyWithFile(200, path.join(__dirname, './test/samples/single-error.html'))
// the value passed to T() can be anything that resembles XML, since
// the response is mocked (and therefore not dependent on the input)
actual = await T('<?xml version="1.0" encoding="utf-8"?>')
})
it('must return the expected value', function () {
expect(actual).to.deep.equal(expected)
})
})
})
})
})