@platform/http
Version:
Tools for working with HTTP.
739 lines (738 loc) • 40.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var stream_1 = require("stream");
var http_1 = require("http");
var __1 = require("..");
var test_1 = require("../test");
describe('http', function () {
describe('default instance (singleton)', function () {
it('has methods', function () {
(0, test_1.expect)(__1.http.head).to.be.an.instanceof(Function);
(0, test_1.expect)(__1.http.get).to.be.an.instanceof(Function);
(0, test_1.expect)(__1.http.put).to.be.an.instanceof(Function);
(0, test_1.expect)(__1.http.post).to.be.an.instanceof(Function);
(0, test_1.expect)(__1.http.patch).to.be.an.instanceof(Function);
(0, test_1.expect)(__1.http.delete).to.be.an.instanceof(Function);
});
it('has empty headers (by default)', function () {
(0, test_1.expect)(__1.http.headers).to.eql({});
});
});
describe('create', function () {
it('creates (default headers)', function () {
var client = __1.http.create();
(0, test_1.expect)(client.headers).to.eql({});
});
it('creates with custom headers (passed through all calls)', function () {
var client = __1.Http.create({ headers: { MyHeader: 'abc' } });
(0, test_1.expect)(client.headers.MyHeader).to.eql('abc');
});
it('clones headers when creating child', function () {
var client1 = __1.Http.create({ headers: { foo: 'foo' } });
var client2 = client1.create({ headers: { bar: 'bar' } });
var client3 = client2.create({ headers: { foo: 'zoo', baz: 'baz' } });
(0, test_1.expect)(client1.headers).to.eql({ foo: 'foo' });
(0, test_1.expect)(client2.headers).to.eql({ foo: 'foo', bar: 'bar' });
(0, test_1.expect)(client3.headers).to.eql({ foo: 'zoo', bar: 'bar', baz: 'baz' });
});
});
describe('headers', function () {
it('headers immutable', function () {
var client = __1.http.create({ headers: { foo: 'hello' } });
var res1 = client.headers;
var res2 = client.headers;
(0, test_1.expect)(res1).to.eql(res2);
(0, test_1.expect)(res1).to.not.equal(res2);
});
it('merges headers (client => method)', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var client, res, headers;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
client = __1.Http.create({ headers: { foo: 'one' } });
client.req$.subscribe(function (e) { return e.respond({ status: 200 }); });
return [4, client.get('http://localhost/foo', { headers: { bar: 'two' } })];
case 1:
res = _a.sent();
headers = res.headers;
(0, test_1.expect)(headers.foo).to.eql('one');
(0, test_1.expect)(headers.bar).to.eql('two');
return [2];
}
});
}); });
it('overrides headers (client => method)', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var client, res, headers;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
client = __1.http.create({ headers: { foo: 'one', bar: 'abc' } });
client.req$.subscribe(function (e) { return e.respond({ status: 200 }); });
return [4, client.get('http://localhost/foo', {
headers: { foo: 'two', new: 'hello' },
})];
case 1:
res = _a.sent();
headers = res.headers;
(0, test_1.expect)(headers.foo).to.eql('two');
(0, test_1.expect)(headers.bar).to.eql('abc');
(0, test_1.expect)(headers.new).to.eql('hello');
return [2];
}
});
}); });
});
describe('events (observable)', function () {
it('BEFORE event', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var client, events;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
client = __1.http.create();
events = [];
client.req$.subscribe(function (e) { return e.respond({ status: 200 }); });
client.req$.subscribe(function (e) { return events.push(e); });
return [4, client.get('http://localhost/foo')];
case 1:
_a.sent();
(0, test_1.expect)(events.length).to.eql(1);
(0, test_1.expect)(events[0].method).to.eql('GET');
(0, test_1.expect)(events[0].url).to.eql('http://localhost/foo');
return [2];
}
});
}); });
it('AFTER event: respond sync (object/json)', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var client, events, res1, res2;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
client = __1.http.create();
events = [];
client.req$.subscribe(function (e) {
e.respond({
status: 202,
statusText: 'foobar',
data: { msg: 'hello' },
});
});
client.res$.subscribe(function (e) { return events.push(e); });
return [4, client.get('http://localhost/foo')];
case 1:
res1 = _a.sent();
(0, test_1.expect)(events.length).to.eql(1);
(0, test_1.expect)(events[0].method).to.eql('GET');
(0, test_1.expect)(events[0].url).to.eql('http://localhost/foo');
res2 = events[0].response;
(0, test_1.expect)(res2.status).to.eql(202);
(0, test_1.expect)(res2.statusText).to.eql('foobar');
(0, test_1.expect)(res1.text).to.eql(res2.text);
(0, test_1.expect)(res1.json).to.eql({ msg: 'hello' });
return [2];
}
});
}); });
it('AFTER event: respond async function (object/json)', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var client, events, res1, res2;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
client = __1.http.create();
events = [];
client.req$.subscribe(function (e) {
e.respond(function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, test_1.time.wait(20)];
case 1:
_a.sent();
return [2, {
status: 202,
statusText: 'foobar',
data: { msg: 'hello' },
}];
}
});
}); });
});
client.res$.subscribe(function (e) { return events.push(e); });
return [4, client.get('http://localhost/foo')];
case 1:
res1 = _a.sent();
(0, test_1.expect)(events.length).to.eql(1);
(0, test_1.expect)(events[0].method).to.eql('GET');
(0, test_1.expect)(events[0].url).to.eql('http://localhost/foo');
(0, test_1.expect)(events[0].ok).to.eql(true);
(0, test_1.expect)(events[0].status).to.eql(202);
res2 = events[0].response;
(0, test_1.expect)(res2.status).to.eql(202);
(0, test_1.expect)(res2.statusText).to.eql('foobar');
(0, test_1.expect)(res1.text).to.eql(res2.text);
(0, test_1.expect)(res1.json).to.eql({ msg: 'hello' });
return [2];
}
});
}); });
it('AFTER event: respond sync function (file/binary)', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var client, events, image1, image2, res, path, _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
client = __1.http.create();
events = [];
return [4, test_1.fs.readFile(test_1.fs.resolve('src/test/assets/kitten.jpg'))];
case 1:
image1 = _b.sent();
return [4, test_1.fs.readFile(test_1.fs.resolve('src/test/assets/bird.png'))];
case 2:
image2 = _b.sent();
client.req$.subscribe(function (e) {
var data = new stream_1.Readable();
data.push(image2);
data.push(null);
e.respond(function () { return ({
status: 202,
statusText: 'foobar',
data: data,
}); });
});
client.res$.subscribe(function (e) { return events.push(e); });
return [4, client.post('http://localhost/foo', image1)];
case 3:
res = _b.sent();
(0, test_1.expect)(events.length).to.eql(1);
(0, test_1.expect)(events[0].method).to.eql('POST');
(0, test_1.expect)(events[0].url).to.eql('http://localhost/foo');
(0, test_1.expect)(res.body).to.not.eql(undefined);
if (!res.body) return [3, 6];
path = test_1.fs.resolve('tmp/response-bird.png');
return [4, test_1.fs.stream.save(path, res.body)];
case 4:
_b.sent();
_a = test_1.expect;
return [4, test_1.fs.readFile(path)];
case 5:
_a.apply(void 0, [(_b.sent()).toString()]).to.eql(image2.toString());
_b.label = 6;
case 6: return [2];
}
});
}); });
it('AFTER event: respond (string)', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var client, events, res1, res2;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
client = __1.http.create();
events = [];
client.req$.subscribe(function (e) {
e.respond({
status: 200,
headers: { 'content-type': 'text/plain' },
data: 'hello',
});
});
client.res$.subscribe(function (e) { return events.push(e); });
return [4, client.get('http://localhost/foo')];
case 1:
res1 = _a.sent();
(0, test_1.expect)(events.length).to.eql(1);
(0, test_1.expect)(events[0].method).to.eql('GET');
(0, test_1.expect)(events[0].url).to.eql('http://localhost/foo');
res2 = events[0].response;
(0, test_1.expect)(res2.status).to.eql(200);
(0, test_1.expect)(res2.statusText).to.eql('OK');
(0, test_1.expect)(res1.text).to.eql(res2.text);
(0, test_1.expect)(res1.text).to.eql('hello');
(0, test_1.expect)(res1.json).to.eql('');
return [2];
}
});
}); });
it('AFTER event: respond (<empty>)', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var client, events, res1, res2;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
client = __1.http.create();
events = [];
client.req$.subscribe(function (e) {
e.respond({
status: 200,
data: undefined,
});
});
client.res$.subscribe(function (e) { return events.push(e); });
return [4, client.get('http://localhost/foo')];
case 1:
res1 = _a.sent();
(0, test_1.expect)(events.length).to.eql(1);
(0, test_1.expect)(events[0].method).to.eql('GET');
(0, test_1.expect)(events[0].url).to.eql('http://localhost/foo');
res2 = events[0].response;
(0, test_1.expect)(res2.status).to.eql(200);
(0, test_1.expect)(res2.statusText).to.eql('OK');
(0, test_1.expect)(res1.text).to.eql(res2.text);
(0, test_1.expect)(res1.text).to.eql('');
(0, test_1.expect)(res1.json).to.eql('');
return [2];
}
});
}); });
it('sends event identifier ("uid") that is shared between before/after events', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var client, events;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
client = __1.http.create();
client.req$.subscribe(function (e) { return e.respond({ status: 200 }); });
events = [];
client.$.subscribe(function (e) { return events.push(e); });
return [4, client.get('http://localhost/foo')];
case 1:
_a.sent();
(0, test_1.expect)(events.length).to.eql(2);
(0, test_1.expect)(events[0].payload.tx).to.eql(events[1].payload.tx);
return [2];
}
});
}); });
it('does not share events between instances', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var client1, client2, events1, events2;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
client1 = __1.http.create();
client2 = client1.create();
client1.req$.subscribe(function (e) { return e.respond({ status: 200 }); });
client2.req$.subscribe(function (e) { return e.respond({ status: 200 }); });
events1 = [];
events2 = [];
client1.$.subscribe(function (e) { return events1.push(e); });
client2.$.subscribe(function (e) { return events2.push(e); });
return [4, client1.get('http://localhost/foo')];
case 1:
_a.sent();
return [4, client2.get('http://localhost/foo')];
case 2:
_a.sent();
return [4, client2.get('http://localhost/foo')];
case 3:
_a.sent();
(0, test_1.expect)(events1.length).to.eql(2);
(0, test_1.expect)(events2.length).to.eql(4);
return [2];
}
});
}); });
});
describe('verbs', function () {
var events = [];
var client;
beforeEach(function () {
events = [];
client = __1.http.create();
client.$.subscribe(function (e) { return events.push(e); });
client.req$.subscribe(function (e) { return e.respond({ status: 200 }); });
});
it('head', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, client.head('http://localhost/foo')];
case 1:
_a.sent();
(0, test_1.expect)(events.length).to.eql(2);
(0, test_1.expect)(events[0].payload.method).to.eql('HEAD');
(0, test_1.expect)(events[1].payload.method).to.eql('HEAD');
return [2];
}
});
}); });
it('get', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, client.get('http://localhost/foo')];
case 1:
_a.sent();
(0, test_1.expect)(events.length).to.eql(2);
(0, test_1.expect)(events[0].payload.method).to.eql('GET');
(0, test_1.expect)(events[1].payload.method).to.eql('GET');
return [2];
}
});
}); });
it('put', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, client.put('http://localhost/foo', { foo: 123 })];
case 1:
_a.sent();
(0, test_1.expect)(events.length).to.eql(2);
(0, test_1.expect)(events[0].payload.method).to.eql('PUT');
(0, test_1.expect)(events[1].payload.method).to.eql('PUT');
return [2];
}
});
}); });
it('post', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, client.post('http://localhost/foo', { foo: 123 })];
case 1:
_a.sent();
(0, test_1.expect)(events.length).to.eql(2);
(0, test_1.expect)(events[0].payload.method).to.eql('POST');
(0, test_1.expect)(events[1].payload.method).to.eql('POST');
return [2];
}
});
}); });
it('patch', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, client.patch('http://localhost/foo', { foo: 123 })];
case 1:
_a.sent();
(0, test_1.expect)(events.length).to.eql(2);
(0, test_1.expect)(events[0].payload.method).to.eql('PATCH');
(0, test_1.expect)(events[1].payload.method).to.eql('PATCH');
return [2];
}
});
}); });
it('delete', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, client.delete('http://localhost/foo')];
case 1:
_a.sent();
(0, test_1.expect)(events.length).to.eql(2);
(0, test_1.expect)(events[0].payload.method).to.eql('DELETE');
(0, test_1.expect)(events[1].payload.method).to.eql('DELETE');
return [2];
}
});
}); });
});
describe('fetch', function () {
it('http server: text', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var data, port, server, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
data = "console.log('hello');";
port = (0, test_1.randomPort)();
server = (0, http_1.createServer)(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/javascript' });
res.write(data);
res.end();
}).listen(port);
return [4, __1.http.create().get("http://localhost:".concat(port))];
case 1:
res = _a.sent();
server.close();
(0, test_1.expect)(res.status).to.eql(200);
(0, test_1.expect)(res.statusText).to.eql('OK');
(0, test_1.expect)(res.headers['content-type']).to.eql('text/javascript');
(0, test_1.expect)(res.contentType.is.binary).to.eql(false);
(0, test_1.expect)(res.contentType.is.json).to.eql(false);
(0, test_1.expect)(res.contentType.is.text).to.eql(true);
(0, test_1.expect)(res.text).to.eql(data);
(0, test_1.expect)(res.json).to.eql('');
(0, test_1.expect)(test_1.util.isStream(res.body)).to.eql(true);
return [2];
}
});
}); });
it('http server: json', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var data, port, server, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
data = { msg: 'hello' };
port = (0, test_1.randomPort)();
server = (0, http_1.createServer)(function (req, res) {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(data));
res.end();
}).listen(port);
return [4, __1.http.create().get("http://localhost:".concat(port))];
case 1:
res = _a.sent();
server.close();
(0, test_1.expect)(res.status).to.eql(200);
(0, test_1.expect)(res.statusText).to.eql('OK');
(0, test_1.expect)(res.headers['content-type']).to.eql('application/json');
(0, test_1.expect)(res.contentType.is.binary).to.eql(false);
(0, test_1.expect)(res.contentType.is.json).to.eql(true);
(0, test_1.expect)(res.contentType.is.text).to.eql(false);
(0, test_1.expect)(res.text).to.eql('');
(0, test_1.expect)(res.json).to.eql(data);
(0, test_1.expect)(test_1.util.isStream(res.body)).to.eql(true);
return [2];
}
});
}); });
it('http server: json (404)', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var data, port, server, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
data = { error: 'Fail' };
port = (0, test_1.randomPort)();
server = (0, http_1.createServer)(function (req, res) {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(data));
res.end();
}).listen(port);
return [4, __1.http.create().get("http://localhost:".concat(port))];
case 1:
res = _a.sent();
server.close();
(0, test_1.expect)(res.status).to.eql(404);
(0, test_1.expect)(res.statusText).to.eql('Not Found');
return [2];
}
});
}); });
it('http server: file/binary', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var path, image, port, server, res, path_1, _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
path = test_1.fs.resolve('src/test/assets/kitten.jpg');
return [4, test_1.fs.readFile(path)];
case 1:
image = _b.sent();
port = (0, test_1.randomPort)();
server = (0, http_1.createServer)(function (req, res) {
res.writeHead(200, {
'Content-Type': 'image/jpeg',
'Content-Length': image.length,
});
test_1.fs.createReadStream(path).pipe(res);
}).listen(port);
return [4, __1.http.create().get("http://localhost:".concat(port))];
case 2:
res = _b.sent();
server.close();
(0, test_1.expect)(res.status).to.eql(200);
(0, test_1.expect)(res.statusText).to.eql('OK');
(0, test_1.expect)(res.headers['content-type']).to.eql('image/jpeg');
(0, test_1.expect)(res.contentType.is.binary).to.eql(true);
(0, test_1.expect)(res.contentType.is.json).to.eql(false);
(0, test_1.expect)(res.contentType.is.text).to.eql(false);
(0, test_1.expect)(res.text).to.eql('');
(0, test_1.expect)(res.json).to.eql('');
(0, test_1.expect)(test_1.util.isStream(res.body)).to.eql(true);
if (!res.body) return [3, 5];
path_1 = test_1.fs.resolve('tmp/kitten.jpg');
return [4, test_1.fs.stream.save(path_1, res.body)];
case 3:
_b.sent();
_a = test_1.expect;
return [4, test_1.fs.readFile(path_1)];
case 4:
_a.apply(void 0, [(_b.sent()).toString()]).to.eql(image.toString());
_b.label = 5;
case 5: return [2];
}
});
}); });
it('injected [fetch] function', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var requests, data, fetch, client, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
requests = [];
data = { msg: 'hello' };
fetch = function (req) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
requests.push(req);
return [4, test_1.time.wait(10)];
case 1:
_a.sent();
return [2, {
status: 202,
headers: test_1.util.toRawHeaders({ foo: 'bar', 'Content-Type': 'application/json' }),
body: null,
text: function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
return [2, JSON.stringify(data)];
}); }); },
json: function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
return [2, data];
}); }); },
}];
}
});
}); };
client = __1.http.create({ fetch: fetch });
return [4, client.post("http://localhost", { send: true }, { headers: { foo: '123' } })];
case 1:
res = _a.sent();
(0, test_1.expect)(res.status).to.eql(202);
(0, test_1.expect)(res.statusText).to.eql('OK');
(0, test_1.expect)(res.headers.foo).to.eql('bar');
(0, test_1.expect)(res.headers['content-type']).to.eql('application/json');
(0, test_1.expect)(res.contentType.is.binary).to.eql(false);
(0, test_1.expect)(res.contentType.is.json).to.eql(true);
(0, test_1.expect)(res.contentType.is.text).to.eql(false);
(0, test_1.expect)(res.json).to.eql(data);
(0, test_1.expect)(res.json).to.eql(data);
(0, test_1.expect)(res.text).to.eql('');
(0, test_1.expect)(res.body).to.eql(undefined);
(0, test_1.expect)(requests.length).to.eql(1);
(0, test_1.expect)(requests[0].url).to.eql('http://localhost');
(0, test_1.expect)(requests[0].mode).to.eql('cors');
(0, test_1.expect)(requests[0].method).to.eql('POST');
(0, test_1.expect)(requests[0].data).to.eql({ send: true });
return [2];
}
});
}); });
it('modified JSON content-type', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var requests, data, fetch, client, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
requests = [];
data = { msg: 'hello' };
fetch = function (req) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
requests.push(req);
return [4, test_1.time.wait(10)];
case 1:
_a.sent();
return [2, {
status: 202,
headers: test_1.util.toRawHeaders({ 'Content-Type': 'application/vnd.foo.picture+json' }),
body: null,
text: function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
return [2, JSON.stringify(data)];
}); }); },
json: function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
return [2, data];
}); }); },
}];
}
});
}); };
client = __1.http.create({ fetch: fetch });
return [4, client.post("http://localhost", { send: true })];
case 1:
res = _a.sent();
(0, test_1.expect)(res.contentType.is.json).to.eql(true);
return [2];
}
});
}); });
});
describe('modify (HTTP server)', function () {
var testServer = function () {
var port = (0, test_1.randomPort)();
var instance = (0, http_1.createServer)(function (req, res) {
api.headers = req.headers;
res.writeHead(200).end();
}).listen(port);
var api = {
port: port,
instance: instance,
headers: {},
dispose: function () { return instance.close(); },
};
return api;
};
it('header(key, value)', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var server, client, events, headers;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
server = testServer();
client = __1.http.create({ headers: { foo: 'one' } });
events = [];
client.req$.subscribe(function (e) {
events.push(e);
e.modify.header('foo', 'abc');
e.modify.header('bar', 'hello');
e.modify.header('baz', 'hello');
});
client.req$.subscribe(function (e) {
e.modify.header('foo', 'zoo');
e.modify.header('bar', '');
});
return [4, client.get("http://localhost:".concat(server.port))];
case 1:
_a.sent();
server.dispose();
(0, test_1.expect)(events[0].isModified).to.eql(true);
headers = server.headers;
(0, test_1.expect)(headers.foo).to.eql('zoo');
(0, test_1.expect)(headers.bar).to.eql(undefined);
(0, test_1.expect)(headers.baz).to.eql('hello');
return [2];
}
});
}); });
it('headers.merge (multiple times, cumulative)', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var server, client, events, headers;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
server = testServer();
client = __1.http.create({ headers: { foo: 'one', bar: 'abc', zoo: 'zoo' } });
events = [];
client.req$.subscribe(function (e) {
events.push(e);
e.modify.headers.merge({ foo: 'two', baz: 'baz' });
e.modify.headers.merge({ zoo: '' });
e.modify.headers.merge({ foo: 'three' });
});
return [4, client.get("http://localhost:".concat(server.port, "/foo"))];
case 1:
_a.sent();
server.dispose();
(0, test_1.expect)(events[0].isModified).to.eql(true);
headers = server.headers;
(0, test_1.expect)(headers.zoo).to.eql(undefined);
(0, test_1.expect)(headers.foo).to.eql('three');
(0, test_1.expect)(headers.bar).to.eql('abc');
(0, test_1.expect)(headers.baz).to.eql('baz');
return [2];
}
});
}); });
it('headers.replace', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var server, client, events, headers;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
server = testServer();
client = __1.http.create({ headers: { foo: 'one' } });
events = [];
client.req$.subscribe(function (e) {
events.push(e);
e.modify.headers.replace({ foo: 'two', baz: 'baz' });
});
client.req$.subscribe(function (e) {
e.modify.headers.replace({ bar: 'bar', zoo: '' });
});
return [4, client.get("http://localhost:".concat(server.port))];
case 1:
_a.sent();
server.dispose();
(0, test_1.expect)(events[0].isModified).to.eql(true);
headers = server.headers;
(0, test_1.expect)(headers.foo).to.eql(undefined);
(0, test_1.expect)(headers.zoo).to.eql(undefined);
(0, test_1.expect)(headers.bar).to.eql('bar');
return [2];
}
});
}); });
});
});