-
-
Notifications
You must be signed in to change notification settings - Fork 813
/
Copy pathtest.request.js
177 lines (137 loc) · 4.58 KB
/
test.request.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
169
170
171
172
173
174
175
176
177
/**
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
var tape = require( 'tape' );
var proxyquire = require( 'proxyquire' );
var string2buffer = require( '@stdlib/buffer/from-string' );
var request = require( './../lib/request.js' );
// FIXTURES //
var getOpts = require( './fixtures/opts.js' );
var http = require( './fixtures/http.js' );
var data = require( './fixtures/results.json' );
// TESTS //
tape( 'main export is a function', function test( t ) {
t.ok( true, __filename );
t.equal( typeof request, 'function', 'main export is a function' );
t.end();
});
tape( 'if unable to query an endpoint, an error is returned to a provided callback', function test( t ) {
var request;
var mock;
var opts;
opts = getOpts();
opts.protocol = 'http:';
mock = http( new Error( 'beep' ) );
request = proxyquire( './../lib/request.js', {
'http': mock
});
request( opts, string2buffer( '{"body":"beep"}' ), clbk );
function clbk( error ) {
t.equal( typeof error, 'object', 'error is an object' );
t.equal( error.status, 500, '500 status' );
t.equal( error.message, 'Request error: beep', 'message contains error message' );
t.end();
}
});
tape( 'if an endpoint returns a status code other than 201, an error containing the status code, the HTTP response object, and the response body are returned to a provided callback', function test( t ) {
var request;
var mock;
var opts;
opts = getOpts();
opts.protocol = 'http:';
mock = http( null, 404 );
request = proxyquire( './../lib/request.js', {
'http': mock
});
request( opts, string2buffer( '{"body":"beep"}' ), clbk );
function clbk( error, response, body ) {
t.equal( error.status, 404, 'equal status codes' );
t.equal( error.message, 'bad request', 'equal messages' );
t.equal( typeof response, 'object', 'second argument is an object' );
t.equal( typeof body, 'string', 'third argument is a string' );
t.end();
}
});
tape( 'if an endpoint returns an invalid JSON response, an error with a status code of 502 (bad gateway; invalid response from upstream server), the HTTP response object, and the response body are returned to a provided callback', function test( t ) {
var request;
var mock;
var opts;
opts = getOpts();
opts.protocol = 'http:';
mock = http( null, 201 );
request = proxyquire( './../lib/request.js', {
'http': mock,
'@stdlib/utils/parse-json': parse
});
request( opts, string2buffer( '{"body":"beep"}' ), clbk );
function parse() {
return new Error( 'bad json' );
}
function clbk( error, response, body ) {
t.equal( error.status, 502, 'equal status codes' );
t.equal( typeof error.message, 'string', 'error message' );
t.equal( typeof response, 'object', 'second argument is an object' );
t.equal( typeof body, 'string', 'third argument is a string' );
t.end();
}
});
tape( 'if a query is successful, a JSON object is returned to a provided callback', function test( t ) {
var request;
var mock;
var opts;
opts = getOpts();
opts.protocol = 'http:';
mock = http( null, 201 );
request = proxyquire( './../lib/request.js', {
'http': mock
});
request( opts, string2buffer( '{"body":"beep"}' ), clbk );
function clbk( error, response, body ) {
if ( error ) {
t.fail( error.message );
} else {
t.equal( typeof response, 'object', 'second argument is an object' );
t.equal( typeof body, 'object', 'returns an object' );
t.deepEqual( body, data, 'returns expected value' );
}
t.end();
}
});
tape( 'the function supports HTTPS', function test( t ) {
var request;
var mock;
var opts;
opts = getOpts();
opts.protocol = 'https:';
mock = http( null, 201 );
request = proxyquire( './../lib/request.js', {
'https': mock
});
request( opts, string2buffer( '{"body":"beep"}' ), clbk );
function clbk( error, response, body ) {
if ( error ) {
t.fail( error.message );
} else {
t.equal( typeof response, 'object', 'second argument is an object' );
t.equal( typeof body, 'object', 'returns an object' );
t.deepEqual( body, data, 'returns expected value' );
}
t.end();
}
});