Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 3287c36

Browse files
authored
fix (datafile manager): Pass hostname and port in request options (optimizely#18)
Summary: Before this change, we were passing the host property returned from url.parse as the host option going into http.request (or https.request). The host property from url.parse includes the port when the URL contains a port. The problem is, request needs port to be passed as a separate option (not included as part of the host option). To fix, stop passing host, and replace it with hostname: url.hostname (where url.hostname excludes port), and port: url.port. References: https://nodejs.org/api/http.html#http_http_request_url_options_callback https://nodejs.org/api/url.html#url_urlobject_host https://nodejs.org/api/url.html#url_urlobject_hostname https://nodejs.org/api/url.html#url_urlobject_port Test plan: Added a regression test. Manually tested in @mikeng13's prototype datafile management branches for JS-testapp and compatibility suite.
1 parent c3b6ce7 commit 3287c36

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

packages/datafile-manager/__test__/nodeRequest.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,21 @@ describe('nodeEnvironment', () => {
140140
await expect(req.responsePromise).rejects.toThrow()
141141
scope.done()
142142
})
143+
144+
it('handles a url with a host and a port', async () => {
145+
const hostWithPort = 'http://datafiles:3000'
146+
const path = '/12/345.json'
147+
const scope = nock(hostWithPort)
148+
.get(path)
149+
.reply(200, '{"foo":"bar"}')
150+
const req = makeGetRequest(`${hostWithPort}${path}`, {})
151+
const resp = await req.responsePromise
152+
expect(resp).toEqual({
153+
statusCode: 200,
154+
body: '{"foo":"bar"}',
155+
headers: {}
156+
})
157+
scope.done()
158+
})
143159
})
144160
})

packages/datafile-manager/src/nodeRequest.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ type ClientRequestCreator = (options: http.RequestOptions) => http.ClientRequest
2424

2525
function getRequestOptionsFromUrl(url: url.UrlWithStringQuery): http.RequestOptions {
2626
return {
27-
protocol: url.protocol,
28-
host: url.host,
27+
hostname: url.hostname,
2928
path: url.path,
29+
port: url.port,
30+
protocol: url.protocol,
3031
}
3132
}
3233

0 commit comments

Comments
 (0)