Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 14 additions & 5 deletions lib/utils/http_request_handler/request_handler.node.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2022, 2024, Optimizely
* Copyright 2022, 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -147,12 +147,21 @@ describe('NodeRequestHandler', () => {
scope.done();
});

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

const request = nodeRequestHandler.makeRequest(invalidHttpProtocolUrl, {}, 'get');
const request = nodeRequestHandler.makeRequest(httpProtocolUrl, {}, 'get');
const response = await request.responsePromise;

await expect(request.responsePromise).rejects.toThrow();
expect(response).toEqual({
statusCode: 200,
body,
headers: {},
});
scope.done();
});

it('should returns a rejected response promise when the URL protocol is unsupported', async () => {
Expand Down
11 changes: 6 additions & 5 deletions lib/utils/http_request_handler/request_handler.node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2022-2023 Optimizely
* Copyright 2022-2023, 2025 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,14 +46,15 @@ export class NodeRequestHandler implements RequestHandler {
makeRequest(requestUrl: string, headers: Headers, method: string, data?: string): AbortableRequest {
const parsedUrl = url.parse(requestUrl);

if (parsedUrl.protocol !== 'https:') {
if (parsedUrl.protocol !== 'https:' && parsedUrl.protocol !== 'http:') {
return {
responsePromise: Promise.reject(new OptimizelyError(UNSUPPORTED_PROTOCOL, parsedUrl.protocol)),
abort: () => {},
};
}

const request = https.request({
const requestModule = parsedUrl.protocol === 'https:' ? https : http;
const request = requestModule.request({
...this.getRequestOptionsFromUrl(parsedUrl),
method,
headers: {
Expand All @@ -77,9 +78,9 @@ export class NodeRequestHandler implements RequestHandler {
* Parses a URL into its constituent parts
* @param url URL object to parse
* @private
* @returns https.RequestOptions Standard request options dictionary
* @returns http.RequestOptions Standard request options dictionary compatible with both http and https
*/
private getRequestOptionsFromUrl(url: url.UrlWithStringQuery): https.RequestOptions {
private getRequestOptionsFromUrl(url: url.UrlWithStringQuery): http.RequestOptions {
return {
hostname: url.hostname,
path: url.path,
Expand Down
Loading