feat: add client method for invoking plugin routes

This commit is contained in:
Rainer Killinger
2022-01-18 16:52:40 +01:00
parent d601880be1
commit 83120a6734
4 changed files with 160 additions and 118 deletions

View File

@@ -13,9 +13,6 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
SCFeedbackRequest,
SCFeedbackResponse,
SCFeedbackRoute,
SCIndexResponse,
SCIndexRoute,
SCMessage,
@@ -44,7 +41,6 @@ chai.use(chaiAsPromised);
const sandbox = chai.spy.sandbox();
const indexRoute = new SCIndexRoute();
const feedbackRoute = new SCFeedbackRoute();
const multiSearchRoute = new SCMultiSearchRoute();
const searchRoute = new SCSearchRoute();
@@ -65,6 +61,9 @@ export type RecursivePartial<T> = {
async function invokeIndexRoute(): Promise<RecursivePartial<HttpClientResponse<SCIndexResponse>>> {
return {
body: {
app: {
features: {},
},
backend: {
SCVersion: 'foo.bar.dummy',
},
@@ -113,58 +112,7 @@ export class ClientSpec {
'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',
],
categories: [
'news'
],
messageBody: 'Lorem ipsum.',
metaData: {
debug: true,
platform: 'android',
scope: {},
sendable: true,
state: 'foo',
userAgent: 'bar',
version: 'foobar',
},
name: 'foo',
origin: {
indexed: 'foo',
name: 'foo',
type: SCThingOriginType.Remote,
},
type: SCThingType.Message,
uid: 'foo',
};
await client.feedback(feedback);
expect(httpClient.request).to.have.been.first.called.with({
body: feedback,
headers: {
"Content-Type": "application/json",
},
method: feedbackRoute.method,
url: new URL('http://localhost' + feedbackRoute.getUrlFragment()),
url: new URL('http://localhost' + indexRoute.getUrlPath()),
});
}
@@ -227,7 +175,7 @@ export class ClientSpec {
"Content-Type": "application/json",
},
method: searchRoute.method,
url: new URL('http://localhost' + searchRoute.getUrlFragment()),
url: new URL('http://localhost' + searchRoute.getUrlPath()),
});
}
@@ -320,7 +268,7 @@ export class ClientSpec {
"Content-Type": "application/json",
},
method: indexRoute.method,
url: new URL('http://localhost' + indexRoute.getUrlFragment()),
url: new URL('http://localhost' + indexRoute.getUrlPath()),
});
}
@@ -335,6 +283,70 @@ export class ClientSpec {
return client.handshake('bar.bar.dummy').should.be.rejectedWith(ApiError);
}
@test
async invokePlugin() {
sandbox.on(httpClient, 'request', async(): Promise<RecursivePartial<HttpClientResponse<SCIndexResponse>>> => {
return {
body: {
app: {
features: {
plugins: {
"supportedPlugin": { urlPath: "/" }
},
},
},
},
statusCode: indexRoute.statusCodeSuccess,
};
});
expect(httpClient.request).not.to.have.been.first.called();
const client = new Client(httpClient, 'http://localhost');
await client.invokePlugin('unsupportedPlugin').should.be.rejectedWith(ApiError,/.*supportedPlugin.*/gmi);
// again with cached feature definitions
return client.invokePlugin('supportedPlugin')
.should.not.be.rejectedWith(ApiError,/.*supportedPlugin.*/gmi);
}
@test
async invokePluginUnavailable() {
sandbox.on(httpClient, 'request', async(): Promise<RecursivePartial<HttpClientResponse<SCIndexResponse>>> => {
return {
body: {},
statusCode: indexRoute.statusCodeSuccess,
};
});
expect(httpClient.request).not.to.have.been.first.called();
const client = new Client(httpClient, 'http://localhost');
await client.invokePlugin('supportedPlugin').should.be.rejectedWith(ApiError,/.*supportedPlugin.*/gmi);
sandbox.restore();
sandbox.on(httpClient, 'request', async(): Promise<RecursivePartial<HttpClientResponse<SCIndexResponse>>> => {
return {
body: {
app: {
features: {
plugins: {
'unsupportedPlugin': {
urlPath: '/unsupported-plugin'
},
},
},
},
},
statusCode: indexRoute.statusCodeSuccess,
};
});
// again with cached feature definitions
return client.invokePlugin('supportedPlugin')
.should.be.rejectedWith(ApiError,/.*supportedPlugin.*/gmi);
}
@test
async invokeRoute() {
sandbox.on(httpClient, 'request', invokeIndexRoute);
@@ -350,7 +362,7 @@ export class ClientSpec {
"Content-Type": "application/json",
},
method: indexRoute.method,
url: new URL('http://localhost' + indexRoute.getUrlFragment()),
url: new URL('http://localhost' + indexRoute.getUrlPath()),
});
}
@@ -411,7 +423,7 @@ export class ClientSpec {
"Content-Type": "application/json",
},
method: multiSearchRoute.method,
url: new URL('http://localhost' + multiSearchRoute.getUrlFragment()),
url: new URL('http://localhost' + multiSearchRoute.getUrlPath()),
});
}
@@ -461,7 +473,7 @@ export class ClientSpec {
"Content-Type": "application/json",
},
method: multiSearchRoute.method,
url: new URL('http://localhost' + multiSearchRoute.getUrlFragment()),
url: new URL('http://localhost' + multiSearchRoute.getUrlPath()),
});
expect(httpClient.request).to.have.been.second.called.with({
body: {foo: {size: 1000}, bar: {size: 500}, foobar: {size: 30}},
@@ -469,7 +481,7 @@ export class ClientSpec {
"Content-Type": "application/json",
},
method: multiSearchRoute.method,
url: new URL('http://localhost' + multiSearchRoute.getUrlFragment()),
url: new URL('http://localhost' + multiSearchRoute.getUrlPath()),
});
}
@@ -532,7 +544,7 @@ export class ClientSpec {
"Content-Type": "application/json",
},
method: searchRoute.method,
url: new URL('http://localhost' + searchRoute.getUrlFragment()),
url: new URL('http://localhost' + searchRoute.getUrlPath()),
});
}
@@ -570,7 +582,7 @@ export class ClientSpec {
"Content-Type": "application/json",
},
method: searchRoute.method,
url: new URL('http://localhost' + searchRoute.getUrlFragment()),
url: new URL('http://localhost' + searchRoute.getUrlPath()),
});
}
@@ -606,7 +618,7 @@ export class ClientSpec {
"Content-Type": "application/json",
},
method: searchRoute.method,
url: new URL('http://localhost' + searchRoute.getUrlFragment()),
url: new URL('http://localhost' + searchRoute.getUrlPath()),
});
expect(httpClient.request).to.have.been.second.called.with({
body: {size: 1000},
@@ -614,7 +626,7 @@ export class ClientSpec {
"Content-Type": "application/json",
},
method: searchRoute.method,
url: new URL('http://localhost' + searchRoute.getUrlFragment()),
url: new URL('http://localhost' + searchRoute.getUrlPath()),
});
}
}