Skip to content

Commit 5f55d87

Browse files
committed
improve file url detection
1 parent 4481b98 commit 5f55d87

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/helpers/networkHelpers.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,21 @@ describe('networkHelpers.getRequestBody', () => {
400400
describe('networkHelpers.urlHasFileExtension', () => {
401401
it('returns true if the url ends with a file extension', () => {
402402
expect(urlHasFileExtension('http://example.com/test.js')).toBe(true)
403+
expect(urlHasFileExtension('http://example.com/style.css')).toBe(true)
404+
expect(urlHasFileExtension('http://example.com/file.png?query=test')).toBe(
405+
true
406+
)
403407
})
404408

405409
it('returns false if the url does not end with a file extension', () => {
406410
expect(urlHasFileExtension('http://example.com/test')).toBe(false)
411+
expect(urlHasFileExtension('http://test.example.com')).toBe(false)
412+
expect(urlHasFileExtension('http://test.example.com/test')).toBe(false)
413+
})
414+
415+
it('returns false for query params containing dots', () => {
416+
expect(urlHasFileExtension('http://example.com/test?query=query.ts')).toBe(
417+
false
418+
)
407419
})
408420
})

src/helpers/networkHelpers.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,19 @@ export interface IIncompleteNetworkRequest
5050
* @returns true if the url ends with a file extension (e.g. .js, .css, .png)
5151
*/
5252
export const urlHasFileExtension = (url: string): boolean => {
53-
// Check if URL ends with a file extension (e.g. .js, .css, .png)
53+
// Try to parse the url as a URL object
54+
try {
55+
const urlObject = new URL(url)
56+
if (urlObject.pathname.includes('.')) {
57+
return true
58+
} else {
59+
return false
60+
}
61+
} catch (e) {
62+
// noop
63+
}
64+
65+
// If parsing fails, just check the last part of the url
5466
const urlParts = url.split('/')
5567
const lastPart = urlParts[urlParts.length - 1]
5668
return lastPart.includes('.')

src/mocks/mock-requests.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,15 @@ export const mockRequests: IMockRequest[] = [
279279
request: [
280280
{
281281
query: `
282-
fragment NameParts on Person {
283-
firstName
284-
lastName
282+
fragment MovieDetails on Movie {
283+
id
284+
title
285+
genre
285286
}
286287
287288
query getMovieQuery($title: String) {
288289
getMovie(title: $title) {
289-
id
290-
title
291-
genre
290+
...MovieDetails
292291
}
293292
}
294293
`,

0 commit comments

Comments
 (0)