forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dev): visualize network requests in the development environment …
…on Chrome Devtools's network tab (DIYgod#17308) * chore: add node-network-devtools to enhance development experience * feat(dev): visualize network requests in the development environment on Chrome Devtools's network tab * chore(deps): update node-network-devtools * fix: lock error when ci * chore: update node-network-devtools to v1.0.18 * chore: update node-network-devtools * chore: use node-network-devtools v1.0.20 * feat: use hook to add custom headers for fetch * fix: complex method 16 * test: add test for fetch * fix: test type error * fix: hard code * chore(cr): rename the fetch.spec.ts to fetch.test.ts and use exact version * chore: fix lock error ---------
- Loading branch information
Showing
5 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { getCurrentCell, setCurrentCell } from 'node-network-devtools'; | ||
import { useCustomHeader } from './fetch'; | ||
import { describe, beforeEach, afterEach, test, expect } from 'vitest'; | ||
|
||
const getInitRequest = () => | ||
({ | ||
requestHeaders: {} as Record<string, string>, | ||
id: '', | ||
loadCallFrames: () => {}, | ||
cookies: '', | ||
requestData: '', | ||
responseData: '', | ||
responseHeaders: {}, | ||
responseInfo: {}, | ||
}) satisfies NonNullable<ReturnType<typeof getCurrentCell>>['request']; | ||
|
||
enum Env { | ||
dev = 'dev', | ||
production = 'production', | ||
test = 'test', | ||
} | ||
|
||
describe('useCustomHeader', () => { | ||
let originalEnv: string; | ||
|
||
beforeEach(() => { | ||
originalEnv = process.env.NODE_ENV || Env.test; | ||
}); | ||
|
||
afterEach(() => { | ||
process.env.NODE_ENV = originalEnv; | ||
}); | ||
|
||
test('should register request with custom headers in dev environment', () => { | ||
process.env.NODE_ENV = Env.dev; | ||
|
||
const headers = new Headers(); | ||
const headerText = 'authorization'; | ||
const headerValue = 'Bearer token'; | ||
headers.set(headerText, headerValue); | ||
|
||
const req = getInitRequest(); | ||
setCurrentCell({ | ||
request: req, | ||
pipes: [], | ||
isAborted: false, | ||
}); | ||
|
||
useCustomHeader(headers); | ||
|
||
const cell = getCurrentCell(); | ||
expect(cell).toBeDefined(); | ||
|
||
let request = req; | ||
if (cell) { | ||
for (const { pipe } of cell.pipes) { | ||
request = pipe(request); | ||
} | ||
} | ||
|
||
expect(request.requestHeaders[headerText]).toEqual(headerValue); | ||
}); | ||
|
||
test('should not register request in non-dev environment', () => { | ||
process.env.NODE_ENV = Env.production; | ||
|
||
const headers = new Headers(); | ||
const headerText = 'content-type'; | ||
const headerValue = 'application/json'; | ||
|
||
headers.set(headerText, headerValue); | ||
const req = getInitRequest(); | ||
|
||
setCurrentCell({ | ||
request: req, | ||
pipes: [], | ||
isAborted: false, | ||
}); | ||
useCustomHeader(headers); | ||
|
||
const cell = getCurrentCell(); | ||
expect(cell).toBeDefined(); | ||
|
||
let request = req; | ||
if (cell) { | ||
for (const { pipe } of cell.pipes) { | ||
request = pipe(request); | ||
} | ||
} | ||
|
||
expect(req.requestHeaders[headerText]).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.