-
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.
Removes unnecessary code in tests and case for general and custom err…
…or handling
- Loading branch information
1 parent
3f7d507
commit 52b04b9
Showing
4 changed files
with
124 additions
and
104 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 was deleted.
Oops, something went wrong.
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,103 @@ | ||
import Endpoint from "../src/endpoints/Endpoint"; | ||
import {SuccessfulApiResponse} from "../index"; | ||
import ApiClient from "../src/ApiClient"; | ||
import ApiResponseErrorHandler from "../src/errors/ApiResponseErrorHandler"; | ||
import AuthenticationErrorResponse from "../src/responses/generalResponses/AuthenticationErrorResponse"; | ||
|
||
import {expect, test} from 'vitest' | ||
import {DummyRequester} from "./utils/DummyRequester.js"; | ||
|
||
class ExampleEndpoint extends Endpoint { | ||
url() { | ||
return "example/example"; | ||
} | ||
|
||
method() { | ||
return "GET"; | ||
} | ||
|
||
needsAuthorization() { | ||
return false; | ||
} | ||
|
||
responses() { | ||
return [SuccessfulApiResponse]; | ||
} | ||
|
||
} | ||
|
||
|
||
class ExampleApiClient extends ApiClient { | ||
exampleEndpoint(customErrorHandler) { | ||
const endpoint = new ExampleEndpoint(); | ||
return this._callEndpoint(endpoint, {}, customErrorHandler); | ||
} | ||
} | ||
|
||
test('Test general error handling can be set for api client', async () => { | ||
// Given a client that is not authenticated | ||
const requester = new DummyRequester(); | ||
requester.setExpectedResponses( | ||
new AuthenticationErrorResponse( | ||
{ | ||
"object": null, | ||
"errors": [ | ||
{ | ||
"code": "authentication_error", | ||
"text": "" | ||
} | ||
] | ||
} | ||
)); | ||
|
||
// And a call to an endpoint that needs authentication | ||
const generalErrorHandler = new ApiResponseErrorHandler().handlesError( | ||
AuthenticationErrorResponse, | ||
(request) => { | ||
return 'general error handler' | ||
}, | ||
); | ||
const client = new ExampleApiClient(requester, generalErrorHandler); | ||
const response = await client.exampleEndpoint(); | ||
|
||
// Then the response is handled by the general error handler | ||
expect(response).toBe('general error handler') | ||
}); | ||
|
||
test('Test general error can be overridden for call in api client', async () => { | ||
// Given a client that is not authenticated | ||
const requester = new DummyRequester(); | ||
requester.setExpectedResponses( | ||
new AuthenticationErrorResponse( | ||
{ | ||
"object": null, | ||
"errors": [ | ||
{ | ||
"code": "authentication_error", | ||
"text": "" | ||
} | ||
] | ||
} | ||
)); | ||
|
||
// And a call to an endpoint that needs authentication | ||
const generalErrorHandler = new ApiResponseErrorHandler().handlesError( | ||
AuthenticationErrorResponse, | ||
(request) => { | ||
return 'general error handler' | ||
}, | ||
); | ||
const client = new ExampleApiClient(requester, generalErrorHandler); | ||
|
||
// but has a custom error handler for it | ||
const customErrorHandler = new ApiResponseErrorHandler().handlesError( | ||
AuthenticationErrorResponse, | ||
(request) => { | ||
return 'custom error handler' | ||
}, | ||
); | ||
const response = await client.exampleEndpoint(customErrorHandler); | ||
|
||
// Then the response is handled by the custom error handler | ||
expect(response).toBe('custom error handler') | ||
}); |
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,16 @@ | ||
import {Requester} from "../../src/requester/Requester.js"; | ||
|
||
export class DummyRequester extends Requester { | ||
constructor(expectedResponses) { | ||
super(); | ||
this._expectedResponses = expectedResponses; | ||
} | ||
|
||
setExpectedResponses(expectedResponses) { | ||
this._expectedResponses = expectedResponses; | ||
} | ||
|
||
call({endpoint, data = undefined}) { | ||
return this._expectedResponses; | ||
} | ||
} |