@platform/http
Version:
Tools for working with HTTP.
131 lines (130 loc) • 6.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var test_1 = require("../test");
var __1 = require("..");
var crypto_1 = tslib_1.__importDefault(require("crypto"));
var TMP = test_1.fs.resolve('./tmp');
var URL = {
IMAGE: 'https://platform.sfo2.digitaloceanspaces.com/modules/http/image.png',
ZIP: 'https://platform.sfo2.digitaloceanspaces.com/modules/http/images.zip',
JSON: 'https://platform.sfo2.digitaloceanspaces.com/modules/http/foo.json',
TEXT: 'https://platform.sfo2.digitaloceanspaces.com/modules/http/file.txt',
};
describe('http (INTEGRATION)', function () {
var _this = this;
this.timeout(30000);
it('GET (text/plain)', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, __1.http.get(URL.TEXT)];
case 1:
res = _a.sent();
(0, test_1.expect)(res.status).to.eql(200);
(0, test_1.expect)(res.text).to.include("Hello \uD83D\uDC37");
(0, test_1.expect)(res.contentType.mime).to.eql('text/plain');
(0, test_1.expect)(res.contentType.is.text).to.eql(true);
(0, test_1.expect)(res.contentType.is.json).to.eql(false);
(0, test_1.expect)(res.contentType.is.binary).to.eql(false);
return [2];
}
});
}); });
it('GET (application/json)', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, __1.http.get(URL.JSON)];
case 1:
res = _a.sent();
(0, test_1.expect)(res.status).to.eql(200);
(0, test_1.expect)(res.json.name).to.eql('foo');
(0, test_1.expect)(res.contentType.mime).to.eql('application/json');
(0, test_1.expect)(res.contentType.is.text).to.eql(false);
(0, test_1.expect)(res.contentType.is.json).to.eql(true);
(0, test_1.expect)(res.contentType.is.binary).to.eql(false);
return [2];
}
});
}); });
it('GET download using http.get (and fs.stream)', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var res, path;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, __1.http.get(URL.ZIP)];
case 1:
res = _a.sent();
if (!res.body) return [3, 3];
path = test_1.fs.join(TMP, 'images-2.zip');
return [4, test_1.fs.stream.save(path, res.body)];
case 2:
_a.sent();
_a.label = 3;
case 3:
(0, test_1.expect)(res.contentType.mime).to.eql('application/zip');
(0, test_1.expect)(res.contentType.is.text).to.eql(false);
(0, test_1.expect)(res.contentType.is.json).to.eql(false);
(0, test_1.expect)(res.contentType.is.binary).to.eql(true);
return [2];
}
});
}); });
describe('POST file (SHA1 checksum)', function () {
var _a;
var token = (_a = process.env.VERCEL_TEST_TOKEN) !== null && _a !== void 0 ? _a : '';
var url = 'https://api.vercel.com/v2/now/files';
it('binary (Buffer)', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var path, file, hash, headers, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
path = test_1.fs.resolve('src/test/assets/kitten.jpg');
return [4, test_1.fs.readFile(path)];
case 1:
file = _a.sent();
hash = shasum(file);
headers = {
Authorization: "Bearer ".concat(token),
'x-vercel-digest': hash,
'Content-Length': file.byteLength.toString(),
'Content-Type': 'application/octet-stream',
};
return [4, __1.http.post(url, file, { headers: headers })];
case 2:
res = _a.sent();
(0, test_1.expect)(res.status).to.eql(200);
return [2];
}
});
}); });
it('binary (Uint8Array)', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var path, file, _a, _b, hash, headers, res;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
path = test_1.fs.resolve('src/test/assets/kitten.jpg');
_b = (_a = Uint8Array).from;
return [4, test_1.fs.readFile(path)];
case 1:
file = _b.apply(_a, [_c.sent()]);
hash = shasum(file);
headers = {
Authorization: "Bearer ".concat(token),
'x-vercel-digest': hash,
'Content-Length': file.byteLength.toString(),
'Content-Type': 'application/octet-stream',
};
return [4, __1.http.post(url, file, { headers: headers })];
case 2:
res = _c.sent();
(0, test_1.expect)(res.status).to.eql(200);
return [2];
}
});
}); });
});
});
function shasum(file) {
return crypto_1.default.createHash('sha1').update(file).digest('hex');
}