This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
71 additions
and
22 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 |
---|---|---|
@@ -1,31 +1,76 @@ | ||
import { amazonMarketplaces, HttpClient, InvalidParameterValue } from '../../src' | ||
import { | ||
amazonMarketplaces, | ||
HttpClient, | ||
HttpError, | ||
InvalidParameterValue, | ||
MWSError, | ||
} from '../../src' | ||
import { Resource } from '../../src/http' | ||
import { getFixture } from '../utils' | ||
|
||
const httpClientThatThrows = (error: unknown) => | ||
new HttpClient( | ||
{ | ||
awsAccessKeyId: '', | ||
marketplace: amazonMarketplaces.CA, | ||
mwsAuthToken: '', | ||
secretKey: '', | ||
sellerId: '', | ||
}, | ||
() => { | ||
throw error | ||
}, | ||
) | ||
|
||
const mockRequest = { | ||
resource: Resource.Sellers, | ||
version: '', | ||
action: 'GetServiceStatus', | ||
parameters: {}, | ||
} as const | ||
|
||
describe('httpClient', () => { | ||
it('should throw a HttpError on failure', async () => { | ||
expect.assertions(1) | ||
|
||
const httpClient = new HttpClient( | ||
{ | ||
awsAccessKeyId: '', | ||
marketplace: amazonMarketplaces.CA, | ||
mwsAuthToken: '', | ||
secretKey: '', | ||
sellerId: '', | ||
}, | ||
() => { | ||
throw getFixture('error-response') | ||
}, | ||
const httpClient = httpClientThatThrows(getFixture('error-response')) | ||
|
||
await expect(() => httpClient.request('POST', mockRequest)).rejects.toStrictEqual( | ||
new InvalidParameterValue('GetServiceStatus request failed'), | ||
) | ||
}) | ||
|
||
it('should throw a HttpError that can be handled', async () => { | ||
expect.assertions(7) | ||
|
||
const httpClient = httpClientThatThrows(getFixture('error-response')) | ||
|
||
let expectedError!: InvalidParameterValue | ||
|
||
try { | ||
await httpClient.request('POST', mockRequest) | ||
} catch (error) { | ||
expectedError = error | ||
} | ||
|
||
await expect(() => | ||
httpClient.request('POST', { | ||
resource: Resource.Sellers, | ||
version: '', | ||
action: 'GetServiceStatus', | ||
parameters: {}, | ||
}), | ||
).rejects.toStrictEqual(new InvalidParameterValue('GetServiceStatus request failed')) | ||
expect(expectedError instanceof MWSError).toStrictEqual(true) | ||
expect(expectedError instanceof HttpError).toStrictEqual(true) | ||
expect(expectedError instanceof InvalidParameterValue).toStrictEqual(true) | ||
expect(expectedError.code).toStrictEqual('InvalidParameterValue') | ||
expect(expectedError.type).toStrictEqual('Sender') | ||
expect(expectedError.requestId).toStrictEqual('e26147f9-30cc-4379-9fb5-bd4ad966c48b') | ||
expect(expectedError.mwsMessage).toStrictEqual( | ||
'CreatedAfter or LastUpdatedAfter must be specified', | ||
) | ||
}) | ||
|
||
it('should propagate non-API errors', async () => { | ||
expect.assertions(1) | ||
|
||
const httpClient = httpClientThatThrows(new Error('Out of memory')) | ||
|
||
await expect(() => httpClient.request('POST', mockRequest)).rejects.toStrictEqual( | ||
new Error('Out of memory'), | ||
) | ||
}) | ||
}) |