Skip to content

Commit 5951829

Browse files
authored
[FSSDK-12027] support http in NodeRequestHandler (#1104)
1 parent 8cbfb3d commit 5951829

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

lib/utils/http_request_handler/request_handler.node.spec.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022, 2024, Optimizely
2+
* Copyright 2022, 2024-2025, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -147,12 +147,21 @@ describe('NodeRequestHandler', () => {
147147
scope.done();
148148
});
149149

150-
it('should throw error for a URL with http protocol (not https)', async () => {
151-
const invalidHttpProtocolUrl = 'http://some.example.com';
150+
it('should handle a URL with http protocol', async () => {
151+
const httpProtocolUrl = 'http://some.example.com';
152+
const scope = nock(httpProtocolUrl)
153+
.get('/')
154+
.reply(200, body);
152155

153-
const request = nodeRequestHandler.makeRequest(invalidHttpProtocolUrl, {}, 'get');
156+
const request = nodeRequestHandler.makeRequest(httpProtocolUrl, {}, 'get');
157+
const response = await request.responsePromise;
154158

155-
await expect(request.responsePromise).rejects.toThrow();
159+
expect(response).toEqual({
160+
statusCode: 200,
161+
body,
162+
headers: {},
163+
});
164+
scope.done();
156165
});
157166

158167
it('should returns a rejected response promise when the URL protocol is unsupported', async () => {

lib/utils/http_request_handler/request_handler.node.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022-2023 Optimizely
2+
* Copyright 2022-2023, 2025 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,14 +46,15 @@ export class NodeRequestHandler implements RequestHandler {
4646
makeRequest(requestUrl: string, headers: Headers, method: string, data?: string): AbortableRequest {
4747
const parsedUrl = url.parse(requestUrl);
4848

49-
if (parsedUrl.protocol !== 'https:') {
49+
if (parsedUrl.protocol !== 'https:' && parsedUrl.protocol !== 'http:') {
5050
return {
5151
responsePromise: Promise.reject(new OptimizelyError(UNSUPPORTED_PROTOCOL, parsedUrl.protocol)),
5252
abort: () => {},
5353
};
5454
}
5555

56-
const request = https.request({
56+
const requestModule = parsedUrl.protocol === 'https:' ? https : http;
57+
const request = requestModule.request({
5758
...this.getRequestOptionsFromUrl(parsedUrl),
5859
method,
5960
headers: {
@@ -77,9 +78,9 @@ export class NodeRequestHandler implements RequestHandler {
7778
* Parses a URL into its constituent parts
7879
* @param url URL object to parse
7980
* @private
80-
* @returns https.RequestOptions Standard request options dictionary
81+
* @returns http.RequestOptions Standard request options dictionary compatible with both http and https
8182
*/
82-
private getRequestOptionsFromUrl(url: url.UrlWithStringQuery): https.RequestOptions {
83+
private getRequestOptionsFromUrl(url: url.UrlWithStringQuery): http.RequestOptions {
8384
return {
8485
hostname: url.hostname,
8586
path: url.path,

0 commit comments

Comments
 (0)