Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from artificialsolutions/feature/append-session…
Browse files Browse the repository at this point in the history
…-to-url

Append session id to the url if provided
  • Loading branch information
iensu authored Feb 11, 2019
2 parents becd7ff + c163f79 commit d0bbf7e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 32 deletions.
7 changes: 5 additions & 2 deletions src/tie-api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const readClientOrigin = () => {

const getParameters = prune(['viewname', 'viewtype', 'userinput', 'text', 'clientOrigin']);
const formatEngineUrl = (url) => url.endsWith('/') ? url : `${url}/`;
const appendSessionId = (url, sessionId) => sessionId ? `${url};jsessionid=${sessionId}` : url;

const requestBody = (body) => {
const clientOrigin = readClientOrigin();
const jspViewNames = { viewname: 'tieapi', viewtype: 'tieapi' };
Expand All @@ -21,7 +23,7 @@ const requestBody = (body) => {
};

function close(teneoEngineUrl, sessionId, cb) {
const endSessionUrl = `${formatEngineUrl(teneoEngineUrl)}endsession`;
const endSessionUrl = appendSessionId(`${formatEngineUrl(teneoEngineUrl)}endsession`, sessionId);
const headers = sessionId ? { 'Cookie': `JSESSIONID=${sessionId}` } : {};

return http.post(endSessionUrl, requestBody(), headers)
Expand All @@ -48,8 +50,9 @@ function sendInput(teneoEngineUrl, currentSessionId, inputData, cb) {
const headers = currentSessionId ? { 'Cookie': `JSESSIONID=${currentSessionId}` } : {};
const parameters = getParameters(inputData);
const body = requestBody(Object.assign({ userinput: inputData.text }, parameters));
const url = appendSessionId(formatEngineUrl(teneoEngineUrl), currentSessionId);

return http.post(formatEngineUrl(teneoEngineUrl), body, headers)
return http.post(url, body, headers)
.then((response) => cb(null, response))
.catch((error) => cb(error));
}
Expand Down
90 changes: 60 additions & 30 deletions test/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('TIE API Client', () => {
const expectedPayload = { userinput: 'Hej där!', viewname: 'tieapi', viewtype: 'tieapi' };
const teneoEngineUrl = 'https://teneo.engine.com/';
const endSessionUrl = `${teneoEngineUrl}endsession`;
const urlWithSession = (url, sessionId) => `${url};jsessionid=${sessionId}`;

it('should exist', () => {
expect(TIE).to.not.be.undefined;
Expand All @@ -25,22 +26,36 @@ describe('TIE API Client', () => {
it('should POST the input to the specified url', (done) => {
fetchMock.post(teneoEngineUrl, 200);

TIE.sendInput(teneoEngineUrl, 'session-id', { text: 'Hej där!' }, () => {
TIE.sendInput(teneoEngineUrl, null, { text: 'Hej där!' }, () => {
const [, request] = fetchMock.lastCall(teneoEngineUrl);
expect(querystring.parse(request.body)).to.eql(expectedPayload);
done();
});
});

it('should append the session id to the url if passed', (done) => {
const sessionId = 'D3CE28F6558DB88B8AFCE3F36E90920B';
const expectedUrl = urlWithSession(teneoEngineUrl, sessionId);

fetchMock.post(expectedUrl, 200);

TIE.sendInput(teneoEngineUrl, sessionId, { text: 'Hello world' }, () => {
expect(fetchMock.lastUrl()).to.eql(expectedUrl);
done();
});
});

it('should POST with the correct headers', (done) => {
fetchMock.post(teneoEngineUrl, 200);
const sessionId = 'D3CE28F6558DB88B8AFCE3F36E90920B';
const expectedUrl = urlWithSession(teneoEngineUrl, sessionId);
fetchMock.post(expectedUrl, 200);

TIE.sendInput(teneoEngineUrl, 'session-id', { text: 'Hello there' }, () => {
const [, { headers }] = fetchMock.lastCall(teneoEngineUrl);
TIE.sendInput(teneoEngineUrl, sessionId, { text: 'Hello there' }, () => {
const [, { headers }] = fetchMock.lastCall(expectedUrl);

expect(headers.get('Accept')).to.equal('application/json;charset=UTF-8');
expect(headers.get('Content-Type')).to.equal('application/x-www-form-urlencoded;charset=UTF-8');
expect(headers.get('Cookie')).to.equal('JSESSIONID=session-id');
expect(headers.get('Cookie')).to.equal(`JSESSIONID=${sessionId}`);
done();
});
});
Expand All @@ -51,7 +66,7 @@ describe('TIE API Client', () => {
foo: 'bar', baz: '42'
});

TIE.sendInput(teneoEngineUrl, 'session-id', { text: 'Hej där!', foo: 'bar', baz: 42 }, () => {
TIE.sendInput(teneoEngineUrl, null, { text: 'Hej där!', foo: 'bar', baz: 42 }, () => {
const [, request] = fetchMock.lastCall(teneoEngineUrl);
const body = querystring.parse(request.body);
expect(body).to.eql(expected);
Expand All @@ -70,7 +85,7 @@ describe('TIE API Client', () => {
clientOrigin: 'http://some-origin'
};

TIE.sendInput(teneoEngineUrl, 'session-id', inputData, () => {
TIE.sendInput(teneoEngineUrl, null, inputData, () => {
const [, request] = fetchMock.lastCall(teneoEngineUrl);
expect(querystring.parse(request.body)).to.eql(expectedPayload);
done();
Expand All @@ -80,27 +95,27 @@ describe('TIE API Client', () => {
it('should return a Promise if no callback is passed', async () => {
fetchMock.post(teneoEngineUrl, {});

await TIE.sendInput(teneoEngineUrl, 'session-id', { text: 'Hej där!' });
await TIE.sendInput(teneoEngineUrl, null, { text: 'Hej där!' });

const [, request] = fetchMock.lastCall(teneoEngineUrl);
expect(querystring.parse(request.body)).to.eql(expectedPayload);
expect(request.headers.get('Cookie')).to.equal('JSESSIONID=session-id');
});

it('should pass on the Engine response to the caller', async () => {
const sessionId = '9CB85D4871939D0FD1E05BD26C456B06';
const expectedUrl = urlWithSession(teneoEngineUrl, sessionId);
const responseBody = {
status: 0,
input: { text: 'Hej där!', parameters: {} },
output: { text: 'I do not understand', parameters: {} },
sessionId
};

fetchMock.post(teneoEngineUrl, {
fetchMock.post(expectedUrl, {
body: responseBody
});

const response = await TIE.sendInput(teneoEngineUrl, null, { text: 'Hej där!' });
const response = await TIE.sendInput(teneoEngineUrl, sessionId, { text: 'Hej där!' });
expect(response).to.eql(responseBody);
});

Expand Down Expand Up @@ -139,11 +154,14 @@ describe('TIE API Client', () => {
});

it('should send the client origin as a parameter', async () => {
fetchMock.post(teneoEngineUrl, {});
const sessionId = 'D3CE28F6558DB88B8AFCE3F36E90920B';
const expectedUrl = urlWithSession(teneoEngineUrl, sessionId);

await TIE.sendInput(teneoEngineUrl, 'session-id', { text: 'Hej där!' });
fetchMock.post(expectedUrl, {});

const [, request] = fetchMock.lastCall(teneoEngineUrl);
await TIE.sendInput(teneoEngineUrl, sessionId, { text: 'Hej där!' });

const [, request] = fetchMock.lastCall(expectedUrl);
const expected = Object.assign({}, expectedPayload, { clientOrigin });

expect(querystring.parse(request.body)).to.eql(expected);
Expand All @@ -153,25 +171,31 @@ describe('TIE API Client', () => {

describe('#close', () => {
it('should end the current session', (done) => {
fetchMock.post(endSessionUrl, 200);
const sessionId = 'D3CE28F6558DB88B8AFCE3F36E90920B';
const expectedUrl = urlWithSession(endSessionUrl, sessionId);

fetchMock.post(expectedUrl, 200);

TIE.close(teneoEngineUrl, 'session-id', () => {
const [, { headers }] = fetchMock.lastCall(endSessionUrl);
expect(headers.get('Cookie')).to.equal('JSESSIONID=session-id');
TIE.close(teneoEngineUrl, sessionId, () => {
const [, { headers }] = fetchMock.lastCall(expectedUrl);
expect(headers.get('Cookie')).to.equal(`JSESSIONID=${sessionId}`);
done();
});
});

it('should return a Promise if no callback is passed', async () => {
fetchMock.post(endSessionUrl, {});
const sessionId = 'D3CE28F6558DB88B8AFCE3F36E90920B';
const expectedUrl = urlWithSession(endSessionUrl, sessionId);

fetchMock.post(expectedUrl, {});

const request = TIE.close(teneoEngineUrl, 'session-id');
const request = TIE.close(teneoEngineUrl, sessionId);
expect(request).to.be.a('Promise');

await request;

const [, { headers }] = fetchMock.lastCall(endSessionUrl);
expect(headers.get('Cookie')).to.equal('JSESSIONID=session-id');
const [, { headers }] = fetchMock.lastCall(expectedUrl);
expect(headers.get('Cookie')).to.equal(`JSESSIONID=${sessionId}`);
});

it('should return a Promise if no sessionId and no callback are passed', async () => {
Expand All @@ -188,26 +212,32 @@ describe('TIE API Client', () => {

describe('#init', () => {
it('should return a version of the API with the Teneo Engine URL prefilled for #sendInput', (done) => {
fetchMock.post(teneoEngineUrl, 200);
const sessionId = 'D3CE28F6558DB88B8AFCE3F36E90920B';
const expectedUrl = urlWithSession(teneoEngineUrl, sessionId);

fetchMock.post(expectedUrl, 200);

const tieApi = TIE.init(teneoEngineUrl);

tieApi.sendInput('session-id', { text: 'Hej där!' }, () => {
const [, request] = fetchMock.lastCall(teneoEngineUrl);
tieApi.sendInput(sessionId, { text: 'Hej där!' }, () => {
const [, request] = fetchMock.lastCall(expectedUrl);
expect(querystring.parse(request.body)).to.eql(expectedPayload);
expect(request.headers.get('Cookie')).to.equal('JSESSIONID=session-id');
expect(request.headers.get('Cookie')).to.equal(`JSESSIONID=${sessionId}`);
done();
});
});

it('should return a version of the API with the Teneo Engine URL prefilled for #close', (done) => {
fetchMock.post(endSessionUrl, 200);
const sessionId = 'D3CE28F6558DB88B8AFCE3F36E90920B';
const expectedUrl = urlWithSession(endSessionUrl, sessionId);

fetchMock.post(expectedUrl, 200);

const tieApi = TIE.init(teneoEngineUrl);

tieApi.close('session-id', () => {
const [, { headers }] = fetchMock.lastCall(endSessionUrl);
expect(headers.get('Cookie')).to.equal('JSESSIONID=session-id');
tieApi.close(sessionId, () => {
const [, { headers }] = fetchMock.lastCall(expectedUrl);
expect(headers.get('Cookie')).to.equal(`JSESSIONID=${sessionId}`);
done();
});
});
Expand Down

0 comments on commit d0bbf7e

Please sign in to comment.