-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #720 from snyk/fix/broker-var-sub-in-universal-broker
fix: body and header var sub in universal mode
- Loading branch information
Showing
3 changed files
with
393 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
const PORT = 8001; | ||
process.env.BROKER_SERVER_URL = `http://localhost:${PORT}`; | ||
|
||
jest.mock('../../lib/common/http/request'); | ||
import { WebSocketConnection } from '../../lib/client/types/client'; | ||
import { makeRequestToDownstream } from '../../lib/common/http/request'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const mockedFn = makeRequestToDownstream.mockImplementation((data) => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
return data; | ||
}); | ||
|
||
import { forwardWebSocketRequest as relay } from '../../lib/common/relay/forwardWebsocketRequest'; | ||
import { | ||
LoadedClientOpts, | ||
LoadedServerOpts, | ||
} from '../../lib/common/types/options'; | ||
|
||
const dummyWebsocketHandler: WebSocketConnection = { | ||
destroy: () => { | ||
return; | ||
}, | ||
latency: 0, | ||
options: { | ||
ping: 0, | ||
pong: 0, | ||
queueSize: Infinity, | ||
reconnect: '', | ||
stategy: '', | ||
timeout: 100, | ||
transport: '', | ||
}, | ||
send: () => {}, | ||
serverId: '0', | ||
socket: {}, | ||
supportedIntegrationType: 'github', | ||
transport: '', | ||
url: '', | ||
on: () => {}, | ||
readyState: 3, | ||
}; | ||
|
||
const dummyLoadedFilters = { | ||
private: () => { | ||
return { url: '/', auth: '', stream: true }; | ||
}, | ||
public: () => { | ||
return { url: '/', auth: '', stream: true }; | ||
}, | ||
}; | ||
|
||
describe('body relay', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterAll(() => { | ||
delete process.env.BROKER_SERVER_URL; | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('relay swaps body values found in BROKER_VAR_SUB', (done) => { | ||
expect.hasAssertions(); | ||
|
||
const brokerToken = 'test-broker'; | ||
|
||
const config = { | ||
universalBrokerEnabled: true, | ||
connections: { | ||
myconn: { | ||
identifier: brokerToken, | ||
HOST: 'localhost', | ||
PORT: '8001', | ||
}, | ||
}, | ||
}; | ||
const options: LoadedClientOpts | LoadedServerOpts = { | ||
filters: { | ||
private: [ | ||
{ | ||
method: 'any', | ||
url: '/*', | ||
}, | ||
], | ||
public: [], | ||
}, | ||
config, | ||
port: 8001, | ||
loadedFilters: dummyLoadedFilters, | ||
}; | ||
|
||
const route = relay(options, dummyWebsocketHandler)(brokerToken); | ||
|
||
const body = { | ||
BROKER_VAR_SUB: ['url'], | ||
url: '${HOST}:${PORT}/webhook', | ||
}; | ||
|
||
route( | ||
{ | ||
url: '/', | ||
method: 'POST', | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
body: Buffer.from(JSON.stringify(body)), | ||
headers: {}, | ||
}, | ||
() => { | ||
expect(makeRequestToDownstream).toHaveBeenCalledTimes(1); | ||
const arg = mockedFn.mock.calls[0][0]; | ||
expect(JSON.parse(arg.body).url).toEqual( | ||
`${config.connections.myconn.HOST}:${config.connections.myconn.PORT}/webhook`, | ||
); | ||
|
||
done(); | ||
}, | ||
); | ||
}); | ||
|
||
it('relay does NOT swap body values found in BROKER_VAR_SUB if disabled', (done) => { | ||
expect.hasAssertions(); | ||
|
||
const brokerToken = 'test-broker'; | ||
|
||
const config = { | ||
universalBrokerEnabled: true, | ||
connections: { | ||
myconn: { | ||
identifier: brokerToken, | ||
HOST: 'localhost', | ||
PORT: '8001', | ||
}, | ||
}, | ||
disableBodyVarsSubstitution: true, | ||
brokerServerUrl: 'http://localhost:8001', | ||
}; | ||
|
||
const options: LoadedClientOpts | LoadedServerOpts = { | ||
filters: { | ||
private: [ | ||
{ | ||
method: 'any', | ||
url: '/*', | ||
}, | ||
], | ||
public: [], | ||
}, | ||
config, | ||
port: 8001, | ||
loadedFilters: dummyLoadedFilters, | ||
}; | ||
const route = relay(options, dummyWebsocketHandler)(brokerToken); | ||
|
||
const body = { | ||
BROKER_VAR_SUB: ['url'], | ||
url: '${HOST}:${PORT}/webhook', | ||
}; | ||
|
||
route( | ||
{ | ||
url: '/', | ||
method: 'POST', | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
body: Buffer.from(JSON.stringify(body)), | ||
headers: {}, | ||
}, | ||
() => { | ||
expect(makeRequestToDownstream).toHaveBeenCalledTimes(1); | ||
const arg = mockedFn.mock.calls[0][0]; | ||
expect(JSON.parse(arg.body).url).toEqual('${HOST}:${PORT}/webhook'); | ||
|
||
done(); | ||
}, | ||
); | ||
}); | ||
}); |
Oops, something went wrong.