-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/fetch #54
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
Merged
Merged
Feature/fetch #54
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
609a893
relocated request.library to common folder
magmacomputing 20f7199
add Tempo.epoch
magmacomputing e227a39
add shorthand to set, add, until, since
magmacomputing 11316f0
ready for review
magmacomputing 06522a2
PR 1st review
magmacomputing 877d176
tighten hh|mi|ss upper boundaries
magmacomputing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,129 @@ | ||
| import { fetchRequest, fetchHead, HttpError } from '../../src/common/request.library.js'; | ||
|
|
||
| describe('request.library', () => { | ||
| const mockFetch = vi.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| globalThis.fetch = mockFetch; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('fetchRequest', () => { | ||
| it('should parse JSON when Content-Type is application/json', async () => { | ||
| const mockData = { message: 'hello' }; | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: true, | ||
| headers: new Headers({ 'Content-Type': 'application/json; charset=utf-8' }), | ||
| json: async () => mockData | ||
| } as unknown as Response); | ||
|
|
||
| const result = await fetchRequest('https://example.com/api'); | ||
| expect(result).toEqual(mockData); | ||
| }); | ||
|
|
||
| it('should return raw text when Content-Type is not JSON', async () => { | ||
| const mockText = 'hello world'; | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: true, | ||
| headers: new Headers({ 'Content-Type': 'text/plain' }), | ||
| text: async () => mockText | ||
| } as unknown as Response); | ||
|
|
||
| const result = await fetchRequest('https://example.com/text'); | ||
| expect(result).toEqual(mockText); | ||
| }); | ||
|
|
||
| it('should strip prefix if provided', async () => { | ||
| const prefix = ')]}\'\n'; | ||
| const rawText = `${prefix}{"a":1}`; | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: true, | ||
| headers: new Headers({ 'Content-Type': 'application/json' }), | ||
| text: async () => rawText | ||
| } as unknown as Response); | ||
|
|
||
| const result = await fetchRequest('https://example.com/data', {}, { prefix }); | ||
| expect(result).toEqual({ a: 1 }); | ||
| }); | ||
|
|
||
| it('should throw HttpError with JSON body on 400', async () => { | ||
| const errorBody = { error: 'Bad request' }; | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: false, | ||
| status: 400, | ||
| statusText: 'Bad Request', | ||
| text: async () => JSON.stringify(errorBody) | ||
| } as unknown as Response); | ||
|
|
||
| try { | ||
| await fetchRequest('https://example.com/api'); | ||
| expect.fail('Should have thrown HttpError'); | ||
| } catch (err: any) { | ||
| expect(err).toBeInstanceOf(HttpError); | ||
| expect(err.status).toBe(400); | ||
| expect(err.statusText).toBe('Bad Request'); | ||
| expect(err.body).toEqual(errorBody); | ||
| } | ||
| }); | ||
|
|
||
| it('should throw HttpError with text body on 500 when JSON parsing fails', async () => { | ||
| const errorText = 'Internal Server Error Occurred'; | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: false, | ||
| status: 500, | ||
| statusText: 'Server Error', | ||
| text: async () => errorText | ||
| } as unknown as Response); | ||
|
|
||
| try { | ||
| await fetchRequest('https://example.com/api'); | ||
| expect.fail('Should have thrown HttpError'); | ||
| } catch (err: any) { | ||
| expect(err).toBeInstanceOf(HttpError); | ||
| expect(err.status).toBe(500); | ||
| expect(err.body).toBe(errorText); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe('fetchHead', () => { | ||
| it('should return status and headers on ok response', async () => { | ||
| const mockHeaders = new Headers({ 'Content-Length': '123' }); | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: true, | ||
| status: 200, | ||
| headers: mockHeaders | ||
| } as unknown as Response); | ||
|
|
||
| const result = await fetchHead('https://example.com/api'); | ||
| expect(result.status).toBe(200); | ||
| expect(result.headers).toBe(mockHeaders); | ||
| }); | ||
|
|
||
| it('should throw Error on non-ok response (except Forbidden)', async () => { | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: false, | ||
| status: 404, | ||
| statusText: 'Not Found' | ||
| } as unknown as Response); | ||
|
|
||
| await expect(fetchHead('https://example.com/api')).rejects.toThrow('404: Not Found'); | ||
| }); | ||
|
|
||
| it('should return status and headers on Forbidden (403)', async () => { | ||
| const mockHeaders = new Headers(); | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: false, | ||
| status: 403, | ||
| headers: mockHeaders | ||
| } as unknown as Response); | ||
|
|
||
| const result = await fetchHead('https://example.com/api'); | ||
| expect(result.status).toBe(403); | ||
| expect(result.headers).toBe(mockHeaders); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.