Skip to content

Commit

Permalink
fix(request): correct url as a string handling
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Aug 16, 2019
1 parent 1c566c5 commit 9b0fc87
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"esdoc-coverage-plugin": "^1.1.0",
"esdoc-flow-plugin": "^1.0.0",
"esdoc-standard-plugin": "^1.0.0",
"eslint-plugin-flowtype": "^3.12.2",
"eslint-plugin-flowtype": "^3.13.0",
"flow-bin": "^0.104.0",
"flow-coverage-report": "^0.6.1",
"flow-remove-types": "^2.104.0",
Expand Down
9 changes: 7 additions & 2 deletions src/main/js/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,15 @@ export class ReqOptions {
this.app = input.app || DEFAULT_APP

const headers = {}
const urlStr: string = input.url || 'http://localhost'
const urlStr: string = input.url || '/'
const urlOpts = input.host
? input
: { host: urlStr }
: url.parse(urlStr) // eslint-disable-line

// NOTE host & protocol are required by URL constructor
urlOpts.host = urlOpts.host || 'localhost'
urlOpts.protocol = urlOpts.protocol || 'http'

const urlData: IUrl = new url.URL(url.format(urlOpts))
const connection = assign(
{ encrypted: urlData.protocol === 'https:' },
Expand Down
2 changes: 1 addition & 1 deletion src/test/js/reqresnext.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('reqresnext', () => {
it('generates proper result map', () => {
const { req, res, next } = reqresnext({
method: 'GET',
url: 'http://example.com'
url: '/example.com'
})

expect(req).toEqual(expect.any(Object))
Expand Down
8 changes: 8 additions & 0 deletions src/test/js/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ describe('request', () => {
expect(req.host).toBe('foobar.com')
expect(req.protocol).toBe('http')
})

it('attaches default host and protocol values', () => {
const req = new Request({ url: '/example.com' })

expect(req.url).toBe('/example.com')
expect(req.host).toBe('localhost')
expect(req.protocol).toBe('http')
})
})

describe('headers', () => {
Expand Down

0 comments on commit 9b0fc87

Please sign in to comment.