Skip to content

Commit

Permalink
#235 Add fake origin header (#237)
Browse files Browse the repository at this point in the history
* #235 Add fake origin

* #235 Add fake origin - better test case
  • Loading branch information
alainrk authored Feb 15, 2023
1 parent dda00b4 commit 8de1566
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ function Request (options) {
if (('user-agent' in this.headers) === false) {
this.headers['user-agent'] = 'lightMyRequest'
}
if (('origin' in this.headers) === false) {
this.headers.origin = 'http://example.com:8080'
}
this.headers.host = this.headers.host || options.authority || hostHeaderFromURL(parsedURL)

if (options.cookies) {
Expand Down
29 changes: 28 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,37 @@ test('passes headers', (t) => {
})
})

test('passes fake origin header', (t) => {
t.plan(2)
const dispatch = function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end(req.headers.origin)
}

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello' }, (err, res) => {
t.error(err)
t.equal(res.payload, 'http://example.com:8080')
})
})

test('leave intact original origin header', (t) => {
t.plan(2)
const dispatch = function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end(req.headers.origin)
}

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello', headers: { origin: 'https://lightmyrequest' } }, (err, res) => {
t.error(err)
t.equal(res.payload, 'https://lightmyrequest')
})
})

test('request has rawHeaders', (t) => {
t.plan(3)
const dispatch = function (req, res) {
t.ok(Array.isArray(req.rawHeaders))
t.match(req.rawHeaders, ['super', 'duper', 'user-agent', 'lightMyRequest', 'host', 'example.com:8080'])
t.match(req.rawHeaders, ['super', 'duper', 'user-agent', 'lightMyRequest', 'origin', 'http://example.com:8080', 'host', 'example.com:8080'])
res.writeHead(200)
res.end()
}
Expand Down Expand Up @@ -1615,6 +1641,7 @@ test('correctly handles no string headers', (t) => {
true: 'true',
false: 'false',
host: 'example.com:8080',
origin: 'http://example.com:8080',
'user-agent': 'lightMyRequest'
})
})
Expand Down

0 comments on commit 8de1566

Please sign in to comment.