forked from jnyryan/node-soap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient-customHttp-test.js
117 lines (102 loc) · 3.34 KB
/
client-customHttp-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
'use strict';
var fs = require('fs'),
soap = require('..'),
http = require('http'),
assert = require('assert'),
duplexer = require('duplexer'),
req = require('request'),
httpClient = require('../lib/http.js'),
// stream = require('stream'),
stream = require('readable-stream'),
util = require('util'),
events = require('events'),
semver = require('semver'),
should = require('should');
it('should allow customization of httpClient and the wsdl file download should pass through it', 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() {
};
//Custom httpClient
function MyHttpClient (options, socket){
httpClient.call(this,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 = self._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,1984);
});
}
//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: 1904\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) {
assert.ok(client);
assert.ok(!err);
assert.equal(client.httpClient, httpCustomClient);
var description = (client.describe());
assert.deepEqual(client.describe(), {
MyService: {
MyServicePort: {
MyOperation: {
input: {
},
output: {
}
}
}
}
});
done();
});
});