nodeunit-api-client 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/httpclient.js +64 -28
package/package.json
CHANGED
package/src/httpclient.js
CHANGED
|
@@ -3,7 +3,6 @@ var querystring = require('querystring'),
|
|
|
3
3
|
underscore = require('underscore'),
|
|
4
4
|
debug;
|
|
5
5
|
|
|
6
|
-
|
|
7
6
|
/**
|
|
8
7
|
* @param {object} Options:
|
|
9
8
|
* auth ('username:password')
|
|
@@ -12,7 +11,7 @@ var querystring = require('querystring'),
|
|
|
12
11
|
* path ('') - Base path URL e.g. '/api'
|
|
13
12
|
* headers ({}) - Test that these headers are present on every response (unless overridden)
|
|
14
13
|
* status (null) - Test that every response has this status (unless overridden)
|
|
15
|
-
*
|
|
14
|
+
* https (false) - https/http
|
|
16
15
|
* @param options
|
|
17
16
|
*/
|
|
18
17
|
var HttpClient = module.exports = function(options) {
|
|
@@ -20,11 +19,12 @@ var HttpClient = module.exports = function(options) {
|
|
|
20
19
|
|
|
21
20
|
this.auth = options.auth || undefined;
|
|
22
21
|
this.host = options.host || 'localhost';
|
|
23
|
-
this.port = options.port || 80;
|
|
22
|
+
this.port = options.port || (options.https ? 443 : 80);
|
|
24
23
|
this.path = options.path || '';
|
|
25
24
|
this.headers = options.headers || {};
|
|
26
25
|
this.status = options.status;
|
|
27
|
-
this.
|
|
26
|
+
this.https = options.https || false;
|
|
27
|
+
this.http = require(this.https ? 'https' : 'http');
|
|
28
28
|
debug = options.debug ? true : false;
|
|
29
29
|
};
|
|
30
30
|
|
|
@@ -60,7 +60,6 @@ methods.forEach(function(method) {
|
|
|
60
60
|
req = {};
|
|
61
61
|
res = {};
|
|
62
62
|
}
|
|
63
|
-
|
|
64
63
|
//(assert, path, res)
|
|
65
64
|
else {
|
|
66
65
|
cb = null;
|
|
@@ -87,8 +86,9 @@ methods.forEach(function(method) {
|
|
|
87
86
|
//Don't add to querystring if POST or PUT
|
|
88
87
|
if (['post', 'put'].indexOf(method) === -1) {
|
|
89
88
|
var data = req.data;
|
|
90
|
-
|
|
91
|
-
|
|
89
|
+
if (data) {
|
|
90
|
+
fullPath += '?' + querystring.stringify(data);
|
|
91
|
+
}
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
var options = {
|
|
@@ -96,7 +96,8 @@ methods.forEach(function(method) {
|
|
|
96
96
|
port: this.port,
|
|
97
97
|
path: fullPath,
|
|
98
98
|
method: method == 'del' ? 'DELETE' : method.toUpperCase(),
|
|
99
|
-
headers: underscore.extend({}, this.headers, req.headers)
|
|
99
|
+
headers: underscore.extend({}, this.headers, req.headers || {}),
|
|
100
|
+
rejectUnauthorized: false // Pour éviter les erreurs de certificat SSL en dev
|
|
100
101
|
};
|
|
101
102
|
|
|
102
103
|
if (req.auth) {
|
|
@@ -107,31 +108,59 @@ methods.forEach(function(method) {
|
|
|
107
108
|
|
|
108
109
|
var request = this.http.request(options);
|
|
109
110
|
|
|
110
|
-
//Write POST &
|
|
111
|
+
//Write POST & PUT data
|
|
111
112
|
if (['post', 'put'].indexOf(method) != -1) {
|
|
112
113
|
var data = req.data || req.body;
|
|
113
114
|
|
|
114
115
|
if (data) {
|
|
115
116
|
if (typeof data == 'object') {
|
|
116
|
-
|
|
117
|
-
|
|
117
|
+
options.headers['content-type'] = 'application/json';
|
|
118
|
+
var jsonData = JSON.stringify(data);
|
|
119
|
+
options.headers['content-length'] = Buffer.byteLength(jsonData);
|
|
120
|
+
request.write(jsonData);
|
|
118
121
|
} else {
|
|
122
|
+
options.headers['content-length'] = Buffer.byteLength(data);
|
|
119
123
|
request.write(data);
|
|
120
124
|
}
|
|
121
125
|
}
|
|
122
126
|
}
|
|
123
127
|
|
|
124
|
-
if (debug) {
|
|
128
|
+
if (debug) {
|
|
129
|
+
httpClientLogger.log('REQUEST', {
|
|
130
|
+
method: options.method,
|
|
131
|
+
host: options.host,
|
|
132
|
+
port: options.port,
|
|
133
|
+
path: options.path,
|
|
134
|
+
headers: options.headers
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
125
138
|
//Send
|
|
126
139
|
request.end();
|
|
127
140
|
|
|
141
|
+
request.on('error', function(err) {
|
|
142
|
+
console.error('Request error:', err);
|
|
143
|
+
if (cb) {
|
|
144
|
+
return cb(null, err);
|
|
145
|
+
} else if (assert) {
|
|
146
|
+
assert.ok(false, 'Request failed: ' + err.message);
|
|
147
|
+
return assert.done();
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
|
|
128
151
|
request.on('response', function(response) {
|
|
129
|
-
if (debug) {
|
|
152
|
+
if (debug) {
|
|
153
|
+
httpClientLogger.log('RESPONSE', {
|
|
154
|
+
statusCode: response.statusCode,
|
|
155
|
+
headers: response.headers
|
|
156
|
+
});
|
|
157
|
+
}
|
|
130
158
|
|
|
131
159
|
response.setEncoding('utf8');
|
|
160
|
+
response.body = '';
|
|
132
161
|
|
|
133
162
|
response.on('data', function(chunk) {
|
|
134
|
-
|
|
163
|
+
response.body += chunk;
|
|
135
164
|
});
|
|
136
165
|
|
|
137
166
|
//Handle the response; run response tests and hand back control to test
|
|
@@ -139,24 +168,28 @@ methods.forEach(function(method) {
|
|
|
139
168
|
//Add parsed JSON
|
|
140
169
|
var contentType = response.headers['content-type'];
|
|
141
170
|
if (contentType && contentType.indexOf('application/json') != -1) {
|
|
142
|
-
if (
|
|
171
|
+
if (response.body) {
|
|
143
172
|
//Catch errors on JSON.parse and attempt to handle cases where the response.body contains html
|
|
144
173
|
try {
|
|
145
174
|
response.data = JSON.parse(response.body);
|
|
146
175
|
} catch (err) {
|
|
147
176
|
console.log('JSON.parse response.body error:');
|
|
148
177
|
console.log(err);
|
|
149
|
-
if (debug) {
|
|
178
|
+
if (debug) {
|
|
179
|
+
httpClientLogger.log('RESPONSE.BODY', response.body);
|
|
180
|
+
}
|
|
150
181
|
var responseTest = response.body.split('{');
|
|
151
182
|
if (responseTest.length > 1) {
|
|
152
183
|
var actualResponse = '{' + responseTest[1];
|
|
153
184
|
try {
|
|
154
185
|
response.data = JSON.parse(actualResponse);
|
|
155
186
|
console.log('JSON.parse second attempt success.');
|
|
156
|
-
} catch (
|
|
187
|
+
} catch (err2) {
|
|
157
188
|
console.log('JSON.parse error on second parse attempt.');
|
|
158
|
-
console.log(
|
|
159
|
-
if (debug) {
|
|
189
|
+
console.log(err2);
|
|
190
|
+
if (debug) {
|
|
191
|
+
httpClientLogger.log('FILTERED RESPONSE.BODY', actualResponse);
|
|
192
|
+
}
|
|
160
193
|
}
|
|
161
194
|
}
|
|
162
195
|
}
|
|
@@ -171,29 +204,32 @@ methods.forEach(function(method) {
|
|
|
171
204
|
//Status code
|
|
172
205
|
var status = res.status || self.status;
|
|
173
206
|
if (status) {
|
|
174
|
-
assert.equal(response.statusCode, status);
|
|
207
|
+
assert.equal(response.statusCode, status, 'Status code mismatch');
|
|
175
208
|
}
|
|
176
209
|
|
|
177
210
|
//Headers
|
|
178
211
|
var headers = underscore.extend({}, self.headers, res.headers);
|
|
179
212
|
for (var key in headers) {
|
|
180
|
-
assert.equal(response.headers[key], headers[key]);
|
|
213
|
+
assert.equal(response.headers[key], headers[key], 'Header mismatch: ' + key);
|
|
181
214
|
}
|
|
182
215
|
|
|
183
216
|
//Body
|
|
184
217
|
if (res.body) {
|
|
185
|
-
assert.equal(response.body, res.body);
|
|
218
|
+
assert.equal(response.body, res.body, 'Body mismatch');
|
|
186
219
|
}
|
|
187
220
|
|
|
188
221
|
//JSON data
|
|
189
222
|
if (res.data) {
|
|
190
|
-
assert.deepEqual(response.data, res.data);
|
|
223
|
+
assert.deepEqual(response.data, res.data, 'Data mismatch');
|
|
191
224
|
}
|
|
192
225
|
})();
|
|
193
226
|
|
|
194
|
-
|
|
195
227
|
//Done, return control to test
|
|
196
|
-
if (cb) {
|
|
228
|
+
if (cb) {
|
|
229
|
+
return cb(response);
|
|
230
|
+
} else if (assert) {
|
|
231
|
+
return assert.done();
|
|
232
|
+
}
|
|
197
233
|
});
|
|
198
234
|
});
|
|
199
235
|
};
|
|
@@ -201,7 +237,7 @@ methods.forEach(function(method) {
|
|
|
201
237
|
|
|
202
238
|
var httpClientLogger = {
|
|
203
239
|
log: function(header, data) {
|
|
204
|
-
console.log(header);
|
|
205
|
-
console.log(data);
|
|
240
|
+
console.log('=== ' + header + ' ===');
|
|
241
|
+
console.log(JSON.stringify(data, null, 2));
|
|
206
242
|
}
|
|
207
|
-
};
|
|
243
|
+
};
|