Skip to content

Commit

Permalink
Add more FetchFunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
sgohlke committed Oct 14, 2024
1 parent 4503613 commit b85d25b
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,16 @@ The following core functions can be used to create a new function or create dyna
The following functions return a fix value. This can be helpful for testing code with different scenarios.

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

## Contact
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dreamit/funpara",
"version": "1.2.0",
"version": "1.3.0",
"description": "Function parameter library for coding and testing",
"scripts": {
"build": "tsup-node",
Expand Down
54 changes: 53 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ export function fixedResponseFetchFunction(
Promise.resolve(new Response(bodyInit, init))
}

/**
* FetchFunction that returns a fixed Response with and empty body and status 400 (Bad Request).
*/
export const badRequestFetchFunction = fixedResponseFetchFunction(undefined, {
status: 400,
})

/**
* FetchFunction that returns a fixed Response with and empty body and status 404 (Not Found).
*/
Expand All @@ -74,7 +81,15 @@ export const notFoundFetchFunction: FetchFunction = fixedResponseFetchFunction(
)

/**
* FetchFunction that returns a fixed Response with broken JSON (missing bracket)
* FetchFunction that returns a fixed Response with and empty body and status 500 (Internal Server Error).
*/
export const internalServerErrorFetchFunction = fixedResponseFetchFunction(
undefined,
{ status: 500 },
)

/**
* FetchFunction that returns a fixed Response with broken JSON (missing bracket)
*/
export const brokenJSONFetchFunction: FetchFunction =
fixedResponseFetchFunction('{"data": {"message": "Missing bracket"}', {
Expand All @@ -100,6 +115,43 @@ export const timeoutFetchFunction: FetchFunction = (): Promise<Response> =>
throw new Error('Connection failed ETIMEDOUT')
})

/**
* FetchFunction that returns a fixed Response with an AggregateError.
*/
export const aggregateErrorFetchFunction = fixedResponseFetchFunction(
'{"errors":[{"message":"aaa The first error!, The second error!", "originalError": {"errors": [{"message":"The first error!"}, {"message":"The second error!"} ] } }]}',
{
headers: { 'Content-Type': 'application/json' },
status: 200,
},
)

/**
* FetchFunction that returns a fixed Response that GraphQL introspection is disabled.
*/
export const graphQLIntrospectionDisabledFetchFunction =
fixedResponseFetchFunction(
'{"errors": [ { "message": "Introspection is disabled"}],"data": null}',
{ status: 200 },
)

/**
* FetchFunction that returns a fixed Response with an invalid GraphQL schema.
*/
export const graphQLInvalidSchemaFetchFunction = fixedResponseFetchFunction(
'{"data": {"__schema":"NotAGraphQLSchema", ' +
'"_service": {"sdl":"NotAGraphQLSchema"}}}',
{ status: 200 },
)

/**
* FetchFunction that returns a fixed Response with an invalid GraphQL body.
*/
export const graphQLInvalidBodyFetchFunction = fixedResponseFetchFunction(
'{"message": "I am not GraphQL!"}',
{ status: 200 },
)

// Exit function related types and functions

/**
Expand Down
42 changes: 40 additions & 2 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import type {
TimeoutFunction,
} from '@/index'
import {
aggregateErrorFetchFunction,
badRequestFetchFunction,
brokenJSONFetchFunction,
doNotExitFunction,
fixedDateFunction,
fixedResponseFetchFunction,
graphQLIntrospectionDisabledFetchFunction,
graphQLInvalidBodyFetchFunction,
graphQLInvalidSchemaFetchFunction,
internalServerErrorFetchFunction,
noCallbackTimeoutFunction,
notFoundFetchFunction,
nowDateFunction,
Expand Down Expand Up @@ -66,7 +72,7 @@ async function getUserById(
`https://localhost:3000/users/${id}`,
)
if (!userDataResponse.ok) {
return 'User was not found'
return `User was not found. Status is ${userDataResponse.status}`
}
return `User ${await userDataResponse.text()}`
} catch (error) {
Expand Down Expand Up @@ -137,16 +143,48 @@ test('Test fetch functions', async () => {
),
).toBe('User John Doe')

// Test that badRequestFetchFunction is handled properly
expect(await getUserById('1', badRequestFetchFunction)).toBe(
'User was not found. Status is 400',
)

// Test that notFoundFetchFunction is handled properly
expect(await getUserById('1', notFoundFetchFunction)).toBe(
'User was not found',
'User was not found. Status is 404',
)

// Test that internalServerErrorFetchFunction is handled properly
expect(await getUserById('1', internalServerErrorFetchFunction)).toBe(
'User was not found. Status is 500',
)

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

// Test that aggregateErrorFetchFunction is handled properly
expect(await getUserById('1', aggregateErrorFetchFunction)).toBe(
'User {"errors":[{"message":"aaa The first error!, The second error!", "originalError": {"errors": [{"message":"The first error!"}, {"message":"The second error!"} ] } }]}',
)

// Test that graphQLIntrospectionDisabledFetchFunction is handled properly
expect(
await getUserById('1', graphQLIntrospectionDisabledFetchFunction),
).toBe(
'User {"errors": [ { "message": "Introspection is disabled"}],"data": null}',
)

// Test that graphQLInvalidSchemaFetchFunction is handled properly
expect(await getUserById('1', graphQLInvalidSchemaFetchFunction)).toBe(
'User {"data": {"__schema":"NotAGraphQLSchema", "_service": {"sdl":"NotAGraphQLSchema"}}}',
)

// Test that graphQLInvalidBodyFetchFunction is handled properly
expect(await getUserById('1', graphQLInvalidBodyFetchFunction)).toBe(
'User {"message": "I am not GraphQL!"}',
)

// Test that fixedResponseFetchFunction with a JSON response return the correct response
expect(
await getJSONMessage(
Expand Down

0 comments on commit b85d25b

Please sign in to comment.