Skip to content

Commit

Permalink
Renames "urlToOptions" to "getRequestOptionsByUrl"
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Jun 13, 2020
1 parent 1742c6b commit e88c118
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/http/ClientRequest/normalizeHttpRequestParams.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RequestOptions } from 'https'
import { HttpRequestCallback } from '../../glossary'
import { urlToOptions } from '../../utils/urlToOptions'
import { getRequestOptionsByUrl } from '../../utils/getRequestOptionsByUrl'

const debug = require('debug')('http:normalize-http-request-params')

Expand All @@ -26,7 +26,7 @@ function resolveRequestOptions(
// Calling `fetch` provides only URL to ClientRequest,
// without RequestOptions or callback.
if (['function', 'undefined'].includes(typeof args[1])) {
return urlToOptions(url)
return getRequestOptionsByUrl(url)
}

return args[1] as RequestOptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { RequestOptions } from 'http'

/**
* Converts a URL instance into an ordinary object expected by
* the `http.request`/`https.request` calls.
* Converts a URL instance into the RequestOptions object expected by
* the `ClientRequest` class.
* @see https://github.com/nodejs/node/blob/908292cf1f551c614a733d858528ffb13fb3a524/lib/internal/url.js#L1257
*/
export function urlToOptions(url: URL): RequestOptions {
export function getRequestOptionsByUrl(url: URL): RequestOptions {
const options: RequestOptions = {
method: 'GET',
protocol: url.protocol,
Expand Down
55 changes: 42 additions & 13 deletions test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import https from 'https'
import http, { IncomingMessage, RequestOptions } from 'http'
import nodeFetch, { Response, RequestInfo, RequestInit } from 'node-fetch'
import { urlToOptions } from '../src/utils/urlToOptions'
import { getRequestOptionsByUrl } from '../src/utils/getRequestOptionsByUrl'
import { InterceptedRequest } from '../src/glossary'
import { getCleanUrl } from '../src/utils/getCleanUrl'

interface PromisifiedResponse {
res: IncomingMessage
resBody: string
url: string
options: RequestOptions
}
Expand All @@ -15,15 +16,22 @@ export function httpGet(
url: string,
options?: RequestOptions
): Promise<PromisifiedResponse> {
let resBody = ''
const parsedUrl = new URL(url)
const resolvedOptions = Object.assign({}, urlToOptions(parsedUrl), options)
const resolvedOptions = Object.assign(
{},
getRequestOptionsByUrl(parsedUrl),
options
)

return new Promise((resolve, reject) => {
http.get(resolvedOptions, (res) => {
res.setEncoding('utf8')
res.on('data', () => null)
res.on('data', (chunk) => (resBody += chunk))
res.on('error', reject)
res.on('end', () => resolve({ res, url, options: resolvedOptions }))
res.on('end', () =>
resolve({ res, resBody, url, options: resolvedOptions })
)
})
})
}
Expand All @@ -32,15 +40,22 @@ export function httpsGet(
url: string,
options?: RequestOptions
): Promise<PromisifiedResponse> {
let resBody = ''
const parsedUrl = new URL(url)
const resolvedOptions = Object.assign({}, urlToOptions(parsedUrl), options)
const resolvedOptions = Object.assign(
{},
getRequestOptionsByUrl(parsedUrl),
options
)

return new Promise((resolve, reject) => {
https.get(resolvedOptions, (res) => {
res.setEncoding('utf8')
res.on('data', () => null)
res.on('data', (chunk) => (resBody += chunk))
res.on('error', reject)
res.on('end', () => resolve({ res, url, options: resolvedOptions }))
res.on('end', () =>
resolve({ res, resBody, url, options: resolvedOptions })
)
})
})
}
Expand All @@ -50,15 +65,22 @@ export function httpRequest(
options?: RequestOptions,
body?: string
): Promise<PromisifiedResponse> {
let resBody = ''
const parsedUrl = new URL(url)
const resolvedOptions = Object.assign({}, urlToOptions(parsedUrl), options)
const resolvedOptions = Object.assign(
{},
getRequestOptionsByUrl(parsedUrl),
options
)

return new Promise((resolve, reject) => {
const req = http.request(resolvedOptions, (res) => {
res.setEncoding('utf8')
res.on('data', () => null)
res.on('data', (chunk) => (resBody += chunk))
res.on('error', reject)
res.on('end', () => resolve({ res, url, options: resolvedOptions }))
res.on('end', () =>
resolve({ res, resBody, url, options: resolvedOptions })
)
})

if (body) {
Expand All @@ -74,15 +96,22 @@ export function httpsRequest(
options?: RequestOptions,
body?: string
): Promise<PromisifiedResponse> {
let resBody = ''
const parsedUrl = new URL(url)
const resolvedOptions = Object.assign({}, urlToOptions(parsedUrl), options)
const resolvedOptions = Object.assign(
{},
getRequestOptionsByUrl(parsedUrl),
options
)

return new Promise((resolve, reject) => {
const req = https.request(resolvedOptions, (res) => {
res.setEncoding('utf8')
res.on('data', () => null)
res.on('data', (chunk) => (resBody += chunk))
res.on('error', reject)
res.on('end', () => resolve({ res, url, options: resolvedOptions }))
res.on('end', () =>
resolve({ res, resBody, url, options: resolvedOptions })
)
})

if (body) {
Expand Down

0 comments on commit e88c118

Please sign in to comment.