Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit b85d25b

Browse files
committed
Add more FetchFunctions
1 parent 4503613 commit b85d25b

File tree

4 files changed

+100
-4
lines changed

4 files changed

+100
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,16 @@ The following core functions can be used to create a new function or create dyna
7979
The following functions return a fix value. This can be helpful for testing code with different scenarios.
8080

8181
- **testDateFunction**: DateFunction that returns a fixed test date '1001-01-01T00:00:00.000Z'
82+
- **badRequestFetchFunction**: FetchFunction that returns a fixed Response with and empty body and status 400 (Bad Request).
8283
- **notFoundFetchFunction**: FetchFunction that returns a fixed Response with and empty body and status 404 (Not Found).
84+
- **internalServerErrorFetchFunction**: FetchFunction that returns a fixed Response with and empty body and status 500 (Internal Server Error).
8385
- **brokenJSONFetchFunction**: FetchFunction that returns a fixed Response with broken JSON (missing bracket)
8486
- **unknownContentTypeFetchFunction**: FetchFunction that returns a fixed Response with an unknown Content-Type ('application/unknown')
8587
- **timeoutFetchFunction**: FetchFunction that returns a fixed Response that throws a Timeout error.
88+
- **aggregateErrorFetchFunction**: FetchFunction that returns a fixed Response with an AggregateError.
89+
- **graphQLIntrospectionDisabledFetchFunction**: FetchFunction that returns a fixed Response that GraphQL introspection is disabled.
90+
- **graphQLInvalidSchemaFetchFunction**: FetchFunction that returns a fixed Response with an invalid GraphQL schema.
91+
- **graphQLInvalidBodyFetchFunction**: FetchFunction that returns a fixed Response with an invalid GraphQL body.
8692
- **noCallbackTimeoutFunction**: TimeoutFunction that does not call the callback function but returns a fixed timeout ID 1.
8793

