-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
110 additions
and
23 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
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,26 @@ | ||
module.exports = { | ||
*summary() { | ||
return 'The rule to replace websocket message'; | ||
}, | ||
|
||
*beforeSendWsMessageToClient(requestDetail) { | ||
const message = requestDetail.data; | ||
try { | ||
const messageObject = JSON.parse(message); | ||
if (messageObject.type === 'onMessage') { | ||
messageObject.content = 'replaced by beforeSendWsMessageToClient'; | ||
return { | ||
data: JSON.stringify(messageObject), | ||
} | ||
} | ||
} catch (err) { /* ignore error */ } | ||
|
||
return null; | ||
}, | ||
|
||
*beforeSendWsMessageToServer() { | ||
return { | ||
data: 'replaced by beforeSendWsMessageToServer', | ||
}; | ||
}, | ||
}; |
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,69 @@ | ||
const async = require('async'); | ||
const ProxyServerUtil = require('../util/ProxyServerUtil.js'); | ||
const TestServer = require('../server/server.js'); | ||
const { printLog } = require('../util/CommonUtil.js'); | ||
const { proxyWs, generateWsUrl } = require('../util/HttpUtil.js'); | ||
const rule = require('./rule/rule_replace_ws_message'); | ||
|
||
describe('Rule to replace the websocket message', () => { | ||
let testServer = null; | ||
let proxyServer = null; | ||
|
||
beforeAll((done) => { | ||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 50000; | ||
printLog('Start server for rule_replace_ws_message_spec'); | ||
|
||
testServer = new TestServer(); | ||
proxyServer = ProxyServerUtil.proxyServerWithRule(rule); | ||
|
||
setTimeout(done, 2000); | ||
}); | ||
|
||
afterAll(() => { | ||
testServer && testServer.close(); | ||
proxyServer && proxyServer.close(); | ||
printLog('Close server for rule_replace_ws_message_spec'); | ||
}); | ||
|
||
it('should replace websocket message from server', (done) => { | ||
async.mapSeries([ | ||
{ scheme: 'ws', masked: false }, | ||
{ scheme: 'ws', masked: true }, | ||
{ scheme: 'wss', masked: false }, | ||
{ scheme: 'wss', masked: true }, | ||
], (unit, callback) => { | ||
const url = generateWsUrl(unit.scheme, '/test/socket'); | ||
const wsClient = proxyWs(url); | ||
|
||
wsClient.on('open', () => { | ||
wsClient.send('test', unit.masked); | ||
}); | ||
|
||
wsClient.on('message', (message) => { | ||
// test beforeSendWsMessageToServer | ||
const requestRecord = testServer.getProxyRequestRecord(url); | ||
expect(requestRecord.messages[0]).toBe('replaced by beforeSendWsMessageToServer'); | ||
|
||
try { | ||
const result = JSON.parse(message); | ||
if (result.type === 'onMessage') { | ||
// test beforeSendWsMessageToClient | ||
expect(result.content).toBe('replaced by beforeSendWsMessageToClient'); | ||
callback(); | ||
} | ||
} catch (err) { /* ignore error */ } | ||
}); | ||
|
||
wsClient.on('error', (err) => { | ||
printLog('Error happened in proxy websocket'); | ||
callback(err); | ||
}); | ||
}, (err) => { | ||
if (err) { | ||
done.fail(err); | ||
} else { | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); |
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