Skip to content

Commit 5ee5028

Browse files
committed
use correct insecure port, update tests
1 parent ecd0f12 commit 5ee5028

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mondago/integrator-api",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"license": "MIT",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/IntegratorAPI.test.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ beforeEach(() => {
1111
}
1212
})
1313

14+
// @ts-ignore
15+
global.WebSocket = jest.fn()
16+
const WebSocket: jest.Mock = global.WebSocket as any
17+
1418
global.fetch = jest.fn()
1519
const fetch: jest.Mock = global.fetch as any
1620

@@ -29,22 +33,40 @@ describe('IntegratorAPI', () => {
2933
it('should use port 10443 if `https` is set to true', async () => {
3034
config.https = true
3135
const integrator = new IntegratorAPI(config)
36+
3237
fetch.mockImplementationOnce((url) => {
3338
expect(new URL(url).port).toBe('10443')
3439
return new Response()
3540
})
3641

42+
WebSocket.mockImplementation((url) => {
43+
expect(new URL(url).port).toBe('10443')
44+
return { addEventListener: jest.fn() }
45+
})
46+
3747
await integrator.version()
48+
integrator.connectWS(() => {})
49+
50+
expect.assertions(2)
3851
})
39-
it('should use port 10080 if `https` is set to false', async () => {
52+
it('should use port 10088 if `https` is set to false', async () => {
4053
config.https = false
4154
const integrator = new IntegratorAPI(config)
4255

4356
fetch.mockImplementationOnce((url) => {
44-
expect(new URL(url).port).toBe('10080')
57+
expect(new URL(url).port).toBe('10088')
4558
return new Response()
4659
})
60+
61+
WebSocket.mockImplementation((url) => {
62+
expect(new URL(url).port).toBe('10088')
63+
return { addEventListener: jest.fn() }
64+
})
65+
4766
await integrator.version()
67+
integrator.connectWS(() => {})
68+
69+
expect.assertions(2)
4870
})
4971
it('should serialize data to `application/x-www-form-urlencoded` correctly', async () => {
5072
const integrator = new IntegratorAPI(config)

src/IntegratorAPI.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Integrator } from './types'
33

44
class IntegratorAPI {
55
private static SECURE_PORT = 10443
6-
private static INSECURE_PORT = 10080
6+
private static INSECURE_PORT = 10088
77
private static VERSION = 'v1'
88

99
private _headers = new Headers()
@@ -194,11 +194,9 @@ class IntegratorAPI {
194194
return this._post('Pickup', body)
195195
}
196196

197-
/**
198-
* Currently only supports WSS, with secure sockets enabled on the client
199-
*/
200197
public connectWS(eventHandler: Integrator.WebSocketEventHandler) {
201-
const url = `wss://localhost:${IntegratorAPI.SECURE_PORT}/api/${IntegratorAPI.VERSION}/events/${this._config.id}`
198+
const origin = this._config.https ? `wss://localhost:${IntegratorAPI.SECURE_PORT}` : `ws://localhost:${IntegratorAPI.INSECURE_PORT}`
199+
const url = origin + `/api/${IntegratorAPI.VERSION}/events/${this._config.id}`
202200
const ws = new WebSocket(url)
203201
ws.addEventListener('message', (e) => {
204202
const event = JSON.parse(e.data)

0 commit comments

Comments
 (0)