forked from vpulim/node-soap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient-customHttp-test.js
More file actions
175 lines (146 loc) · 5.37 KB
/
Copy pathclient-customHttp-test.js
File metadata and controls
175 lines (146 loc) · 5.37 KB
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
'use strict';
var fs = require('fs'),
soap = require('..').soap,
http = require('http'),
assert = require('assert'),
duplexer = require('duplexer'),
req = require('request'),
httpClient = require('..').http,
// stream = require('stream'),
stream = require('readable-stream'),
util = require('util'),
events = require('events'),
semver = require('semver'),
should = require('should');
describe('custom http client', function() {
it('should be used by wsdl file loading', function(done) {
//Make a custom http agent to use streams instead on net socket
function CustomAgent(options, socket) {
var self = this;
events.EventEmitter.call(this);
self.requests = [];
self.maxSockets = 1;
self.proxyStream = socket;
self.options = options || {};
self.proxyOptions = {};
}
util.inherits(CustomAgent, events.EventEmitter);
CustomAgent.prototype.addRequest = function(req, options) {
req.onSocket(this.proxyStream);
};
//Create a duplex stream
var httpReqStream = new stream.PassThrough();
var httpResStream = new stream.PassThrough();
var socketStream = duplexer(httpReqStream, httpResStream);
// Node 4.x requires cork/uncork
socketStream.cork = function() {
};
socketStream.uncork = function() {
};
socketStream.destroy = function() {
};
//Custom httpClient
function MyHttpClient(options, socket) {
this.httpCl = new httpClient(options);
this.agent = new CustomAgent(options, socket);
}
util.inherits(MyHttpClient, httpClient);
MyHttpClient.prototype.request = function(rurl, data, callback, exheaders, exoptions) {
var self = this;
var options = self.buildRequest(rurl, data, exheaders, exoptions);
//Specify agent to use
options.agent = this.agent;
var headers = options.headers;
var req = this.httpCl._request(options, function(err, res, body) {
if (err) {
return callback(err);
}
body = self.handleResponse(req, res, body);
callback(null, res, body);
});
if (headers.Connection !== 'keep-alive') {
req.end(data);
}
util.inherits(CustomAgent, events.EventEmitter);
CustomAgent.prototype.addRequest = function(req, options) {
req.onSocket(this.proxyStream);
};
//Create a duplex stream
var httpReqStream = new stream.PassThrough();
var httpResStream = new stream.PassThrough();
var socketStream = duplexer(httpReqStream, httpResStream);
// Node 4.x requires cork/uncork
socketStream.cork = function() {
};
socketStream.uncork = function() {
};
socketStream.destroy = function() {
};
//Custom httpClient
function MyHttpClient(options, socket) {
this.httpCl = new httpClient(options);
this.agent = new CustomAgent(options, socket);
}
util.inherits(MyHttpClient, httpClient);
MyHttpClient.prototype.request =
function(rurl, data, callback, exheaders, exoptions) {
var self = this;
var options = self.buildRequest(rurl, data, exheaders, exoptions);
//Specify agent to use
options.agent = this.agent;
var headers = options.headers;
var req = this.httpCl._request(options, function(err, res, body) {
if (err) {
return callback(err);
}
body = self.handleResponse(req, res, body);
callback(null, res, body);
});
if (headers.Connection !== 'keep-alive') {
req.end(data);
}
return req;
};
};
var wsdl = fs.readFileSync('./test/wsdl/default_namespace.wsdl')
.toString('utf8');
//Should be able to read from stream the request
httpReqStream.once('readable', function readRequest() {
var chunk = httpReqStream.read();
should.exist(chunk);
//This is for compatibility with old node releases <= 0.10
//Hackish
if (semver.lt(process.version, '0.11.0')) {
socketStream.on('data', function(data) {
socketStream.ondata(data, 0, wsdl.length + 80);
});
}
//Now write the response with the wsdl
var state = httpResStream.write('HTTP/1.1 200 OK\r\nContent-Type: ' +
'text/xml; charset=utf-8\r\nContent-Length: ' + wsdl.length +
'\r\n\r\n' + wsdl);
});
var httpCustomClient = new MyHttpClient({}, socketStream);
var url = 'http://localhost:50000/Platform.asmx?wsdl';
soap.createClient(url,
{httpClient: httpCustomClient},
function(err, client) {
if (err) console.error(err);
assert.ok(!err);
assert.ok(client);
assert.equal(client.httpClient, httpCustomClient);
var description = client.describe();
var myOp = description.MyService.MyServicePort.MyOperation;
assert.equal(myOp.name, 'MyOperation');
assert.equal(myOp.style, 'documentLiteral');
assert.equal(myOp.soapAction, 'MyOperation');
var reqElement = myOp.input.body.elements[0].qname;
assert.equal(reqElement.nsURI, 'http://www.example.com/v1');
assert.equal(reqElement.name, 'Request');
var resElement = myOp.output.body.elements[0].qname;
assert.equal(resElement.nsURI, 'http://www.example.com/v1');
assert.equal(resElement.name, 'Response');
done();
});
});
});