forked from vpulim/node-soap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress-server-test.js
168 lines (149 loc) · 6.75 KB
/
express-server-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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
var request = require('request');
var assert = require('assert');
var express = require('express');
var bodyParser = require('body-parser');
var soap = require('../');
var expressServer;
var server;
var port;
var url;
var wsdl = '<definitions name="HelloService" targetNamespace="http://www.examples.com/wsdl/HelloService.wsdl" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><message name="SayHelloRequest"><part name="firstName" type="xsd:string"/></message><message name="SayHelloResponse"><part name="greeting" type="xsd:string"/></message><portType name="Hello_PortType"><operation name="sayHello"><input message="tns:SayHelloRequest"/><output message="tns:SayHelloResponse"/></operation></portType><binding name="Hello_Binding" type="tns:Hello_PortType"><soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/><operation name="sayHello"><soap:operation soapAction="sayHello"/><input><soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:helloservice" use="encoded"/></input><output><soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:helloservice" use="encoded"/></output></operation></binding><service name="Hello_Service"><documentation>WSDL File for HelloService</documentation><port binding="tns:Hello_Binding" name="Hello_Port"><soap:address location="http://localhost:51515/SayHello/" /></port></service></definitions>';
var requestXML = '<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">' +
'<Body>' +
'<sayHello xmlns="http://www.examples.com/wsdl/HelloService.wsdl">' +
'<firstName>tarun</firstName>' +
'</sayHello>' +
'</Body>' +
'</Envelope>';
var responseXML = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl">' +
'<soap:Body>' +
'<tns:sayHelloResponse>' +
'<tns:greeting>tarun</tns:greeting>' +
'</tns:sayHelloResponse>' +
'</soap:Body>' +
'</soap:Envelope>';
describe('Express server without middleware', function () {
before(function (done) {
var service = {
Hello_Service: {
Hello_Port: {
sayHello: function (args) {
return {
greeting: args.firstName
};
}
}
}
};
expressServer = express();
server = expressServer.listen(51515, function () {
var soapServer = soap.listen(expressServer, '/SayHello', service, wsdl);
url = 'http://' + server.address().address + ':' + server.address().port;
if (server.address().address === '0.0.0.0' || server.address().address === '::') {
url = 'http://127.0.0.1:' + server.address().port;
}
done();
});
});
after(function () {
server.close();
});
it('should handle body without middleware', function (done) {
request({
url: url + '/SayHello',
method: 'POST',
headers: {
SOAPAction: "sayHello",
"Content-Type": 'text/xml; charset="utf-8"'
},
body: requestXML
}, function (err, response, body) {
if (err) {
throw err;
}
assert.equal(body, responseXML);
done();
});
});
it('should serve wsdl', function (done) {
request({
url: url + '/SayHello?wsdl',
method: 'GET',
headers: {
"Content-Type": 'text/xml; charset="utf-8"'
}
}, function (err, response, body) {
if (err) {
throw err;
}
assert.equal(body, wsdl);
done();
});
});
it('should handle other routes as usual', function (done) {
expressServer.route('/test/r1').get(function (req, res, next) {
//make sure next() works as well
return next();
}, function (req, res) {
return res.status(200).send('test passed');
});
request({
url: url + '/test/r1',
method: 'GET'
}, function (err, response, body) {
if (err) {
throw err;
}
assert.equal(body, 'test passed');
done();
});
});
});
describe('Express server with middleware', function () {
before(function (done) {
var wsdl = '<definitions name="HelloService" targetNamespace="http://www.examples.com/wsdl/HelloService.wsdl" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><message name="SayHelloRequest"><part name="firstName" type="xsd:string"/></message><message name="SayHelloResponse"><part name="greeting" type="xsd:string"/></message><portType name="Hello_PortType"><operation name="sayHello"><input message="tns:SayHelloRequest"/><output message="tns:SayHelloResponse"/></operation></portType><binding name="Hello_Binding" type="tns:Hello_PortType"><soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/><operation name="sayHello"><soap:operation soapAction="sayHello"/><input><soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:helloservice" use="encoded"/></input><output><soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:helloservice" use="encoded"/></output></operation></binding><service name="Hello_Service"><documentation>WSDL File for HelloService</documentation><port binding="tns:Hello_Binding" name="Hello_Port"><soap:address location="http://localhost:51515/SayHello/" /></port></service></definitions>';
var service = {
Hello_Service: {
Hello_Port: {
sayHello: function (args) {
return {
greeting: args.firstName
};
}
}
}
};
expressServer = express();
expressServer.use(bodyParser.raw({ type: function () { return true; }, limit: '5mb' }));
server = expressServer.listen(51515, function () {
var soapServer = soap.listen(expressServer, '/SayHello', service, wsdl);
url = 'http://' + server.address().address + ':' + server.address().port;
if (server.address().address === '0.0.0.0' || server.address().address === '::') {
url = 'http://127.0.0.1:' + server.address().port;
}
done();
});
});
after(function () {
server.close();
});
it('should allow parsing body via express middleware', function (done) {
request({
url: url + '/SayHello',
method: 'POST',
headers: {
SOAPAction: "sayHello",
"Content-Type": 'text/xml; charset="utf-8"'
},
body: requestXML
}, function (err, response, body) {
if (err) {
throw err;
}
assert.equal(body, responseXML);
done();
});
});
});