mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-07 22:12:53 +00:00
feat: add api
This commit is contained in:
143
test/bulk.spec.ts
Normal file
143
test/bulk.spec.ts
Normal file
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Copyright (C) 2018 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {SCBulkAddRoute, SCBulkDoneRoute, SCDish, SCMessage} from '@openstapps/core';
|
||||
import * as chai from 'chai';
|
||||
import {expect} from 'chai';
|
||||
import * as chaiAsPromised from 'chai-as-promised';
|
||||
import * as chaiSpies from 'chai-spies';
|
||||
import {suite, test} from 'mocha-typescript';
|
||||
import * as moment from 'moment';
|
||||
import {Bulk} from '../src/bulk';
|
||||
import {Client} from '../src/client';
|
||||
import {BulkWithMultipleTypesError} from '../src/errors';
|
||||
import {HttpClient} from '../src/httpClient';
|
||||
|
||||
chai.should();
|
||||
chai.use(chaiSpies);
|
||||
chai.use(chaiAsPromised);
|
||||
|
||||
const sandbox = chai.spy.sandbox();
|
||||
|
||||
const bulkAddRoute = new SCBulkAddRoute();
|
||||
const bulkDoneRoute = new SCBulkDoneRoute();
|
||||
|
||||
const httpClient = new HttpClient();
|
||||
const client = new Client(httpClient, 'http://localhost');
|
||||
|
||||
@suite()
|
||||
export class BulkSpec {
|
||||
@test
|
||||
async add() {
|
||||
sandbox.on(client, 'invokeRoute', () => {
|
||||
return {};
|
||||
});
|
||||
|
||||
expect(client.invokeRoute).not.to.have.been.called();
|
||||
|
||||
const bulk = new Bulk('dish', client, {
|
||||
expiration: moment().add(3600, 'seconds').format(),
|
||||
source: 'foo',
|
||||
state: 'in progress',
|
||||
type: 'dish',
|
||||
uid: 'bar',
|
||||
});
|
||||
|
||||
const dish: SCDish = {
|
||||
categories: [
|
||||
'main dish',
|
||||
],
|
||||
name: 'foobar',
|
||||
origin: {
|
||||
indexed: moment().format(),
|
||||
name: 'bar',
|
||||
},
|
||||
type: 'dish',
|
||||
uid: 'foo',
|
||||
};
|
||||
|
||||
await bulk.add(dish);
|
||||
|
||||
expect(client.invokeRoute).to.have.been.first.called.with(bulkAddRoute, {
|
||||
UID: 'bar',
|
||||
}, dish);
|
||||
}
|
||||
|
||||
@test
|
||||
async addFails() {
|
||||
const bulk = new Bulk('dish', client, {
|
||||
expiration: moment().add(3600, 'seconds').format(),
|
||||
source: 'foo',
|
||||
state: 'in progress',
|
||||
type: 'dish',
|
||||
uid: 'bar',
|
||||
});
|
||||
|
||||
const message: SCMessage = {
|
||||
audiences: [
|
||||
'students',
|
||||
],
|
||||
message: 'Lorem ipsum.',
|
||||
name: 'foobar',
|
||||
origin: {
|
||||
indexed: moment().format(),
|
||||
name: 'bar',
|
||||
},
|
||||
type: 'message',
|
||||
uid: 'foo',
|
||||
};
|
||||
|
||||
return bulk.add(message).should.be.rejectedWith(BulkWithMultipleTypesError);
|
||||
}
|
||||
|
||||
async after() {
|
||||
sandbox.restore();
|
||||
}
|
||||
|
||||
@test
|
||||
async construct() {
|
||||
expect(() => {
|
||||
return new Bulk('dish', client, {
|
||||
expiration: moment().add(3600, 'seconds').format(),
|
||||
source: 'foo',
|
||||
state: 'in progress',
|
||||
type: 'dish',
|
||||
uid: 'bar',
|
||||
});
|
||||
}).not.to.throw();
|
||||
}
|
||||
|
||||
@test
|
||||
async done() {
|
||||
sandbox.on(client, 'invokeRoute', () => {
|
||||
return {};
|
||||
});
|
||||
|
||||
expect(client.invokeRoute).not.to.have.been.called();
|
||||
|
||||
const bulk = new Bulk('dish', client, {
|
||||
expiration: moment().add(3600, 'seconds').format(),
|
||||
source: 'foo',
|
||||
state: 'in progress',
|
||||
type: 'dish',
|
||||
uid: 'bar',
|
||||
});
|
||||
|
||||
await bulk.done();
|
||||
|
||||
expect(client.invokeRoute).to.have.been.first.called.with(bulkDoneRoute, {
|
||||
UID: 'bar',
|
||||
});
|
||||
}
|
||||
}
|
||||
583
test/client.spec.ts
Normal file
583
test/client.spec.ts
Normal file
@@ -0,0 +1,583 @@
|
||||
/*
|
||||
* Copyright (C) 2018 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {
|
||||
SCFeedbackRequest,
|
||||
SCFeedbackResponse,
|
||||
SCFeedbackRoute,
|
||||
SCIndexResponse,
|
||||
SCIndexRoute,
|
||||
SCMessage,
|
||||
SCMultiSearchResponse,
|
||||
SCMultiSearchRoute,
|
||||
SCSearchRequest,
|
||||
SCSearchResponse,
|
||||
SCSearchRoute,
|
||||
} from '@openstapps/core';
|
||||
import * as chai from 'chai';
|
||||
import {expect} from 'chai';
|
||||
import * as chaiAsPromised from 'chai-as-promised';
|
||||
import * as chaiSpies from 'chai-spies';
|
||||
import {suite, test} from 'mocha-typescript';
|
||||
import {Client} from '../src/client';
|
||||
import {ApiError, OutOfRangeError} from '../src/errors';
|
||||
import {HttpClient} from '../src/httpClient';
|
||||
import {HttpClientResponse} from '../src/httpClientInterface';
|
||||
|
||||
chai.should();
|
||||
chai.use(chaiSpies);
|
||||
chai.use(chaiAsPromised);
|
||||
|
||||
const sandbox = chai.spy.sandbox();
|
||||
|
||||
const indexRoute = new SCIndexRoute();
|
||||
const feedbackRoute = new SCFeedbackRoute();
|
||||
const multiSearchRoute = new SCMultiSearchRoute();
|
||||
const searchRoute = new SCSearchRoute();
|
||||
|
||||
const httpClient = new HttpClient();
|
||||
|
||||
/**
|
||||
* Recursive Partial
|
||||
*
|
||||
* @see https://stackoverflow.com/a/51365037
|
||||
*/
|
||||
export type RecursivePartial<T> = {
|
||||
[P in keyof T]?:
|
||||
T[P] extends Array<(infer U)> ? Array<RecursivePartial<U>> :
|
||||
T[P] extends object ? RecursivePartial<T[P]> :
|
||||
T[P];
|
||||
};
|
||||
|
||||
async function invokeIndexRoute(): Promise<RecursivePartial<HttpClientResponse<SCIndexResponse>>> {
|
||||
return {
|
||||
body: {
|
||||
backend: {
|
||||
SCVersion: 'foo.bar.dummy',
|
||||
},
|
||||
},
|
||||
statusCode: indexRoute.statusCodeSuccess,
|
||||
};
|
||||
}
|
||||
|
||||
async function invokeIndexRouteFails(): Promise<RecursivePartial<HttpClientResponse<SCIndexResponse>>> {
|
||||
return {
|
||||
body: {
|
||||
backend: {
|
||||
SCVersion: 'foo.bar.dummy',
|
||||
},
|
||||
},
|
||||
statusCode: indexRoute.statusCodeSuccess + 1,
|
||||
};
|
||||
}
|
||||
|
||||
@suite()
|
||||
export class ClientSpec {
|
||||
async after() {
|
||||
sandbox.restore();
|
||||
}
|
||||
|
||||
@test
|
||||
async construct() {
|
||||
expect(() => {
|
||||
return new Client(httpClient, 'http://localhost');
|
||||
}).not.to.throw();
|
||||
}
|
||||
|
||||
@test
|
||||
async constructWithVersion() {
|
||||
sandbox.on(httpClient, 'request', invokeIndexRoute);
|
||||
|
||||
expect(httpClient.request).not.to.have.been.first.called();
|
||||
|
||||
const client = new Client(httpClient, 'http://localhost', 'foo.foo.foo');
|
||||
await client.handshake('foo.bar.dummy');
|
||||
|
||||
expect(httpClient.request).to.have.been.first.called.with({
|
||||
body: {},
|
||||
headers: {
|
||||
'X-StApps-Version': 'foo.foo.foo',
|
||||
},
|
||||
method: indexRoute.method,
|
||||
url: new URL('http://localhost' + indexRoute.getUrlFragment()),
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
async feedback() {
|
||||
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCFeedbackResponse>> => {
|
||||
return {
|
||||
body: {},
|
||||
headers: {},
|
||||
statusCode: feedbackRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
expect(httpClient.request).not.to.have.been.first.called();
|
||||
|
||||
const client = new Client(httpClient, 'http://localhost');
|
||||
const feedback: SCFeedbackRequest = {
|
||||
audiences: [
|
||||
'employees',
|
||||
],
|
||||
message: 'Lorem ipsum.',
|
||||
metaData: {
|
||||
debug: true,
|
||||
platform: 'android',
|
||||
scope: {},
|
||||
sendable: true,
|
||||
state: 'foo',
|
||||
userAgent: 'bar',
|
||||
version: 'foobar',
|
||||
},
|
||||
name: 'foo',
|
||||
origin: {
|
||||
indexed: 'foo',
|
||||
name: 'foo',
|
||||
},
|
||||
type: 'message',
|
||||
uid: 'foo',
|
||||
};
|
||||
await client.feedback(feedback);
|
||||
|
||||
expect(httpClient.request).to.have.been.first.called.with({
|
||||
body: feedback,
|
||||
headers: {},
|
||||
method: feedbackRoute.method,
|
||||
url: new URL('http://localhost' + feedbackRoute.getUrlFragment()),
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
async getThing() {
|
||||
const message: SCMessage = {
|
||||
audiences: [
|
||||
'employees',
|
||||
],
|
||||
message: 'Lorem ipsum.',
|
||||
name: 'foo',
|
||||
origin: {
|
||||
indexed: 'foo',
|
||||
name: 'foo',
|
||||
},
|
||||
type: 'message',
|
||||
uid: 'foo',
|
||||
};
|
||||
|
||||
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCSearchResponse>> => {
|
||||
return {
|
||||
body: {
|
||||
data: [message],
|
||||
facets: [],
|
||||
pagination: {
|
||||
count: 0,
|
||||
offset: 0,
|
||||
total: 0,
|
||||
},
|
||||
stats: {
|
||||
time: 0,
|
||||
},
|
||||
},
|
||||
headers: {},
|
||||
statusCode: searchRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
expect(httpClient.request).not.to.have.been.first.called();
|
||||
|
||||
const client = new Client(httpClient, 'http://localhost');
|
||||
await client.getThing('foo');
|
||||
|
||||
expect(httpClient.request).to.have.been.first.called.with({
|
||||
body: {
|
||||
filter: {
|
||||
arguments: {
|
||||
field: 'uid',
|
||||
value: 'foo',
|
||||
},
|
||||
type: 'value',
|
||||
},
|
||||
size: 1,
|
||||
},
|
||||
headers: {},
|
||||
method: searchRoute.method,
|
||||
url: new URL('http://localhost' + searchRoute.getUrlFragment()),
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
async getThingFailsByEmptyResponse() {
|
||||
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCSearchResponse>> => {
|
||||
return {
|
||||
body: {
|
||||
data: [],
|
||||
facets: [],
|
||||
pagination: {
|
||||
count: 0,
|
||||
offset: 0,
|
||||
total: 0,
|
||||
},
|
||||
stats: {
|
||||
time: 0,
|
||||
},
|
||||
},
|
||||
headers: {},
|
||||
statusCode: searchRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
expect(httpClient.request).not.to.have.been.first.called();
|
||||
|
||||
const client = new Client(httpClient, 'http://localhost');
|
||||
|
||||
return client.getThing('bar').should.be.rejected;
|
||||
}
|
||||
|
||||
@test
|
||||
async getThingFailsByUid() {
|
||||
const message: SCMessage = {
|
||||
audiences: [
|
||||
'employees',
|
||||
],
|
||||
message: 'Lorem ipsum.',
|
||||
name: 'foo',
|
||||
origin: {
|
||||
indexed: 'foo',
|
||||
name: 'foo',
|
||||
},
|
||||
type: 'message',
|
||||
uid: 'foo',
|
||||
};
|
||||
|
||||
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCSearchResponse>> => {
|
||||
return {
|
||||
body: {
|
||||
data: [message],
|
||||
facets: [],
|
||||
pagination: {
|
||||
count: 0,
|
||||
offset: 0,
|
||||
total: 0,
|
||||
},
|
||||
stats: {
|
||||
time: 0,
|
||||
},
|
||||
},
|
||||
headers: {},
|
||||
statusCode: searchRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
expect(httpClient.request).not.to.have.been.first.called();
|
||||
|
||||
const client = new Client(httpClient, 'http://localhost');
|
||||
|
||||
return client.getThing('bar').should.be.rejected;
|
||||
}
|
||||
|
||||
@test
|
||||
async handshake() {
|
||||
sandbox.on(httpClient, 'request', invokeIndexRoute);
|
||||
|
||||
expect(httpClient.request).not.to.have.been.first.called();
|
||||
|
||||
const client = new Client(httpClient, 'http://localhost');
|
||||
await client.handshake('foo.bar.dummy');
|
||||
|
||||
expect(httpClient.request).to.have.been.first.called.with({
|
||||
body: {},
|
||||
headers: {},
|
||||
method: indexRoute.method,
|
||||
url: new URL('http://localhost' + indexRoute.getUrlFragment()),
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
async handshakeFails() {
|
||||
sandbox.on(httpClient, 'request', invokeIndexRoute);
|
||||
|
||||
expect(httpClient.request).not.to.have.been.first.called();
|
||||
|
||||
const client = new Client(httpClient, 'http://localhost');
|
||||
|
||||
return client.handshake('bar.bar.dummy').should.be.rejectedWith(ApiError);
|
||||
}
|
||||
|
||||
@test
|
||||
async invokeRoute() {
|
||||
sandbox.on(httpClient, 'request', invokeIndexRoute);
|
||||
|
||||
expect(httpClient.request).not.to.have.been.first.called();
|
||||
|
||||
const client = new Client(httpClient, 'http://localhost');
|
||||
await client.invokeRoute(indexRoute);
|
||||
|
||||
expect(httpClient.request).to.have.been.first.called.with({
|
||||
body: undefined,
|
||||
headers: {},
|
||||
method: indexRoute.method,
|
||||
url: new URL('http://localhost' + indexRoute.getUrlFragment()),
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
async invokeRouteFails() {
|
||||
sandbox.on(httpClient, 'request', invokeIndexRouteFails);
|
||||
|
||||
expect(httpClient.request).not.to.have.been.first.called();
|
||||
|
||||
const client = new Client(httpClient, 'http://localhost');
|
||||
|
||||
return client.invokeRoute(indexRoute).should.be.rejectedWith(ApiError);
|
||||
}
|
||||
|
||||
@test
|
||||
async multiSearch() {
|
||||
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCMultiSearchResponse>> => {
|
||||
return {
|
||||
body: {
|
||||
a: {
|
||||
data: [],
|
||||
facets: [],
|
||||
pagination: {
|
||||
count: 0,
|
||||
offset: 0,
|
||||
total: 0,
|
||||
},
|
||||
stats: {
|
||||
time: 0,
|
||||
},
|
||||
},
|
||||
b: {
|
||||
data: [],
|
||||
facets: [],
|
||||
pagination: {
|
||||
count: 0,
|
||||
offset: 0,
|
||||
total: 0,
|
||||
},
|
||||
stats: {
|
||||
time: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
headers: {},
|
||||
statusCode: searchRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
expect(httpClient.request).not.to.have.been.first.called();
|
||||
|
||||
const client = new Client(httpClient, 'http://localhost');
|
||||
await client.multiSearch({a: {size: 1}, b: {size: 1}});
|
||||
|
||||
expect(httpClient.request).to.have.been.first.called.with({
|
||||
body: {a: {size: 1}, b: {size: 1}},
|
||||
headers: {},
|
||||
method: multiSearchRoute.method,
|
||||
url: new URL('http://localhost' + multiSearchRoute.getUrlFragment()),
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
async multiSearchWithPreflight() {
|
||||
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCMultiSearchResponse>> => {
|
||||
return {
|
||||
body: {
|
||||
bar: {
|
||||
data: [],
|
||||
facets: [],
|
||||
pagination: {
|
||||
count: 0,
|
||||
offset: 0,
|
||||
total: 500,
|
||||
},
|
||||
stats: {
|
||||
time: 0,
|
||||
},
|
||||
},
|
||||
foo: {
|
||||
data: [],
|
||||
facets: [],
|
||||
pagination: {
|
||||
count: 0,
|
||||
offset: 0,
|
||||
total: 1000,
|
||||
},
|
||||
stats: {
|
||||
time: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
headers: {},
|
||||
statusCode: searchRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
expect(httpClient.request).not.to.have.been.first.called();
|
||||
|
||||
const client = new Client(httpClient, 'http://localhost');
|
||||
await client.multiSearch({foo: {}, bar: {}, foobar: {size: 30}});
|
||||
|
||||
expect(httpClient.request).to.have.been.first.called.with({
|
||||
body: {foo: {size: 0}, bar: {size: 0}},
|
||||
headers: {},
|
||||
method: multiSearchRoute.method,
|
||||
url: new URL('http://localhost' + multiSearchRoute.getUrlFragment()),
|
||||
});
|
||||
expect(httpClient.request).to.have.been.second.called.with({
|
||||
body: {foo: {size: 1000}, bar: {size: 500}, foobar: {size: 30}},
|
||||
headers: {},
|
||||
method: multiSearchRoute.method,
|
||||
url: new URL('http://localhost' + multiSearchRoute.getUrlFragment()),
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
nextWindow() {
|
||||
let searchRequest: SCSearchRequest = {size: 30};
|
||||
const searchResponse: SCSearchResponse = {
|
||||
data: [],
|
||||
facets: [],
|
||||
pagination: {
|
||||
count: 30,
|
||||
offset: 0,
|
||||
total: 60,
|
||||
},
|
||||
stats: {
|
||||
time: 0,
|
||||
},
|
||||
};
|
||||
|
||||
searchRequest = Client.nextWindow(searchRequest, searchResponse);
|
||||
|
||||
expect(searchRequest.from).to.equal(30);
|
||||
|
||||
searchResponse.pagination.offset = 30;
|
||||
|
||||
expect(() => {
|
||||
Client.nextWindow(searchRequest, searchResponse);
|
||||
}).to.throw(OutOfRangeError);
|
||||
}
|
||||
|
||||
@test
|
||||
async search() {
|
||||
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCSearchResponse>> => {
|
||||
return {
|
||||
body: {
|
||||
data: [],
|
||||
facets: [],
|
||||
pagination: {
|
||||
count: 0,
|
||||
offset: 0,
|
||||
total: 0,
|
||||
},
|
||||
stats: {
|
||||
time: 0,
|
||||
},
|
||||
},
|
||||
headers: {},
|
||||
statusCode: searchRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
expect(httpClient.request).not.to.have.been.first.called();
|
||||
|
||||
const client = new Client(httpClient, 'http://localhost');
|
||||
await client.search({size: 1});
|
||||
|
||||
expect(httpClient.request).to.have.been.first.called.with({
|
||||
body: {size: 1},
|
||||
headers: {},
|
||||
method: searchRoute.method,
|
||||
url: new URL('http://localhost' + searchRoute.getUrlFragment()),
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
async searchNext() {
|
||||
const searchResponse: SCSearchResponse = {
|
||||
data: [],
|
||||
facets: [],
|
||||
pagination: {
|
||||
count: 30,
|
||||
offset: 0,
|
||||
total: 60,
|
||||
},
|
||||
stats: {
|
||||
time: 0,
|
||||
},
|
||||
};
|
||||
|
||||
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCSearchResponse>> => {
|
||||
return {
|
||||
body: searchResponse,
|
||||
headers: {},
|
||||
statusCode: searchRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
expect(httpClient.request).not.to.have.been.first.called();
|
||||
|
||||
const client = new Client(httpClient, 'http://localhost');
|
||||
await client.searchNext({from: 0, size: 30}, searchResponse);
|
||||
|
||||
expect(httpClient.request).to.have.been.first.called.with({
|
||||
body: {from: 30, size: 30},
|
||||
headers: {},
|
||||
method: searchRoute.method,
|
||||
url: new URL('http://localhost' + searchRoute.getUrlFragment()),
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
async searchWithPreflight() {
|
||||
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCSearchResponse>> => {
|
||||
return {
|
||||
body: {
|
||||
data: [],
|
||||
facets: [],
|
||||
pagination: {
|
||||
count: 0,
|
||||
offset: 0,
|
||||
total: 1000,
|
||||
},
|
||||
stats: {
|
||||
time: 0,
|
||||
},
|
||||
},
|
||||
headers: {},
|
||||
statusCode: searchRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
expect(httpClient.request).not.to.have.been.first.called();
|
||||
|
||||
const client = new Client(httpClient, 'http://localhost');
|
||||
await client.search({});
|
||||
|
||||
expect(httpClient.request).to.have.been.first.called.with({
|
||||
body: {size: 0},
|
||||
headers: {},
|
||||
method: searchRoute.method,
|
||||
url: new URL('http://localhost' + searchRoute.getUrlFragment()),
|
||||
});
|
||||
expect(httpClient.request).to.have.been.second.called.with({
|
||||
body: {size: 1000},
|
||||
headers: {},
|
||||
method: searchRoute.method,
|
||||
url: new URL('http://localhost' + searchRoute.getUrlFragment()),
|
||||
});
|
||||
}
|
||||
}
|
||||
341
test/connectorClient.spec.ts
Normal file
341
test/connectorClient.spec.ts
Normal file
@@ -0,0 +1,341 @@
|
||||
/*
|
||||
* Copyright (C) 2018 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {
|
||||
SCBulkAddResponse,
|
||||
SCBulkAddRoute,
|
||||
SCBulkDoneResponse,
|
||||
SCBulkDoneRoute,
|
||||
SCBulkResponse,
|
||||
SCBulkRoute,
|
||||
SCMessage,
|
||||
SCThingUpdateResponse,
|
||||
SCThingUpdateRoute,
|
||||
} from '@openstapps/core';
|
||||
import * as chai from 'chai';
|
||||
import {expect} from 'chai';
|
||||
import * as chaiAsPromised from 'chai-as-promised';
|
||||
import * as chaiSpies from 'chai-spies';
|
||||
import {suite, test} from 'mocha-typescript';
|
||||
import * as moment from 'moment';
|
||||
import {ConnectorClient} from '../src/connectorClient';
|
||||
import {EmptyBulkError, NamespaceNotDefinedError} from '../src/errors';
|
||||
import {HttpClient} from '../src/httpClient';
|
||||
import {HttpClientRequest, HttpClientResponse} from '../src/httpClientInterface';
|
||||
|
||||
chai.should();
|
||||
chai.use(chaiSpies);
|
||||
chai.use(chaiAsPromised);
|
||||
|
||||
const sandbox = chai.spy.sandbox();
|
||||
|
||||
const bulkAddRoute = new SCBulkAddRoute();
|
||||
const bulkDoneRoute = new SCBulkDoneRoute();
|
||||
const bulkRoute = new SCBulkRoute();
|
||||
const thingUpdateRoute = new SCThingUpdateRoute();
|
||||
|
||||
const httpClient = new HttpClient();
|
||||
|
||||
@suite()
|
||||
export class ConnectorClientSpec {
|
||||
async after() {
|
||||
sandbox.restore();
|
||||
}
|
||||
|
||||
@test
|
||||
async bulk() {
|
||||
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCBulkResponse>> => {
|
||||
return {
|
||||
body: {
|
||||
expiration: moment().add(1800, 'seconds').format(),
|
||||
source: 'foo',
|
||||
state: 'in progress',
|
||||
type: 'message',
|
||||
uid: 'foo',
|
||||
},
|
||||
headers: {},
|
||||
statusCode: bulkRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
expect(httpClient.request).not.to.have.been.called();
|
||||
|
||||
const connectorClient = new ConnectorClient(httpClient, 'http://localhost');
|
||||
await connectorClient.bulk('message', 'foo', 1800);
|
||||
|
||||
expect(httpClient.request).to.have.been.first.called.with({
|
||||
body: {
|
||||
expiration: moment().add(1800, 'seconds').format(),
|
||||
source: 'foo',
|
||||
type: 'message',
|
||||
},
|
||||
headers: {},
|
||||
method: bulkRoute.method,
|
||||
url: new URL('http://localhost' + bulkRoute.getUrlFragment()),
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
async bulkWithoutTimeout() {
|
||||
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCBulkResponse>> => {
|
||||
return {
|
||||
body: {
|
||||
expiration: moment().add(3600, 'seconds').format(),
|
||||
source: 'foo',
|
||||
state: 'in progress',
|
||||
type: 'message',
|
||||
uid: 'foo',
|
||||
},
|
||||
headers: {},
|
||||
statusCode: bulkRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
expect(httpClient.request).not.to.have.been.called();
|
||||
|
||||
const connectorClient = new ConnectorClient(httpClient, 'http://localhost');
|
||||
await connectorClient.bulk('message', 'foo');
|
||||
|
||||
expect(httpClient.request).to.have.been.first.called.with({
|
||||
body: {
|
||||
expiration: moment().add(3600, 'seconds').format(),
|
||||
source: 'foo',
|
||||
type: 'message',
|
||||
},
|
||||
headers: {},
|
||||
method: bulkRoute.method,
|
||||
url: new URL('http://localhost' + bulkRoute.getUrlFragment()),
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
async index() {
|
||||
const messages: SCMessage[] = [
|
||||
{
|
||||
audiences: [
|
||||
'employees',
|
||||
],
|
||||
message: 'Lorem ipsum.',
|
||||
name: 'foo',
|
||||
origin: {
|
||||
indexed: 'foo',
|
||||
name: 'foo',
|
||||
},
|
||||
type: 'message',
|
||||
uid: 'foo',
|
||||
},
|
||||
{
|
||||
audiences: [
|
||||
'employees',
|
||||
],
|
||||
message: 'Lorem ipsum.',
|
||||
name: 'foo',
|
||||
origin: {
|
||||
indexed: 'foo',
|
||||
name: 'foo',
|
||||
},
|
||||
type: 'message',
|
||||
uid: 'bar',
|
||||
},
|
||||
];
|
||||
|
||||
type responses = SCBulkResponse | SCBulkAddResponse | SCBulkDoneResponse;
|
||||
|
||||
sandbox.on(httpClient, 'request', async (request: HttpClientRequest)
|
||||
: Promise<HttpClientResponse<responses>> => {
|
||||
if (request.url.toString() === new URL('http://localhost' + bulkRoute.getUrlFragment()).toString()) {
|
||||
return {
|
||||
body: {
|
||||
expiration: moment().add(3600, 'seconds').format(),
|
||||
source: 'copy',
|
||||
state: 'in progress',
|
||||
type: 'message',
|
||||
uid: 'foo',
|
||||
},
|
||||
headers: {},
|
||||
statusCode: bulkRoute.statusCodeSuccess,
|
||||
};
|
||||
} else if (request.url.toString() === new URL('http://localhost' + bulkAddRoute.getUrlFragment({
|
||||
UID: 'foo',
|
||||
})).toString()) {
|
||||
return {
|
||||
body: {},
|
||||
headers: {},
|
||||
statusCode: bulkAddRoute.statusCodeSuccess,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
body: {},
|
||||
headers: {},
|
||||
statusCode: bulkDoneRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
const connectorClient = new ConnectorClient(httpClient, 'http://localhost');
|
||||
await connectorClient.index(messages, 'copy');
|
||||
|
||||
expect(httpClient.request).to.have.been.first.called.with({
|
||||
body: {
|
||||
expiration: moment().add(3600, 'seconds').format(),
|
||||
source: 'copy',
|
||||
type: 'message',
|
||||
},
|
||||
headers: {},
|
||||
method: bulkRoute.method,
|
||||
url: new URL('http://localhost' + bulkRoute.getUrlFragment()),
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
async indexFails() {
|
||||
const connectorClient = new ConnectorClient(httpClient, 'http://localhost');
|
||||
return connectorClient.index([]).should.be.rejectedWith(EmptyBulkError);
|
||||
}
|
||||
|
||||
@test
|
||||
async indexWithoutSource() {
|
||||
const messages: SCMessage[] = [
|
||||
{
|
||||
audiences: [
|
||||
'employees',
|
||||
],
|
||||
message: 'Lorem ipsum.',
|
||||
name: 'foo',
|
||||
origin: {
|
||||
indexed: 'foo',
|
||||
name: 'foo',
|
||||
},
|
||||
type: 'message',
|
||||
uid: 'foo',
|
||||
},
|
||||
{
|
||||
audiences: [
|
||||
'employees',
|
||||
],
|
||||
message: 'Lorem ipsum.',
|
||||
name: 'foo',
|
||||
origin: {
|
||||
indexed: 'foo',
|
||||
name: 'foo',
|
||||
},
|
||||
type: 'message',
|
||||
uid: 'bar',
|
||||
},
|
||||
];
|
||||
|
||||
type responses = SCBulkResponse | SCBulkAddResponse | SCBulkDoneResponse;
|
||||
|
||||
sandbox.on(httpClient, 'request', async (request: HttpClientRequest)
|
||||
: Promise<HttpClientResponse<responses>> => {
|
||||
if (request.url.toString() === new URL('http://localhost' + bulkRoute.getUrlFragment()).toString()) {
|
||||
return {
|
||||
body: {
|
||||
expiration: moment().add(3600, 'seconds').format(),
|
||||
source: 'stapps-api',
|
||||
state: 'in progress',
|
||||
type: 'message',
|
||||
uid: 'foo',
|
||||
},
|
||||
headers: {},
|
||||
statusCode: bulkRoute.statusCodeSuccess,
|
||||
};
|
||||
} else if (request.url.toString() === new URL('http://localhost' + bulkAddRoute.getUrlFragment({
|
||||
UID: 'foo',
|
||||
})).toString()) {
|
||||
return {
|
||||
body: {},
|
||||
headers: {},
|
||||
statusCode: bulkAddRoute.statusCodeSuccess,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
body: {},
|
||||
headers: {},
|
||||
statusCode: bulkDoneRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
const connectorClient = new ConnectorClient(httpClient, 'http://localhost');
|
||||
await connectorClient.index(messages);
|
||||
|
||||
expect(httpClient.request).to.have.been.first.called.with({
|
||||
body: {
|
||||
expiration: moment().add(3600, 'seconds').format(),
|
||||
source: 'stapps-api',
|
||||
type: 'message',
|
||||
},
|
||||
headers: {},
|
||||
method: bulkRoute.method,
|
||||
url: new URL('http://localhost' + bulkRoute.getUrlFragment()),
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
makeUuid() {
|
||||
const uuid = ConnectorClient.makeUUID('foo', 'b-tu');
|
||||
|
||||
expect(uuid).to.be.equal('abad271e-d9e9-5802-b7bc-96d8a647b451');
|
||||
expect(ConnectorClient.makeUUID('bar', 'b-tu')).not.to.be.equal(uuid);
|
||||
expect(ConnectorClient.makeUUID('foo', 'f-u')).not.to.be.equal(uuid);
|
||||
}
|
||||
|
||||
@test
|
||||
makeUuidFails() {
|
||||
expect(() => {
|
||||
ConnectorClient.makeUUID('foo', 'b-u');
|
||||
}).to.throw(NamespaceNotDefinedError);
|
||||
}
|
||||
|
||||
@test
|
||||
async update() {
|
||||
const message: SCMessage = {
|
||||
audiences: [
|
||||
'employees',
|
||||
],
|
||||
message: 'Lorem ipsum.',
|
||||
name: 'foo',
|
||||
origin: {
|
||||
indexed: 'foo',
|
||||
name: 'foo',
|
||||
},
|
||||
type: 'message',
|
||||
uid: 'foo',
|
||||
};
|
||||
|
||||
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCThingUpdateResponse>> => {
|
||||
return {
|
||||
body: {},
|
||||
headers: {},
|
||||
statusCode: thingUpdateRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
expect(httpClient.request).not.to.have.been.called();
|
||||
|
||||
const connectorClient = new ConnectorClient(httpClient, 'http://localhost');
|
||||
await connectorClient.update(message);
|
||||
|
||||
expect(httpClient.request).to.have.been.called.with({
|
||||
body: message,
|
||||
headers: {},
|
||||
method: thingUpdateRoute.method,
|
||||
url: new URL('http://localhost' + thingUpdateRoute.getUrlFragment({
|
||||
TYPE: 'message',
|
||||
UID: 'foo',
|
||||
})),
|
||||
});
|
||||
}
|
||||
}
|
||||
197
test/copy.spec.ts
Normal file
197
test/copy.spec.ts
Normal file
@@ -0,0 +1,197 @@
|
||||
/*
|
||||
* Copyright (C) 2018 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {
|
||||
SCBulkAddResponse,
|
||||
SCBulkAddRoute,
|
||||
SCBulkDoneResponse,
|
||||
SCBulkDoneRoute,
|
||||
SCBulkResponse,
|
||||
SCBulkRoute,
|
||||
SCSearchRequest,
|
||||
SCSearchResponse,
|
||||
SCSearchRoute,
|
||||
} from '@openstapps/core';
|
||||
import * as chai from 'chai';
|
||||
import * as chaiAsPromised from 'chai-as-promised';
|
||||
import * as chaiSpies from 'chai-spies';
|
||||
import {suite, test} from 'mocha-typescript';
|
||||
import * as moment from 'moment';
|
||||
import {copy} from '../src/copy';
|
||||
import {ApiError} from '../src/errors';
|
||||
import {HttpClient, RequestOptions, Response} from '../src/httpClient';
|
||||
import {RecursivePartial} from './client.spec';
|
||||
|
||||
chai.should();
|
||||
chai.use(chaiSpies);
|
||||
chai.use(chaiAsPromised);
|
||||
|
||||
const sandbox = chai.spy.sandbox();
|
||||
|
||||
const bulkRoute = new SCBulkRoute();
|
||||
const bulkAddRoute = new SCBulkAddRoute();
|
||||
const bulkDoneRoute = new SCBulkDoneRoute();
|
||||
const searchRoute = new SCSearchRoute();
|
||||
|
||||
const httpClient = new HttpClient();
|
||||
|
||||
@suite()
|
||||
export class CopySpec {
|
||||
async after() {
|
||||
sandbox.restore();
|
||||
}
|
||||
|
||||
@test
|
||||
async copy() {
|
||||
type responses = Response<SCBulkAddResponse | SCBulkDoneResponse | SCBulkResponse | SCSearchResponse>;
|
||||
|
||||
sandbox.on(httpClient, 'request', async (request: RequestOptions): Promise<RecursivePartial<responses>> => {
|
||||
if (request.url.toString() === 'http://foo.bar' + searchRoute.getUrlFragment().toString()) {
|
||||
const body = request.body as SCSearchRequest;
|
||||
|
||||
let count = 0;
|
||||
if (typeof body.size === 'number' && body.size > 0) {
|
||||
count = 1;
|
||||
}
|
||||
|
||||
return {
|
||||
body: {
|
||||
data: [{
|
||||
categories: [
|
||||
'main dish',
|
||||
],
|
||||
name: 'foobar',
|
||||
origin: {
|
||||
indexed: moment().format(),
|
||||
name: 'bar',
|
||||
},
|
||||
type: 'dish',
|
||||
uid: 'foo',
|
||||
}],
|
||||
facets: [],
|
||||
pagination: {
|
||||
count: count,
|
||||
offset: 0,
|
||||
total: 1,
|
||||
},
|
||||
stats: {
|
||||
time: 1,
|
||||
},
|
||||
},
|
||||
statusCode: searchRoute.statusCodeSuccess,
|
||||
};
|
||||
} else if (request.url.toString() === 'http://localhost' + bulkRoute.getUrlFragment().toString()) {
|
||||
return {
|
||||
body: {
|
||||
state: 'in progress',
|
||||
uid: 'foo',
|
||||
},
|
||||
statusCode: bulkRoute.statusCodeSuccess,
|
||||
};
|
||||
} else if (request.url.toString() === 'http://localhost' + bulkAddRoute.getUrlFragment({
|
||||
UID: 'foo',
|
||||
}).toString()) {
|
||||
return {
|
||||
body: {},
|
||||
statusCode: bulkAddRoute.statusCodeSuccess,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
body: {},
|
||||
statusCode: bulkDoneRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
await copy(httpClient, {
|
||||
batchSize: 5,
|
||||
from: 'http://foo.bar',
|
||||
source: 'stapps-copy',
|
||||
to: 'http://localhost',
|
||||
type: 'dish',
|
||||
version: 'foo.bar.foobar',
|
||||
});
|
||||
}
|
||||
|
||||
@test
|
||||
async copyShouldFail() {
|
||||
type responses = Response<SCBulkAddResponse | SCBulkDoneResponse | SCBulkResponse | SCSearchResponse>;
|
||||
|
||||
sandbox.on(httpClient, 'request', async (request: RequestOptions): Promise<RecursivePartial<responses>> => {
|
||||
if (request.url.toString() === 'http://foo.bar' + searchRoute.getUrlFragment().toString()) {
|
||||
const body = request.body as SCSearchRequest;
|
||||
|
||||
if (typeof body.size === 'number' && body.size > 0) {
|
||||
throw new ApiError({});
|
||||
}
|
||||
|
||||
return {
|
||||
body: {
|
||||
data: [{
|
||||
categories: [
|
||||
'main dish',
|
||||
],
|
||||
name: 'foobar',
|
||||
origin: {
|
||||
indexed: moment().format(),
|
||||
name: 'bar',
|
||||
},
|
||||
type: 'dish',
|
||||
uid: 'foo',
|
||||
}],
|
||||
facets: [],
|
||||
pagination: {
|
||||
count: 0,
|
||||
offset: 0,
|
||||
total: 1,
|
||||
},
|
||||
stats: {
|
||||
time: 1,
|
||||
},
|
||||
},
|
||||
statusCode: searchRoute.statusCodeSuccess,
|
||||
};
|
||||
} else if (request.url.toString() === 'http://localhost' + bulkRoute.getUrlFragment().toString()) {
|
||||
return {
|
||||
body: {
|
||||
state: 'in progress',
|
||||
uid: 'foo',
|
||||
},
|
||||
statusCode: bulkRoute.statusCodeSuccess,
|
||||
};
|
||||
} else if (request.url.toString() === 'http://localhost' + bulkAddRoute.getUrlFragment({
|
||||
UID: 'foo',
|
||||
}).toString()) {
|
||||
return {
|
||||
body: {},
|
||||
statusCode: bulkAddRoute.statusCodeSuccess,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
body: {},
|
||||
statusCode: bulkDoneRoute.statusCodeSuccess,
|
||||
};
|
||||
});
|
||||
|
||||
return copy(httpClient, {
|
||||
batchSize: 5,
|
||||
from: 'http://foo.bar',
|
||||
source: 'stapps-copy',
|
||||
to: 'http://localhost',
|
||||
type: 'dish',
|
||||
version: 'foo.bar.foobar',
|
||||
}).should.be.rejectedWith(ApiError);
|
||||
}
|
||||
}
|
||||
60
test/errors.spec.ts
Normal file
60
test/errors.spec.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (C) 2018 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import * as chai from 'chai';
|
||||
import {expect} from 'chai';
|
||||
import * as chaiAsPromised from 'chai-as-promised';
|
||||
import * as chaiSpies from 'chai-spies';
|
||||
import {suite, test} from 'mocha-typescript';
|
||||
import {ApiError} from '../src/errors';
|
||||
|
||||
chai.should();
|
||||
chai.use(chaiSpies);
|
||||
chai.use(chaiAsPromised);
|
||||
|
||||
const sandbox = chai.spy.sandbox();
|
||||
|
||||
@suite()
|
||||
export class ErrorsSpec {
|
||||
async after() {
|
||||
sandbox.restore();
|
||||
}
|
||||
|
||||
@test
|
||||
async shouldAddAdditionalData() {
|
||||
const error = new ApiError({
|
||||
additionalData: 'Lorem ipsum',
|
||||
});
|
||||
|
||||
expect(error.toString()).to.contain('Lorem ipsum');
|
||||
}
|
||||
|
||||
@test
|
||||
async shouldAddRemoteStackTrace() {
|
||||
const error = new ApiError({
|
||||
stack: 'Lorem ipsum',
|
||||
});
|
||||
|
||||
expect(error.toString()).to.contain('Lorem ipsum');
|
||||
}
|
||||
|
||||
@test
|
||||
async shouldSetName() {
|
||||
const error = new ApiError({
|
||||
name: 'Foo',
|
||||
});
|
||||
|
||||
expect(error.name).to.be.equal('Foo');
|
||||
}
|
||||
}
|
||||
140
test/httpClient.spec.ts
Normal file
140
test/httpClient.spec.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright (C) 2018 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import * as chai from 'chai';
|
||||
import {expect} from 'chai';
|
||||
import * as chaiAsPromised from 'chai-as-promised';
|
||||
import * as chaiSpies from 'chai-spies';
|
||||
import {suite, test} from 'mocha-typescript';
|
||||
import * as nock from 'nock';
|
||||
import {HttpClient} from '../src/httpClient';
|
||||
|
||||
chai.should();
|
||||
chai.use(chaiSpies);
|
||||
chai.use(chaiAsPromised);
|
||||
|
||||
const sandbox = chai.spy.sandbox();
|
||||
|
||||
@suite()
|
||||
export class ConnectorClientSpec {
|
||||
async after() {
|
||||
sandbox.restore();
|
||||
}
|
||||
|
||||
@test
|
||||
async construct() {
|
||||
expect(() => {
|
||||
return new HttpClient();
|
||||
}).not.to.throw();
|
||||
}
|
||||
|
||||
@test
|
||||
async request() {
|
||||
const client = new HttpClient();
|
||||
|
||||
nock('http://www.example.com')
|
||||
.get('/resource')
|
||||
.reply(200, 'foo');
|
||||
|
||||
const response = await client.request({
|
||||
url: new URL('http://www.example.com/resource'),
|
||||
});
|
||||
|
||||
expect(response.body).to.be.equal('foo');
|
||||
}
|
||||
|
||||
@test
|
||||
async requestWithBody() {
|
||||
const client = new HttpClient();
|
||||
|
||||
nock('http://www.example.com')
|
||||
.get('/resource')
|
||||
.reply(200, 'foo');
|
||||
|
||||
const response = await client.request({
|
||||
body: {
|
||||
foo: 'bar',
|
||||
},
|
||||
url: new URL('http://www.example.com/resource'),
|
||||
});
|
||||
|
||||
expect(response.body).to.be.equal('foo');
|
||||
}
|
||||
|
||||
@test
|
||||
async requestWithError() {
|
||||
const client = new HttpClient();
|
||||
|
||||
nock('http://www.example.com')
|
||||
.get('/resource')
|
||||
.replyWithError('foo');
|
||||
|
||||
return client.request({
|
||||
body: {
|
||||
foo: 'bar',
|
||||
},
|
||||
url: new URL('http://www.example.com/resource'),
|
||||
}).should.be.rejected;
|
||||
}
|
||||
|
||||
@test
|
||||
async requestWithHeaders() {
|
||||
const client = new HttpClient();
|
||||
|
||||
nock('http://www.example.com')
|
||||
.get('/resource')
|
||||
.reply(200, 'foo');
|
||||
|
||||
const response = await client.request({
|
||||
headers: {
|
||||
'X-StApps-Version': 'foo.bar.foobar',
|
||||
},
|
||||
url: new URL('http://www.example.com/resource'),
|
||||
});
|
||||
|
||||
expect(response.body).to.be.equal('foo');
|
||||
}
|
||||
|
||||
@test
|
||||
async requestWithMethodGet() {
|
||||
const client = new HttpClient();
|
||||
|
||||
nock('http://www.example.com')
|
||||
.get('/resource')
|
||||
.reply(200, 'foo');
|
||||
|
||||
const response = await client.request({
|
||||
method: 'GET',
|
||||
url: new URL('http://www.example.com/resource'),
|
||||
});
|
||||
|
||||
expect(response.body).to.be.equal('foo');
|
||||
}
|
||||
|
||||
@test
|
||||
async requestWithMethodPost() {
|
||||
const client = new HttpClient();
|
||||
|
||||
nock('http://www.example.com')
|
||||
.post('/resource')
|
||||
.reply(200, 'foo');
|
||||
|
||||
const response = await client.request({
|
||||
method: 'POST',
|
||||
url: new URL('http://www.example.com/resource'),
|
||||
});
|
||||
|
||||
expect(response.body).to.be.equal('foo');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user