Skip to content

Commit

Permalink
fix on releasereleaseHandshake headers
Browse files Browse the repository at this point in the history
  • Loading branch information
adonisv79 committed Apr 9, 2021
1 parent 687df18 commit e5ddd01
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "request-swish",
"version": "1.4.0",
"version": "1.4.1",
"description": "Request client utilizing the swish protocol",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down
50 changes: 47 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,55 @@ describe('SwishClient.generateHandshake', () => {
})
})

describe('SwishClient.sendSwish', () => {
test('should be able to send a swish command', async () => {
mockedSwishClient.prototype.encryptRequest.mockReturnValueOnce({
headers: {
swishAction: '',
swishSessionId: 'adonisv79',
swishToken: 'thisissomevalidaesiv.thisissomevalidaeskey.thisissomevalidrsanextpublickey',
},
body: {
encBody: '',
isJson: false,
},
})
mockedSwishClient.prototype.decryptResponse.mockReturnValueOnce({ foo: 'bar' })
const r = await client.sendSwish({
data: {},
})
expect(r.swishResponseHeaders.swishAction).toEqual('mocked_response')
expect(r.swishResponseHeaders.swishSessionId).toEqual('a4c45c559590')
expect(r.swishResponse).toStrictEqual({ foo: 'bar' })
})
})

describe('SwishClient.releaseHandshake', () => {
test('', async () => {
test('should be able to destroy a session', async () => {
mockedSwishClient.prototype.encryptRequest.mockReturnValueOnce({
headers: {
swishAction: '',
swishSessionId: 'adonisv79',
swishToken: 'thisissomevalidaesiv.thisissomevalidaeskey.thisissomevalidrsanextpublickey',
},
body: {
encBody: '',
isJson: false,
},
})
mockedSwishClient.prototype.decryptResponse.mockReturnValueOnce({ logout_status: 'ok' })
const r = await client.sendSwish({
data: {},
})
expect(r.swishResponseHeaders.swishAction).toEqual('mocked_response')
expect(r.swishResponseHeaders.swishSessionId).toEqual('a4c45c559590')
expect(r.swishResponse).toStrictEqual({ logout_status: 'ok' })
})

test('should be able to destroy a session', async () => {
mockedSwishClient.prototype.encryptRequest.mockReturnValueOnce({
headers: {
swishAction: 'session_destroy',
swishAction: 'handshake_destroy',
swishSessionId: 'adonisv79',
swishToken: 'thisissomevalidaesiv.thisissomevalidaeskey.thisissomevalidrsanextpublickey',
},
Expand All @@ -67,7 +111,7 @@ describe('SwishClient.releaseHandshake', () => {
},
})
mockedSwishClient.prototype.decryptResponse.mockReturnValueOnce({ logout_status: 'ok' })
const r = await client.releaseHandshake()
const r = await client.releaseHandshake({ someData: 1 })
expect(r.swishResponseHeaders.swishAction).toEqual('mocked_response')
expect(r.swishResponseHeaders.swishSessionId).toEqual('a4c45c559590')
expect(r.swishResponse).toStrictEqual({ logout_status: 'ok' })
Expand Down
24 changes: 11 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,19 @@ export default class RequestSwishClient {
}

/** Ends the swish handshake connection with the server and clears the session */
async releaseHandshake(): Promise<SwishResponse> {
async releaseHandshake(data: Record<string, unknown>): Promise<SwishResponse> {
const options: AxiosRequestConfig = {
method: this.handshakeKillConfig.method,
responseType: 'json',
url: this.handshakeKillConfig.url,
}
const response = await this.sendSwish(options)
const swish = this.client.encryptRequest(data)
const swishHeaders: SwishHttpHeaders = {
'swish-action': 'handshake_destroy',
'swish-sess-id': swish.headers.swishSessionId,
'swish-token': swish.headers.swishToken,
}
const response = await this.handleRequest(options, swishHeaders, swish.body)
this.sessionId = ''
return response
}
Expand All @@ -94,7 +100,6 @@ export default class RequestSwishClient {
*/
async sendSwish(options: AxiosRequestConfig): Promise<SwishResponse> {
const swish = this.client.encryptRequest(options.data)
// run the request. we don't use async await coz request-promise uses bluebird
const swishHeaders: SwishHttpHeaders = {
'swish-action': swish.headers.swishAction,
'swish-sess-id': swish.headers.swishSessionId,
Expand All @@ -104,17 +109,10 @@ export default class RequestSwishClient {
return response
}

private async handleRequest(options: AxiosRequestConfig, swishHeaders?: SwishHttpHeaders, swishBody?: SwishBody): Promise<SwishResponse> {
private async handleRequest(options: AxiosRequestConfig, swishHeaders: SwishHttpHeaders, swishBody: SwishBody): Promise<SwishResponse> {
const retVal: Partial<SwishResponse> = {}

if (swishHeaders || swishBody) { // if any exists...
if (!(swishHeaders && swishBody)) { // but not both
throw new Error('SWISH_REQUEST_INVALID_FORMAT')
} else {
options.headers = { ...options.headers, ...swishHeaders } // merge the headers...
options.data = swishBody // them override the entire body with the encrypted version
}
}
options.headers = { ...options.headers, ...swishHeaders } // merge the headers...
options.data = swishBody // them override the entire body with the encrypted version

const response = await axios(options)
if (RequestSwishClient.validateSwishResponse(response)) {
Expand Down
12 changes: 8 additions & 4 deletions tools/test-client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* eslint-disable no-console */
import RequestSwishClient, { HTTPRequestConfig } from '../src/index'

const SERVER_URL = 'http://localhost:3000'
const SERVER_URL = 'http://localhost:3000/sapi'
const httpStartHandshake: HTTPRequestConfig = {
method: 'POST',
url: `${SERVER_URL}/auth/handshake`,
url: SERVER_URL,
}
const httpKillHandshake: HTTPRequestConfig = {
method: 'DELETE',
url: `${SERVER_URL}/auth/handshake`,
url: SERVER_URL,
}
const swishClient = new RequestSwishClient(httpStartHandshake, httpKillHandshake)

Expand Down Expand Up @@ -42,7 +42,7 @@ async function testRequest(path: string, data: Record<string, unknown>) {

async function testDestroySession() {
console.log('Destroying handshake session...')
const r = await swishClient.releaseHandshake()
const r = await swishClient.releaseHandshake({ reason: 'testing' })
console.log(r.swishResponse)
}

Expand All @@ -57,6 +57,10 @@ async function test() {
await testDestroySession()
// try an illegal access now session is destoryed
await testRequest('test/success', { action: 'move', message: 'Japan', passcode: 'whereami' })
// try to create a new handshake and connect again
console.log('Reconnecting and try again')
await testHandShake()
await testRequest('test/success', { action: 'move', message: 'Japan', passcode: 'whereami' })
} catch (err) {
console.error(err.message)
}
Expand Down

0 comments on commit e5ddd01

Please sign in to comment.