1 | 'use strict';
|
2 | const path = require('path');
|
3 | const chai = require('chai');
|
4 | const expect = chai.expect;
|
5 | const AdapterInterface = require(path.join(__dirname, '../../index'));
|
6 |
|
7 | describe('XML adapter spec', function () {
|
8 | describe('.render functionality', function () {
|
9 | let xmlAdapter = AdapterInterface.create({ adapter: 'xml', xml_root: 'example' });
|
10 | it('Should resolve with rendered XML if .sync is not true and no cb is passed', done => {
|
11 | xmlAdapter.render({
|
12 | person: [{
|
13 | firstname: 'Fake',
|
14 | lastname: 'Guy'
|
15 | }]
|
16 | })
|
17 | .try(result => {
|
18 | expect(result).to.be.a('string');
|
19 | expect(/\<status\>200\<\/status\>/.test(result)).to.be.true;
|
20 | done();
|
21 | })
|
22 | .catch(done);
|
23 | });
|
24 | it('Should invoke the callback if cb argument is passed', done => {
|
25 | xmlAdapter.render({
|
26 | person: [{
|
27 | firstname: 'Fake',
|
28 | lastname: 'Guy'
|
29 | }]
|
30 | }, (err, result) => {
|
31 | if (err) done(err);
|
32 | else {
|
33 | expect(result).to.be.a('string');
|
34 | expect(/\<status\>200\<\/status\>/.test(result)).to.be.true;
|
35 | done();
|
36 | }
|
37 | });
|
38 | });
|
39 | it('Should be handled synchronously if .sync is true', () => {
|
40 | let result = xmlAdapter.render({
|
41 | person: [{
|
42 | firstname: 'Fake',
|
43 | lastname: 'Guy'
|
44 | }]
|
45 | }, { sync: true });
|
46 | expect(result).to.be.a('string');
|
47 | expect(/\<status\>200\<\/status\>/.test(result)).to.be.true;
|
48 | });
|
49 | it('Should respect custom XML generation options', done => {
|
50 | let xmlAdapter = AdapterInterface.create({
|
51 | adapter: 'xml',
|
52 | xml_root: 'example',
|
53 | xml_configuration: {
|
54 | declaration: { encoding: 'UTF-8' }
|
55 | }
|
56 | });
|
57 | xmlAdapter.render({
|
58 | person: [{
|
59 | firstname: 'Fake',
|
60 | lastname: 'Guy'
|
61 | }]
|
62 | })
|
63 | .try(result => {
|
64 | expect(result).to.be.a('string');
|
65 | expect(/UTF-8/.test(result)).to.be.true;
|
66 | done();
|
67 | })
|
68 | .catch(done);
|
69 | });
|
70 | it('Should skip conversion if .skip_conversion is true', () => {
|
71 | let result = xmlAdapter.render({
|
72 | person: [{
|
73 | firstname: 'Fake',
|
74 | lastname: 'Guy'
|
75 | }]
|
76 | }, { sync: true, skip_conversion: true });
|
77 | expect(result).to.be.an('object');
|
78 | expect(result).to.be.ok;
|
79 | });
|
80 | });
|
81 | describe('.error functionality', function () {
|
82 | let xmlAdapter = AdapterInterface.create({ adapter: 'xml', xml_root: 'example' });
|
83 | it('Should resolve with rendered XML if .sync is not true and no cb is passed', done => {
|
84 | xmlAdapter.error({
|
85 | message: 'Some Random Error'
|
86 | })
|
87 | .try(result => {
|
88 | expect(result).to.be.a('string');
|
89 | expect(/\<status\>500\<\/status\>/.test(result)).to.be.true;
|
90 | done();
|
91 | })
|
92 | .catch(done);
|
93 | });
|
94 | it('Should invoke the callback if cb argument is passed', done => {
|
95 | xmlAdapter.error({
|
96 | message: 'Some Random Error'
|
97 | }, (err, result) => {
|
98 | if (err) done(err);
|
99 | else {
|
100 | expect(result).to.be.a('string');
|
101 | expect(/\<status\>500\<\/status\>/.test(result)).to.be.true;
|
102 | done();
|
103 | }
|
104 | });
|
105 | });
|
106 | it('Should be handled synchronously if .sync is true', () => {
|
107 | let result = xmlAdapter.error({
|
108 | message: 'Some Random Error'
|
109 | }, { sync: true });
|
110 | expect(result).to.be.a('string');
|
111 | expect(/\<status\>500\<\/status\>/.test(result)).to.be.true;
|
112 | });
|
113 | it('Should respect custom XML generation options', done => {
|
114 | let xmlAdapter = AdapterInterface.create({
|
115 | adapter: 'xml',
|
116 | xml_root: 'example',
|
117 | xml_configuration: {
|
118 | declaration: { encoding: 'UTF-8' }
|
119 | }
|
120 | });
|
121 | xmlAdapter.error({
|
122 | message: 'Some Random Error'
|
123 | })
|
124 | .try(result => {
|
125 | expect(result).to.be.a('string');
|
126 | expect(/UTF-8/.test(result)).to.be.true;
|
127 | done();
|
128 | })
|
129 | .catch(done);
|
130 | });
|
131 | it('Should skip conversion if .skip_conversion is true', () => {
|
132 | let result = xmlAdapter.error({
|
133 | message: 'Some Random Error'
|
134 | }, { sync: true, skip_conversion: true });
|
135 | expect(result).to.be.an('object');
|
136 | expect(result).to.be.ok;
|
137 | });
|
138 | });
|
139 | });
|