filestack-js
Version:
Official JavaScript library for Filestack
954 lines • 114 kB
JavaScript
/*
* Copyright (c) 2018 by Filestack
* Some rights reserved.
*
* 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.
*/
import { __awaiter, __generator } from "tslib";
/* istanbul ignore file */
import nock from 'nock';
import * as zlib from 'zlib';
import { FsHttpMethod } from '../types';
import { FsCancelToken } from '../token';
import { FsRequestError, FsRequestErrorCode } from '../error';
export var adaptersHttpAbstract = function (adapter, adapterName) {
describe("Request/Adapters/".concat(adapterName), function () {
var scope;
var url = 'https://somewrongdom.moc';
beforeEach(function () {
nock.cleanAll();
scope = null;
});
beforeEach(function () {
scope = nock(url).defaultReplyHeaders({
'access-control-allow-origin': function (req) { var _a; return (_a = req.getHeader('origin')) === null || _a === void 0 ? void 0 : _a.toString(); },
'access-control-allow-methods': function (req) { var _a; return (_a = req.getHeader('access-control-request-method')) === null || _a === void 0 ? void 0 : _a.toString(); },
'access-control-allow-headers': function (req) { var _a; return (_a = req.getHeader('access-control-request-headers')) === null || _a === void 0 ? void 0 : _a.toString(); },
});
if (adapterName === 'xhr') {
scope.options(/.*/).reply(200);
}
});
describe('request basic', function () {
it('should make correct request (https)', function () { return __awaiter(void 0, void 0, void 0, function () {
var options, requestAdapter, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = {
url: url,
method: FsHttpMethod.GET,
};
scope.get('/').reply(200, 'ok', { 'Content-Type': 'text/plain' });
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 1:
res = _a.sent();
expect(res.status).toEqual(200);
expect(res.data).toEqual('ok');
scope.done();
return [2 /*return*/];
}
});
}); });
it('should make correct request (http)', function () { return __awaiter(void 0, void 0, void 0, function () {
var httpUrl, scopeHttp, options, requestAdapter, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
httpUrl = url.replace('https', 'http');
scopeHttp = nock(httpUrl).defaultReplyHeaders({
'access-control-allow-origin': function (req) { var _a; return (_a = req.getHeader('origin')) === null || _a === void 0 ? void 0 : _a.toString(); },
'access-control-allow-methods': function (req) { var _a; return (_a = req.getHeader('access-control-request-method')) === null || _a === void 0 ? void 0 : _a.toString(); },
'access-control-allow-headers': function (req) { var _a; return (_a = req.getHeader('access-control-request-headers')) === null || _a === void 0 ? void 0 : _a.toString(); },
});
if (adapterName === 'xhr') {
scopeHttp.options(/.*/).reply(200);
}
options = {
url: httpUrl,
method: FsHttpMethod.GET,
};
scopeHttp.get('/').reply(200, 'ok', { 'Content-Type': 'text/plain' });
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 1:
res = _a.sent();
expect(res.status).toEqual(200);
expect(res.data).toEqual('ok');
scopeHttp.done();
return [2 /*return*/];
}
});
}); });
it('should add https protocol if no protocol is provided', function () { return __awaiter(void 0, void 0, void 0, function () {
var options, requestAdapter, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = {
url: url.replace('https://', ''),
method: FsHttpMethod.GET,
};
scope.get('/').reply(200, 'ok', { 'Content-Type': 'text/plain' });
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 1:
res = _a.sent();
expect(res.status).toEqual(200);
expect(res.data).toEqual('ok');
scope.done();
return [2 /*return*/];
}
});
}); });
it('should handle string as data param', function () { return __awaiter(void 0, void 0, void 0, function () {
var msg, mock, options, requestAdapter, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
msg = 'Some test stream data';
mock = jest
.fn()
.mockName('bufferData')
.mockReturnValue(msg);
options = {
url: url,
method: FsHttpMethod.POST,
data: msg,
};
scope.post('/').reply(200, function (_, data) {
return mock(data);
}, { 'Content-Type': 'text/plain' });
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 1:
res = _a.sent();
expect(res.status).toEqual(200);
expect(res.data).toEqual(msg);
expect(mock).toHaveBeenLastCalledWith(msg);
scope.done();
return [2 /*return*/];
}
});
}); });
// gzip support is handled by the browser on xhr side
if (adapterName !== 'xhr') {
it('should handle deflate response', function () { return __awaiter(void 0, void 0, void 0, function () {
var options, data, requestAdapter, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = {
url: url,
method: FsHttpMethod.GET,
};
data = zlib.gzipSync(Buffer.from('ok', 'utf-8'));
scope.get('/').reply(200, data, { 'Content-encoding': 'gzip, deflate', 'Content-type': 'text/plain' });
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 1:
res = _a.sent();
expect(res.status).toEqual(200);
expect(res.data).toEqual('ok');
scope.done();
return [2 /*return*/];
}
});
}); });
it('should handle 204 gzip request', function () { return __awaiter(void 0, void 0, void 0, function () {
var options, requestAdapter, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = {
url: url,
method: FsHttpMethod.GET,
};
scope.get('/').reply(204, '', { 'Content-encoding': 'gzip, deflate', 'Content-type': 'text/plain' });
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 1:
res = _a.sent();
expect(res.status).toEqual(204);
expect(res.data).toEqual(null);
scope.done();
return [2 /*return*/];
}
});
}); });
it('should handle Buffer as data param', function () { return __awaiter(void 0, void 0, void 0, function () {
var msg, mock, options, requestAdapter, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
msg = 'Some test stream data';
mock = jest
.fn()
.mockName('bufferData')
.mockReturnValue('ok');
options = {
url: url,
method: FsHttpMethod.POST,
data: Buffer.from(msg, 'utf-8'),
};
scope.post('/').reply(200, function (_, data) {
return mock(data);
}, { 'Content-type': 'text/plain' });
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 1:
res = _a.sent();
expect(res.status).toEqual(200);
expect(mock).toHaveBeenLastCalledWith(msg);
scope.done();
return [2 /*return*/];
}
});
}); });
it('should throw error when data type is unsupported', function () {
var Readable = require('stream').Readable;
var options = {
url: url,
method: FsHttpMethod.POST,
data: Readable.from(['test']),
};
var requestAdapter = new adapter();
return expect(requestAdapter.request(options)).rejects.toEqual(expect.any(FsRequestError));
});
}
it('should make request with auth', function () { return __awaiter(void 0, void 0, void 0, function () {
var auth, options, requestAdapter, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
auth = {
username: 'test',
password: 'test',
};
options = {
url: url,
method: FsHttpMethod.GET,
auth: auth,
};
scope.options('/').reply(200, 'ok');
scope
.get('/')
.basicAuth({ user: auth.username, pass: auth.password })
.reply(200, 'ok', { 'access-control-allow-origin': '*' });
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 1:
res = _a.sent();
expect(res.status).toEqual(200);
return [2 /*return*/];
}
});
}); });
it('should throw an error on empty username', function () { return __awaiter(void 0, void 0, void 0, function () {
var auth, options, requestAdapter;
return __generator(this, function (_a) {
auth = {
username: null,
password: 'test',
};
options = {
url: url,
method: FsHttpMethod.GET,
auth: auth,
};
requestAdapter = new adapter();
return [2 /*return*/, expect(requestAdapter.request(options)).rejects.toEqual(expect.any(FsRequestError))];
});
}); });
it('should throw an error on empty password', function () { return __awaiter(void 0, void 0, void 0, function () {
var auth, options, requestAdapter;
return __generator(this, function (_a) {
auth = {
username: 'test',
password: null,
};
options = {
url: url,
method: FsHttpMethod.GET,
auth: auth,
};
requestAdapter = new adapter();
return [2 /*return*/, expect(requestAdapter.request(options)).rejects.toEqual(expect.any(FsRequestError))];
});
}); });
it('should overwrite auth header if auth data is provided', function () { return __awaiter(void 0, void 0, void 0, function () {
var auth, options, requestAdapter, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
auth = {
username: 'test',
password: 'test',
};
options = {
url: url,
method: FsHttpMethod.GET,
auth: auth,
headers: {
Authorization: 'test123',
},
};
scope.options('/').reply(200, 'ok');
scope
.get('/')
.basicAuth({ user: auth.username, pass: auth.password })
.reply(200, 'ok');
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 1:
res = _a.sent();
expect(res.status).toEqual(200);
return [2 /*return*/];
}
});
}); });
it('should contain default headers', function () { return __awaiter(void 0, void 0, void 0, function () {
var mock, options, requestAdapter, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
mock = jest
.fn()
.mockName('default/headers')
.mockReturnValue('ok');
options = {
url: url,
method: FsHttpMethod.GET,
};
scope.get('/').reply(200, function (_, data) {
return mock(this.req.headers);
});
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 1:
res = _a.sent();
expect(res.status).toEqual(200);
expect(mock).toHaveBeenCalledWith(expect.objectContaining({ 'filestack-source': expect.any(String), 'filestack-trace-id': expect.any(String), 'filestack-trace-span': expect.any(String) }));
return [2 /*return*/];
}
});
}); });
it('should omit default headers', function () { return __awaiter(void 0, void 0, void 0, function () {
var mock, options, requestAdapter, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
mock = jest
.fn()
.mockName('default/headers')
.mockReturnValue('ok');
options = {
url: url,
method: FsHttpMethod.GET,
filestackHeaders: false,
};
scope.get('/').reply(200, function (_, data) {
return mock(this.req.headers);
});
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 1:
res = _a.sent();
expect(res.status).toEqual(200);
expect(mock).toHaveBeenCalledWith(expect.not.objectContaining({ 'filestack-source': expect.any(String), 'filestack-trace-id': expect.any(String), 'filestack-trace-span': expect.any(String) }));
return [2 /*return*/];
}
});
}); });
it('should skip undefined headers', function () { return __awaiter(void 0, void 0, void 0, function () {
var mock, options, requestAdapter, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
mock = jest
.fn()
.mockName('undefined/headers')
.mockReturnValue('ok');
options = {
url: url,
method: FsHttpMethod.GET,
headers: {
test: undefined,
},
};
scope.get('/').reply(200, function (_, data) {
return mock(this.req.headers);
});
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 1:
res = _a.sent();
expect(res.status).toEqual(200);
expect(mock).toHaveBeenCalledWith(expect.not.objectContaining({ test: undefined }));
scope.done();
return [2 /*return*/];
}
});
}); });
});
describe('redirects ', function () {
// xhr redirects are handled by the browser
if (adapterName !== 'xhr') {
it('should follow 302 redirect', function () { return __awaiter(void 0, void 0, void 0, function () {
var options, response, requestAdapter, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = {
url: url,
method: FsHttpMethod.GET,
};
response = { test: 123 };
scope.get('/').reply(302, 'ok', {
location: "".concat(url, "/resp"),
});
scope.get('/resp').reply(200, response, {
'Content-type': 'application/json',
});
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 1:
res = _a.sent();
expect(res.status).toEqual(200);
expect(res.data).toEqual(response);
scope.done();
return [2 /*return*/];
}
});
}); });
it('should throw error when no location is provided', function () { return __awaiter(void 0, void 0, void 0, function () {
var options, requestAdapter, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = {
url: url,
method: FsHttpMethod.GET,
};
scope.get('/').reply(302, 'ok', { location: '' });
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 2:
_a.sent();
// return error in try will not emit error
expect(false).toEqual(true);
return [3 /*break*/, 4];
case 3:
err_1 = _a.sent();
expect(err_1).toEqual(expect.any(FsRequestError));
expect(err_1.code).toEqual(FsRequestErrorCode.REDIRECT);
return [3 /*break*/, 4];
case 4:
scope.done();
return [2 /*return*/];
}
});
}); });
it('should throw error (REDIRECT) on max redirects', function () { return __awaiter(void 0, void 0, void 0, function () {
var options, requestAdapter, err_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = {
url: url,
method: FsHttpMethod.GET,
};
if (adapterName === 'xhr') {
scope.options('/').reply(200, 'ok');
}
scope
.get('/')
.reply(302, 'ok', {
location: "".concat(url, "/a"),
})
.get('/a')
.reply(301, 'ok', {
location: "".concat(url, "/b"),
})
.get('/b')
.reply(302, 'ok', {
location: "".concat(url, "/c"),
})
.get('/c')
.reply(301, 'ok', {
location: "".concat(url, "/d"),
})
.get('/d')
.reply(302, 'ok', {
location: "".concat(url, "/e"),
})
.get('/e')
.reply(301, 'ok', {
location: "".concat(url, "/f"),
})
.get('/f')
.reply(302, 'ok', {
location: "".concat(url, "/g"),
})
.get('/g')
.reply(301, 'ok', {
location: "".concat(url, "/h"),
})
.get('/h')
.reply(302, 'ok', {
location: "".concat(url, "/i"),
})
.get('/i')
.reply(301, 'ok', {
location: "".concat(url, "/j"),
})
.get('/j')
.reply(302, 'ok', {
location: "".concat(url, "/k"),
});
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 2:
_a.sent();
// return error in try will not emit error
expect(false).toEqual(true);
return [3 /*break*/, 4];
case 3:
err_2 = _a.sent();
expect(err_2).toEqual(expect.any(FsRequestError));
expect(err_2.code).toEqual(FsRequestErrorCode.REDIRECT);
return [3 /*break*/, 4];
case 4:
scope.done();
return [2 /*return*/];
}
});
}); });
it('should throw error on redirect loop', function () { return __awaiter(void 0, void 0, void 0, function () {
var options, requestAdapter, err_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = {
url: url,
method: FsHttpMethod.GET,
};
scope
.get('/')
.reply(302, 'ok', { location: "".concat(url, "/a") })
.get('/a')
.reply(302, 'ok', { location: "".concat(url, "/a") })
.get('/a')
.reply(200, 'ok');
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 2:
_a.sent();
// return error in try will not emit error
expect(false).toEqual(true);
return [3 /*break*/, 4];
case 3:
err_3 = _a.sent();
expect(err_3).toEqual(expect.any(FsRequestError));
expect(err_3.code).toEqual(FsRequestErrorCode.REDIRECT);
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); });
}
});
if (adapterName === 'xhr') {
describe('request form', function () {
it('Should send form data', function () { return __awaiter(void 0, void 0, void 0, function () {
var form, options, resp, requestAdapter, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
form = new FormData();
options = {
url: url,
method: FsHttpMethod.POST,
data: form,
};
resp = { form: 'ok' };
scope.post('/').reply(200, resp, { 'Content-type': 'application/json' });
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 1:
res = _a.sent();
expect(res.status).toEqual(200);
expect(res.data).toEqual(resp);
scope.done();
return [2 /*return*/];
}
});
}); });
});
}
describe('4xx and 5xx errors handling', function () {
it('should handle 4xx response', function () { return __awaiter(void 0, void 0, void 0, function () {
var options, errorResp, requestAdapter, err_4;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = {
url: url,
method: FsHttpMethod.GET,
};
errorResp = { test: 123 };
scope.get('/').reply(404, errorResp, {
'Content-type': 'application/json',
});
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 2:
_a.sent();
expect(false).toEqual(true);
return [3 /*break*/, 4];
case 3:
err_4 = _a.sent();
expect(err_4).toEqual(expect.any(FsRequestError));
expect(err_4.code).toEqual(FsRequestErrorCode.REQUEST);
expect(err_4.response.status).toEqual(404);
expect(err_4.response.data).toEqual(errorResp);
return [3 /*break*/, 4];
case 4:
scope.done();
return [2 /*return*/];
}
});
}); });
it('should handle 5xx response', function () { return __awaiter(void 0, void 0, void 0, function () {
var options, errorResp, requestAdapter, err_5;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = {
url: url,
method: FsHttpMethod.GET,
};
errorResp = { test: 123 };
scope.get('/').reply(501, errorResp, {
'Content-type': 'application/json',
});
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 2:
_a.sent();
expect(false).toEqual(true);
return [3 /*break*/, 4];
case 3:
err_5 = _a.sent();
expect(err_5).toEqual(expect.any(FsRequestError));
expect(err_5.code).toEqual(FsRequestErrorCode.SERVER);
expect(err_5.response.status).toEqual(501);
expect(err_5.response.data).toEqual(errorResp);
return [3 /*break*/, 4];
case 4:
scope.done();
return [2 /*return*/];
}
});
}); });
});
if (adapterName === 'xhr') {
describe('progress event', function () {
it('should handle upload progress', function () { return __awaiter(void 0, void 0, void 0, function () {
var progressSpy, buf, options, requestAdapter, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
progressSpy = jest
.fn()
.mockName('bufferData')
.mockReturnThis();
buf = Buffer.alloc(1024);
buf.fill('a');
options = {
url: "".concat(url, "/progress"),
method: FsHttpMethod.POST,
onProgress: progressSpy,
data: buf,
};
scope.options('/progress').reply(200, 'ok', {
'Content-type': 'application/json',
});
scope.post('/progress').reply(200, 'ok', {
'Content-type': 'application/json',
});
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 1:
res = _a.sent();
// for jsdom we cannot check progress event correctly
expect(progressSpy).toHaveBeenCalled();
return [2 /*return*/];
}
});
}); });
});
}
describe('cancelToken', function () {
it('Should throw abort request when token will be called', function () { return __awaiter(void 0, void 0, void 0, function () {
var token, options, requestAdapter, err_6;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
token = new FsCancelToken();
options = {
url: "".concat(url, "/timeout"),
method: FsHttpMethod.GET,
cancelToken: token,
};
scope
.get('/timeout')
.delay(2000)
.reply(201, 'ok', {
'content-type': 'application/json',
});
setTimeout(function () {
token.cancel();
}, 150);
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 2:
_a.sent();
// return error in try will not emit error
expect(false).toEqual(true);
return [3 /*break*/, 4];
case 3:
err_6 = _a.sent();
expect(err_6).toEqual(expect.any(FsRequestError));
expect(err_6.code).toEqual(FsRequestErrorCode.ABORTED);
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); });
it('Should not throw undefined error when cancel token will be called after request finished', function () { return __awaiter(void 0, void 0, void 0, function () {
var token, options, requestAdapter, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
token = new FsCancelToken();
options = {
url: url,
method: FsHttpMethod.GET,
cancelToken: token,
};
scope.get('/').reply(200, 'ok', {
'Content-type': 'text/plain',
});
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 1:
res = _a.sent();
token.cancel();
expect(res.status).toEqual(200);
expect(res.data).toEqual('ok');
scope.done();
return [2 /*return*/];
}
});
}); });
});
describe('Network errors', function () {
it('should throw an error on domain not found', function () { return __awaiter(void 0, void 0, void 0, function () {
var options, requestAdapter;
return __generator(this, function (_a) {
options = {
url: 'https://some-badd-url.er',
method: FsHttpMethod.GET,
};
requestAdapter = new adapter();
return [2 /*return*/, expect(requestAdapter.request(options)).rejects.toEqual(expect.any(FsRequestError))];
});
}); });
it.skip('Should throw an FilestackError on socket abort with FsRequestErrorCode.TIMEOUTED code', function () { return __awaiter(void 0, void 0, void 0, function () {
var options, requestAdapter, err_7;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = {
url: url,
method: FsHttpMethod.GET,
timeout: 50,
};
scope
.get('/')
.delay(2000)
.reply(200, 'ok', {
'Content-type': 'application/json',
});
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 2:
_a.sent();
// return error in try will not emit error
expect(false).toEqual(true);
return [3 /*break*/, 4];
case 3:
err_7 = _a.sent();
expect(err_7).toEqual(expect.any(FsRequestError));
expect(err_7.code).toEqual(FsRequestErrorCode.TIMEOUT);
return [3 /*break*/, 4];
case 4:
scope.done();
return [2 /*return*/];
}
});
}); });
it('Should throw an FilestackError on response ECONNREFUSED error with FsRequestErrorCode.NETWORK code', function () { return __awaiter(void 0, void 0, void 0, function () {
var options, requestAdapter, err_8;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = {
url: url,
method: FsHttpMethod.GET,
};
scope.get('/').replyWithError({ code: 'ECONNREFUSED' });
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 2:
_a.sent();
// return error in try will not emit error
expect(false).toEqual(true);
return [3 /*break*/, 4];
case 3:
err_8 = _a.sent();
expect(err_8).toEqual(expect.any(FsRequestError));
expect(err_8.code).toEqual(FsRequestErrorCode.NETWORK);
return [3 /*break*/, 4];
case 4:
scope.done();
return [2 /*return*/];
}
});
}); });
it('Should throw an FilestackError on response ECONNRESET error with FsRequestErrorCode.NETWORK code', function () { return __awaiter(void 0, void 0, void 0, function () {
var options, requestAdapter, err_9;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = {
url: url,
method: FsHttpMethod.GET,
};
scope.get('/').replyWithError({ code: 'ECONNRESET' });
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 2:
_a.sent();
// return error in try will not emit error
expect(false).toEqual(true);
return [3 /*break*/, 4];
case 3:
err_9 = _a.sent();
expect(err_9).toEqual(expect.any(FsRequestError));
expect(err_9.code).toEqual(FsRequestErrorCode.NETWORK);
return [3 /*break*/, 4];
case 4:
scope.done();
return [2 /*return*/];
}
});
}); });
it('Should throw an FilestackError on response ENOTFOUND error with FsRequestErrorCode.NETWORK code', function () { return __awaiter(void 0, void 0, void 0, function () {
var options, requestAdapter, err_10;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = {
url: url,
method: FsHttpMethod.GET,
};
scope.get('/').replyWithError({ code: 'ENOTFOUND' });
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 2:
_a.sent();
// return error in try will not emit error
expect(false).toEqual(true);
return [3 /*break*/, 4];
case 3:
err_10 = _a.sent();
expect(err_10).toEqual(expect.any(FsRequestError));
expect(err_10.code).toEqual(FsRequestErrorCode.NETWORK);
return [3 /*break*/, 4];
case 4:
scope.done();
return [2 /*return*/];
}
});
}); });
it.skip('Should abort request on timeout', function () { return __awaiter(void 0, void 0, void 0, function () {
var options, requestAdapter, err_11;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = {
url: url,
method: FsHttpMethod.GET,
timeout: 1000,
};
scope
.get('/')
.delay(2000)
.reply(200, 'ok');
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
requestAdapter = new adapter();
return [4 /*yield*/, requestAdapter.request(options)];
case 2:
_a.sent();
// return error in try will not emit error
expect(false).toEqual(true);
return [3 /*break*/, 4];
case 3:
err_11 = _a.sent();
expect(err_11).toEqual(expect.any(FsRequestError));
expect(err_11.code).toEqual(F