8894
## Contact

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dreamit/funpara",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "Function parameter library for coding and testing",
55
"scripts": {
66
"build": "tsup-node",

src/index.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ export function fixedResponseFetchFunction(
6565
Promise.resolve(new Response(bodyInit, init))
6666
}
6767

68+
/**
69+
* FetchFunction that returns a fixed Response with and empty body and status 400 (Bad Request).
70+
*/
71+
export const badRequestFetchFunction = fixedResponseFetchFunction(undefined, {
72+
status: 400,
73+
})
74+
6875
/**
6976
* FetchFunction that returns a fixed Response with and empty body and status 404 (Not Found).
7077
*/
@@ -74,7 +81,15 @@ export const notFoundFetchFunction: FetchFunction = fixedResponseFetchFunction(
7481
)
7582

7683
/**
77-
* FetchFunction that returns a fixed Response with broken JSON (missing bracket)
84+
* FetchFunction that returns a fixed Response with and empty body and status 500 (Internal Server Error).
85+
*/
86+
export const internalServerErrorFetchFunction = fixedResponseFetchFunction(
87+
undefined,
88+
{ status: 500 },
89+
)
90+
91+
/**
92+
* FetchFunction that returns a fixed Response with broken JSON (missing bracket)
7893
*/
7994
export const brokenJSONFetchFunction: FetchFunction =
8095
fixedResponseFetchFunction('{"data": {"message": "Missing bracket"}', {
@@ -100,6 +115,43 @@ export const timeoutFetchFunction: FetchFunction = (): Promise<Response> =>
100115
throw new Error('Connection failed ETIMEDOUT')
101116
})
102117

118+
/**
119+
* FetchFunction that returns a fixed Response with an AggregateError.
120+
*/
121+
export const aggregateErrorFetchFunction = fixedResponseFetchFunction(
122+
'{"errors":[{"message":"aaa The first error!, The second error!", "originalError": {"errors": [{"message":"The first error!"}, {"message":"The second error!"} ] } }]}',
123+
{
124+
headers: { 'Content-Type': 'application/json' },
125+
status: 200,
126+
},
127+
)
128+
129+
/**
130+
* FetchFunction that returns a fixed Response that GraphQL introspection is disabled.
131+
*/
132+
export const graphQLIntrospectionDisabledFetchFunction =
133+
fixedResponseFetchFunction(
134+
'{"errors": [ { "message": "Introspection is disabled"}],"data": null}',
135+
{ status: 200 },
136+
)
137+
138+
/**
139+
* FetchFunction that returns a fixed Response with an invalid GraphQL schema.
140+
*/
141+
export const graphQLInvalidSchemaFetchFunction = fixedResponseFetchFunction(
142+
'{"data": {"__schema":"NotAGraphQLSchema", ' +
143+
'"_service": {"sdl":"NotAGraphQLSchema"}}}',
144+
{ status: 200 },
145+
)
146+
147+
/**
148+
* FetchFunction that returns a fixed Response with an invalid GraphQL body.
149+
*/
150+
export const graphQLInvalidBodyFetchFunction = fixedResponseFetchFunction(
151+
'{"message": "I am not GraphQL!"}',
152+
{ status: 200 },
153+
)
154+
103155
// Exit function related types and functions
104156

105157
/**

tests/index.test.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ import type {
55
TimeoutFunction,
66
} from '@/index'
77
import {
8+
aggregateErrorFetchFunction,
9+
badRequestFetchFunction,
810
brokenJSONFetchFunction,
911
doNotExitFunction,
1012
fixedDateFunction,
1113
fixedResponseFetchFunction,
14+
graphQLIntrospectionDisabledFetchFunction,
15+
graphQLInvalidBodyFetchFunction,
16+
graphQLInvalidSchemaFetchFunction,
17+
internalServerErrorFetchFunction,
1218
noCallbackTimeoutFunction,
1319
notFoundFetchFunction,
1420
nowDateFunction,
@@ -66,7 +72,7 @@ async function getUserById(
6672
`https://localhost:3000/users/${id}`,
6773
)
6874
if (!userDataResponse.ok) {
69-
return 'User was not found'
75+
return `User was not found. Status is ${userDataResponse.status}`
7076
}
7177
return `User ${await userDataResponse.text()}`
7278
} catch (error) {
@@ -137,16 +143,48 @@ test('Test fetch functions', async () => {
137143
),
138144
).toBe('User John Doe')
139145

146+
// Test that badRequestFetchFunction is handled properly
147+
expect(await getUserById('1', badRequestFetchFunction)).toBe(
148+
'User was not found. Status is 400',
149+
)
150+
140151
// Test that notFoundFetchFunction is handled properly
141152
expect(await getUserById('1', notFoundFetchFunction)).toBe(
142-
'User was not found',
153+
'User was not found. Status is 404',
154+
)
155+
156+
// Test that internalServerErrorFetchFunction is handled properly
157+
expect(await getUserById('1', internalServerErrorFetchFunction)).toBe(
158+
'User was not found. Status is 500',
143159
)
144160

145161
// Test that timeoutFetchFunction is handled properly
146162
expect(await getUserById('1', timeoutFetchFunction)).toBe(
147163
'Error: Error: Connection failed ETIMEDOUT',
148164
)
149165

166+
// Test that aggregateErrorFetchFunction is handled properly
167+
expect(await getUserById('1', aggregateErrorFetchFunction)).toBe(
168+
'User {"errors":[{"message":"aaa The first error!, The second error!", "originalError": {"errors": [{"message":"The first error!"}, {"message":"The second error!"} ] } }]}',
169+
)
170+
171+
// Test that graphQLIntrospectionDisabledFetchFunction is handled properly
172+
expect(
173+
await getUserById('1', graphQLIntrospectionDisabledFetchFunction),
174+
).toBe(
175+
'User {"errors": [ { "message": "Introspection is disabled"}],"data": null}',
176+
)
177+
178+
// Test that graphQLInvalidSchemaFetchFunction is handled properly
179+
expect(await getUserById('1', graphQLInvalidSchemaFetchFunction)).toBe(
180+
'User {"data": {"__schema":"NotAGraphQLSchema", "_service": {"sdl":"NotAGraphQLSchema"}}}',
181+
)
182+
183+
// Test that graphQLInvalidBodyFetchFunction is handled properly
184+
expect(await getUserById('1', graphQLInvalidBodyFetchFunction)).toBe(
185+
'User {"message": "I am not GraphQL!"}',
186+
)
187+
150188
// Test that fixedResponseFetchFunction with a JSON response return the correct response
151189
expect(
152190
await getJSONMessage(

0 commit comments

Comments
 (0)