-
Notifications
You must be signed in to change notification settings - Fork 1k
test(instrumentation-http): avoid deprecated url.parse() #6102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; | |
| import { SemconvStability } from '@opentelemetry/instrumentation'; | ||
| import { extractHostnameAndPort } from '../../src/utils'; | ||
| import { AttributeNames } from '../../src/enums/AttributeNames'; | ||
| import { ParsedUrlQuery } from 'node:querystring'; | ||
|
|
||
| describe('Utility', () => { | ||
| describe('parseResponseStatus()', () => { | ||
|
|
@@ -80,7 +81,20 @@ describe('Utility', () => { | |
| describe('getRequestInfo()', () => { | ||
| it('should get options object', () => { | ||
| const webUrl = 'http://u:p@google.fr/aPath?qu=ry'; | ||
| const urlParsed = url.parse(webUrl); | ||
| const urlParsed = { | ||
| protocol: 'http:', | ||
| slashes: true, | ||
| auth: 'u:p', | ||
| host: 'google.fr', | ||
| port: null, | ||
| hostname: 'google.fr', | ||
| hash: null, | ||
| search: '?qu=ry', | ||
| query: 'qu=ry', | ||
| pathname: '/aPath', | ||
| path: '/aPath?qu=ry', | ||
| href: 'http://u:p@google.fr/aPath?qu=ry', | ||
| }; | ||
| const urlParsedWithoutPathname = { | ||
| ...urlParsed, | ||
| pathname: undefined, | ||
|
|
@@ -95,7 +109,7 @@ describe('Utility', () => { | |
| host: undefined, | ||
| port: null, | ||
| }; | ||
| const whatWgUrl = new url.URL(webUrl); | ||
| const whatWgUrl = new URL(webUrl); | ||
| for (const param of [ | ||
| webUrl, | ||
| urlParsed, | ||
|
|
@@ -155,12 +169,44 @@ describe('Utility', () => { | |
| describe('getAbsoluteUrl()', () => { | ||
| it('should return absolute url with localhost', () => { | ||
| const path = '/test/1'; | ||
| const result = utils.getAbsoluteUrl(url.parse(path), {}); | ||
| const result = utils.getAbsoluteUrl( | ||
| { | ||
| protocol: null, | ||
| slashes: null, | ||
| auth: null, | ||
| host: null, | ||
| port: null, | ||
| hostname: null, | ||
| hash: null, | ||
| search: null, | ||
| query: null as unknown as undefined, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated note - the types for
export type ParsedRequestOptions =
| (http.RequestOptions & Partial<url.UrlWithParsedQuery>)
| http.RequestOptions;And, the relevant Node core types are: // Output of `url.parse`
interface Url {
auth: string | null;
hash: string | null;
host: string | null;
hostname: string | null;
href: string;
path: string | null;
pathname: string | null;
protocol: string | null;
search: string | null;
slashes: boolean | null;
port: string | null;
query: string | null | ParsedUrlQuery;
}
interface UrlWithParsedQuery extends Url {
query: ParsedUrlQuery;
}
interface UrlWithStringQuery extends Url {
query: string | null;
}It's not clear to me why
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tired to do some archeology on this and it seems that this was added all the way back in #161 and has grown to be somewhat different since then. I think some cleanup is needed here (in a separate PR). |
||
| pathname: '/test/1', | ||
| path: '/test/1', | ||
| href: '/test/1', | ||
| }, | ||
| {} | ||
| ); | ||
| assert.strictEqual(result, `http://localhost${path}`); | ||
| }); | ||
| it('should return absolute url', () => { | ||
| const absUrl = 'http://www.google/test/1?query=1'; | ||
| const result = utils.getAbsoluteUrl(url.parse(absUrl), {}); | ||
| const result = utils.getAbsoluteUrl( | ||
| { | ||
| protocol: 'http:', | ||
| slashes: true, | ||
| auth: null, | ||
| host: 'www.google', | ||
| port: null, | ||
| hostname: 'www.google', | ||
| hash: null, | ||
| search: '?query=1', | ||
| query: 'query=1' as unknown as ParsedUrlQuery, | ||
| pathname: '/test/1', | ||
| path: '/test/1?query=1', | ||
| href: 'http://www.google/test/1?query=1', | ||
| }, | ||
| {} | ||
| ); | ||
| assert.strictEqual(result, absUrl); | ||
| }); | ||
| it('should return default url', () => { | ||
|
|
@@ -258,7 +304,11 @@ describe('Utility', () => { | |
| assert.strictEqual(utils.isValidOptionsType(options), false); | ||
| }); | ||
| }); | ||
| for (const options of ['url', url.parse('http://url.com'), {}]) { | ||
| for (const options of [ | ||
| 'url', | ||
| url.urlToHttpOptions(new URL('http://url.com')), | ||
| {}, | ||
| ]) { | ||
| it(`should return true with the following value: ${JSON.stringify( | ||
| options | ||
| )}`, () => { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.