Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for query string forwarding #100

Merged
merged 7 commits into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion client/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const validator = require('validator')
const EventSource = require('eventsource')
const superagent = require('superagent')
const url = require('url')
const querystring = require('querystring')

class Client {
constructor ({ source, target, logger = console }) {
Expand All @@ -16,7 +18,13 @@ class Client {
onmessage (msg) {
const data = JSON.parse(msg.data)

const req = superagent.post(this.target).send(data.body)
let target = url.parse(this.target, true)
rpetti marked this conversation as resolved.
Show resolved Hide resolved
let mergedQuery = Object.assign(target.query, data.query)
rpetti marked this conversation as resolved.
Show resolved Hide resolved
target.search = querystring.stringify(mergedQuery)

delete data.query

const req = superagent.post(target).send(data.body)

delete data.body

Expand Down
19 changes: 19 additions & 0 deletions client/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ describe('client', () => {
// Send request to proxy server
await request(proxy).post(channel).send(payload).expect(200)
})

test('POST /:channel forwards query string to target url', async (done) => {
const queryParams = {
param1: 'testData1',
param2: 'testData2'
}

// Expect request to target
const forward = nock('http://example.com').post('/foo/bar').query(queryParams).reply(200)

// Test is done when this is called
client.addEventListener('message', (msg) => {
expect(forward.isDone()).toBe(true)
done()
})

// Send request to proxy server with query string
await request(proxy).post(channel).query(queryParams).send()
})
})

describe('createChannel', () => {
Expand Down
1 change: 1 addition & 0 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ module.exports = (testRoute) => {
events.emit(req.params.channel, {
...req.headers,
body: req.body,
query: req.query,
timestamp: Date.now()
})
res.status(200).end()
Expand Down