From 77eb29bf7d920e40a74028ed989a5b21f2dc9ec4 Mon Sep 17 00:00:00 2001 From: Erno Date: Thu, 6 Oct 2022 20:48:06 +0200 Subject: [PATCH] fix: send body in delete requests --- .changeset/wicked-badgers-pretend.md | 6 + .../src/resolvers/EndpointResolver.ts | 12 +- .../RequestController/RequestController.ts | 4 +- .../mockRequests/streamApi/deleteAddress.ts | 73 ++++-- .../requestControllerDelete.test.ts | 231 ++++++++++++++++++ .../requestControllerPost.test.ts | 228 +++++++++++++++++ .../requestControllerPut.test.ts | 228 +++++++++++++++++ .../test/streamApi/deleteAddress.test.ts | 6 +- 8 files changed, 761 insertions(+), 27 deletions(-) create mode 100644 .changeset/wicked-badgers-pretend.md create mode 100644 packages/integration/test/core/requestController/requestControllerDelete.test.ts create mode 100644 packages/integration/test/core/requestController/requestControllerPost.test.ts create mode 100644 packages/integration/test/core/requestController/requestControllerPut.test.ts diff --git a/.changeset/wicked-badgers-pretend.md b/.changeset/wicked-badgers-pretend.md new file mode 100644 index 0000000000..e741a646e9 --- /dev/null +++ b/.changeset/wicked-badgers-pretend.md @@ -0,0 +1,6 @@ +--- +'@moralisweb3/api-utils': patch +'@moralisweb3/core': patch +--- + +Sent body in delete requests. This fixes issues with Moralis.Streams.deleteAddress diff --git a/packages/apiUtils/src/resolvers/EndpointResolver.ts b/packages/apiUtils/src/resolvers/EndpointResolver.ts index 09b968cd7f..4a7a920bd2 100644 --- a/packages/apiUtils/src/resolvers/EndpointResolver.ts +++ b/packages/apiUtils/src/resolvers/EndpointResolver.ts @@ -73,10 +73,16 @@ export class EndpointResolver(url, searchParams, { - headers: this.createHeaders(), - }); + const result = await this.requestController.delete>( + url, + searchParams, + bodyParams, + { + headers: this.createHeaders(), + }, + ); return new ApiResultAdapter(result, this.endpoint.apiToResult, this.endpoint.resultToJson, params); }; diff --git a/packages/core/src/controllers/RequestController/RequestController.ts b/packages/core/src/controllers/RequestController/RequestController.ts index 767b404ec6..654b54881a 100644 --- a/packages/core/src/controllers/RequestController/RequestController.ts +++ b/packages/core/src/controllers/RequestController/RequestController.ts @@ -137,9 +137,10 @@ export class RequestController { }); } - public async delete( + public async delete>( url: string, searchParams?: Record, + body?: Body, options?: RequestOptions, abortSignal?: AbortController['signal'], ): Promise { @@ -147,6 +148,7 @@ export class RequestController { url, params: searchParams, method: 'DELETE', + data: body, headers: options?.headers, signal: abortSignal, }); diff --git a/packages/integration/mockRequests/streamApi/deleteAddress.ts b/packages/integration/mockRequests/streamApi/deleteAddress.ts index a151ecafd9..bd7e2b4794 100644 --- a/packages/integration/mockRequests/streamApi/deleteAddress.ts +++ b/packages/integration/mockRequests/streamApi/deleteAddress.ts @@ -1,37 +1,60 @@ +import isEqual from 'lodash/isEqual'; +import isNil from 'lodash/isNil'; +import omitBy from 'lodash/omitBy'; import { rest } from 'msw'; import { STREAM_API_ROOT, MOCK_API_KEY } from '../config'; -const DeleteAddressResponse: Record = { - VALID_RESPONSE: { - data: { - streamId: 'VALID_RESPONSE', +const createResponse = (streamId: string, address: string) => ({ + streamId, + address, +}); + +const createErrorResponse = (message: string) => ({ + message, +}); + +// message: 'Validation Failed', +// details: { 'requestBody.address': { message: "'address' is required" } } + +const scenarios = [ + { + condition: { + id: 'VALID_RESPONSE', address: '0x992eccc191d6f74e8be187ed6b6ac196b08314f7', }, - status: 200, + responseStatus: 200, + response: createResponse('VALID_RESPONSE', '0x992eccc191d6f74e8be187ed6b6ac196b08314f7'), }, - INVALID_ADDRESS: { - data: { - message: 'Invalid Address: some-address', + { + condition: { + id: 'INVALID_ADDRESS', + address: 'some-address', }, - status: 400, + responseStatus: 400, + response: createErrorResponse('Invalid Address: some-address'), }, - STREAM_NOT_FOUND: { - data: { - message: 'Stream not found', + { + condition: { + id: 'STREAM_NOT_FOUND', + address: '0x992eccc191d6f74e8be187ed6b6ac196b08314f7', }, - status: 404, + responseStatus: 404, + response: createErrorResponse('Stream not found'), }, - ADDRESS_NOT_FOUND: { - data: { - message: 'Address not found', + { + condition: { + id: 'ADDRESS_NOT_FOUND', + address: '0x295522b61890c3672d12efbff4358a6411ce996f', }, - status: 404, + responseStatus: 404, + response: createErrorResponse('Address not found'), }, -}; +]; export const mockDeleteAddressEvm = rest.delete(`${STREAM_API_ROOT}/streams/evm/:id/address`, (req, res, ctx) => { const apiKey = req.headers.get('x-api-key'); const id = req.params.id as string; + const { address } = req.body as Record; if (apiKey !== MOCK_API_KEY) { return res( @@ -42,9 +65,17 @@ export const mockDeleteAddressEvm = rest.delete(`${STREAM_API_ROOT}/streams/evm/ ); } - if (DeleteAddressResponse[id]) { - return res(ctx.status(DeleteAddressResponse[id].status), ctx.json(DeleteAddressResponse[id].data)); + const params = omitBy({ id, address }, isNil); + + if (apiKey !== MOCK_API_KEY) { + return res(ctx.status(401)); + } + + const scenario = scenarios.find(({ condition }) => isEqual(condition, params)); + + if (scenario) { + return res(ctx.status(scenario.responseStatus), ctx.json(scenario.response)); } - throw new Error('addAddressEvm: Not supported scenario'); + throw new Error(`mockDeleteAddressEvm failed, no scenarios with: ${JSON.stringify(params)}`); }); diff --git a/packages/integration/test/core/requestController/requestControllerDelete.test.ts b/packages/integration/test/core/requestController/requestControllerDelete.test.ts new file mode 100644 index 0000000000..3d58d7f03a --- /dev/null +++ b/packages/integration/test/core/requestController/requestControllerDelete.test.ts @@ -0,0 +1,231 @@ +import { MoralisCore, RequestController } from '@moralisweb3/core'; +import { setupServer } from 'msw/node'; +import { rest } from 'msw'; + +const deleteJsonMock = rest.delete(`http://example.com/deleteJson`, (req, res, ctx) => { + return res( + ctx.status(200), + ctx.json({ + iAm: 'Batman', + }), + ); +}); + +const deleteTextMock = rest.delete(`http://example.com/deleteText`, (req, res, ctx) => { + return res(ctx.status(200), ctx.text('I am Batman')); +}); + +const delete400ErrorMock = rest.delete(`http://example.com/delete400Error`, (req, res, ctx) => { + return res( + ctx.status(400), + ctx.json({ + message: 'I am not Batman', + }), + ); +}); + +const delete404ErrorMock = rest.delete(`http://example.com/delete404Error`, (req, res, ctx) => { + return res( + ctx.status(404), + ctx.json({ + message: 'I am not Batman', + }), + ); +}); + +const delete500ErrorMock = rest.delete(`http://example.com/delete500Error`, (req, res, ctx) => { + return res( + ctx.status(500), + ctx.json({ + message: 'I am not Batman', + }), + ); +}); + +const delete503ErrorMock = rest.delete(`http://example.com/delete503Error`, (req, res, ctx) => { + return res( + ctx.status(503), + ctx.json({ + message: 'I am not Batman', + }), + ); +}); + +const delete400ErrorMultiMessageMock = rest.delete(`http://example.com/delete400MultiMessageError`, (req, res, ctx) => { + return res( + ctx.status(400), + ctx.json({ + message: ['I am not Batman', 'I am not superman'], + }), + ); +}); + +const delete400ErrorEmptyJsonMock = rest.delete(`http://example.com/delete400ErrorEmptyJson`, (req, res, ctx) => { + return res(ctx.status(400), ctx.json({})); +}); + +const delete400ErrorEmptyMock = rest.delete(`http://example.com/delete400ErrorEmpty`, (req, res, ctx) => { + return res(ctx.status(400)); +}); + +const deleteWithSearchParams = rest.delete(`http://example.com/deleteWithSearchParams`, (req, res, ctx) => { + const params = req.url.searchParams; + if (params.get('name') !== 'Batman' || params.get('age') !== '30') { + throw new Error(`Search Params are not being correctly sent. Got params: ${JSON.stringify(params)}`); + } + return res(ctx.status(200), ctx.json({ success: true })); +}); + +const deleteWithBodyParams = rest.delete(`http://example.com/deleteWithBodyParams`, (req, res, ctx) => { + const body = req.body as Record; + if (body.name !== 'Batman' || body.age !== '30') { + throw new Error(`Body Params are not being correctly sent. Got body: ${JSON.stringify(body)}`); + } + return res(ctx.status(200), ctx.json({ success: true })); +}); + +const deleteWithSearchAndBodyParams = rest.delete( + `http://example.com/deleteWithSearchAndBodyParams`, + (req, res, ctx) => { + const body = req.body as Record; + const params = req.url.searchParams; + + if (params.get('name') !== 'Batman' || body.age !== '30') { + throw new Error( + `Params are not being correctly sent. Got body: ${JSON.stringify(body)}, Got params: ${JSON.stringify(params)}`, + ); + } + return res(ctx.status(200), ctx.json({ success: true })); + }, +); + +const handlers = [ + deleteJsonMock, + deleteTextMock, + delete400ErrorMock, + delete404ErrorMock, + delete500ErrorMock, + delete503ErrorMock, + delete400ErrorMultiMessageMock, + delete400ErrorEmptyJsonMock, + delete400ErrorEmptyMock, + deleteWithSearchParams, + deleteWithBodyParams, + deleteWithSearchAndBodyParams, +]; + +const mockServer = setupServer(...handlers); + +describe('RequestControllerDelete', () => { + let requestController: RequestController; + + beforeAll(() => { + const core = MoralisCore.create(); + requestController = RequestController.create(core); + + mockServer.listen({ + onUnhandledRequest: 'warn', + }); + }); + + afterAll(() => { + mockServer.close(); + }); + + beforeEach(() => {}); + + afterEach(async () => { + /** + * Prevent issue with Jest and MSW when running multiple tests, + * where tests are finished before all requests are resolved + * https://github.com/mswjs/msw/issues/474 + */ + await new Promise((resolve) => setTimeout(resolve.bind(null), 0)); + }); + + it('should delete a valid Json response', async () => { + const result = await requestController.delete('http://example.com/deleteJson'); + + expect(result).toStrictEqual({ iAm: 'Batman' }); + }); + + it('should delete a valid text response', async () => { + const result = await requestController.delete('http://example.com/deleteText'); + + expect(result).toStrictEqual('I am Batman'); + }); + + it('should throw an error on 400 response', async () => { + expect(requestController.delete('http://example.com/delete400Error')).rejects.toThrowError( + '[C0006] Request failed, Bad Request(400): I am not Batman', + ); + }); + + it('should throw an error on 404 response', async () => { + expect(requestController.delete('http://example.com/delete404Error')).rejects.toThrowError( + '[C0006] Request failed, Not Found(404): I am not Batman', + ); + }); + + it('should throw an error on 500 response', async () => { + expect(requestController.delete('http://example.com/delete500Error')).rejects.toThrowError( + '[C0006] Request failed, Internal Server Error(500): I am not Batman', + ); + }); + + it('should throw an error on 503 response', async () => { + expect(requestController.delete('http://example.com/delete503Error')).rejects.toThrowError( + '[C0006] Request failed, Service Unavailable(503): I am not Batman', + ); + }); + + it('should handle multiple messages in an error response', async () => { + expect(requestController.delete('http://example.com/delete400MultiMessageError')).rejects.toThrowError( + '[C0006] Request failed, Bad Request(400): I am not Batman, I am not superman', + ); + }); + + it('should handle empty error response', async () => { + expect(requestController.delete('http://example.com/delete400ErrorEmptyJson')).rejects.toThrowError( + '[C0006] Request failed, Bad Request(400): Unknown error (no error info returned from API)', + ); + }); + + it('should handle empty error response', async () => { + expect(requestController.delete('http://example.com/delete400ErrorEmpty')).rejects.toThrowError( + '[C0006] Request failed, Bad Request(400): Unknown error (no error info returned from API)', + ); + }); + + it('should send the correct searchParams', async () => { + const result = await requestController.delete('http://example.com/deleteWithSearchParams', { + name: 'Batman', + age: '30', + }); + + expect(result).toStrictEqual({ success: true }); + }); + + it('should send the correct bodyparams', async () => { + const result = await requestController.delete('http://example.com/deleteWithBodyParams', undefined, { + name: 'Batman', + age: '30', + }); + + expect(result).toStrictEqual({ success: true }); + }); + + it('should send the correct searchparams and bodyparams', async () => { + const result = await requestController.delete( + 'http://example.com/deleteWithSearchAndBodyParams', + { + name: 'Batman', + }, + { + age: '30', + }, + ); + + expect(result).toStrictEqual({ success: true }); + }); +}); diff --git a/packages/integration/test/core/requestController/requestControllerPost.test.ts b/packages/integration/test/core/requestController/requestControllerPost.test.ts new file mode 100644 index 0000000000..43c2421926 --- /dev/null +++ b/packages/integration/test/core/requestController/requestControllerPost.test.ts @@ -0,0 +1,228 @@ +import { MoralisCore, RequestController } from '@moralisweb3/core'; +import { setupServer } from 'msw/node'; +import { rest } from 'msw'; + +const postJsonMock = rest.post(`http://example.com/postJson`, (req, res, ctx) => { + return res( + ctx.status(200), + ctx.json({ + iAm: 'Batman', + }), + ); +}); + +const postTextMock = rest.post(`http://example.com/postText`, (req, res, ctx) => { + return res(ctx.status(200), ctx.text('I am Batman')); +}); + +const post400ErrorMock = rest.post(`http://example.com/post400Error`, (req, res, ctx) => { + return res( + ctx.status(400), + ctx.json({ + message: 'I am not Batman', + }), + ); +}); + +const post404ErrorMock = rest.post(`http://example.com/post404Error`, (req, res, ctx) => { + return res( + ctx.status(404), + ctx.json({ + message: 'I am not Batman', + }), + ); +}); + +const post500ErrorMock = rest.post(`http://example.com/post500Error`, (req, res, ctx) => { + return res( + ctx.status(500), + ctx.json({ + message: 'I am not Batman', + }), + ); +}); + +const post503ErrorMock = rest.post(`http://example.com/post503Error`, (req, res, ctx) => { + return res( + ctx.status(503), + ctx.json({ + message: 'I am not Batman', + }), + ); +}); + +const post400ErrorMultiMessageMock = rest.post(`http://example.com/post400MultiMessageError`, (req, res, ctx) => { + return res( + ctx.status(400), + ctx.json({ + message: ['I am not Batman', 'I am not superman'], + }), + ); +}); + +const post400ErrorEmptyJsonMock = rest.post(`http://example.com/post400ErrorEmptyJson`, (req, res, ctx) => { + return res(ctx.status(400), ctx.json({})); +}); + +const post400ErrorEmptyMock = rest.post(`http://example.com/post400ErrorEmpty`, (req, res, ctx) => { + return res(ctx.status(400)); +}); + +const postWithSearchParams = rest.post(`http://example.com/postWithSearchParams`, (req, res, ctx) => { + const params = req.url.searchParams; + if (params.get('name') !== 'Batman' || params.get('age') !== '30') { + throw new Error(`Search Params are not being correctly sent. Got params: ${JSON.stringify(params)}`); + } + return res(ctx.status(200), ctx.json({ success: true })); +}); + +const postWithBodyParams = rest.post(`http://example.com/postWithBodyParams`, (req, res, ctx) => { + const body = req.body as Record; + if (body.name !== 'Batman' || body.age !== '30') { + throw new Error(`Body Params are not being correctly sent. Got body: ${JSON.stringify(body)}`); + } + return res(ctx.status(200), ctx.json({ success: true })); +}); + +const postWithSearchAndBodyParams = rest.post(`http://example.com/postWithSearchAndBodyParams`, (req, res, ctx) => { + const body = req.body as Record; + const params = req.url.searchParams; + + if (params.get('name') !== 'Batman' || body.age !== '30') { + throw new Error( + `Params are not being correctly sent. Got body: ${JSON.stringify(body)}, Got params: ${JSON.stringify(params)}`, + ); + } + return res(ctx.status(200), ctx.json({ success: true })); +}); + +const handlers = [ + postJsonMock, + postTextMock, + post400ErrorMock, + post404ErrorMock, + post500ErrorMock, + post503ErrorMock, + post400ErrorMultiMessageMock, + post400ErrorEmptyJsonMock, + post400ErrorEmptyMock, + postWithSearchParams, + postWithBodyParams, + postWithSearchAndBodyParams, +]; + +const mockServer = setupServer(...handlers); + +describe('RequestControllerPost', () => { + let requestController: RequestController; + + beforeAll(() => { + const core = MoralisCore.create(); + requestController = RequestController.create(core); + + mockServer.listen({ + onUnhandledRequest: 'warn', + }); + }); + + afterAll(() => { + mockServer.close(); + }); + + beforeEach(() => {}); + + afterEach(async () => { + /** + * Prevent issue with Jest and MSW when running multiple tests, + * where tests are finished before all requests are resolved + * https://github.com/mswjs/msw/issues/474 + */ + await new Promise((resolve) => setTimeout(resolve.bind(null), 0)); + }); + + it('should post a valid Json response', async () => { + const result = await requestController.post('http://example.com/postJson'); + + expect(result).toStrictEqual({ iAm: 'Batman' }); + }); + + it('should post a valid text response', async () => { + const result = await requestController.post('http://example.com/postText'); + + expect(result).toStrictEqual('I am Batman'); + }); + + it('should throw an error on 400 response', async () => { + expect(requestController.post('http://example.com/post400Error')).rejects.toThrowError( + '[C0006] Request failed, Bad Request(400): I am not Batman', + ); + }); + + it('should throw an error on 404 response', async () => { + expect(requestController.post('http://example.com/post404Error')).rejects.toThrowError( + '[C0006] Request failed, Not Found(404): I am not Batman', + ); + }); + + it('should throw an error on 500 response', async () => { + expect(requestController.post('http://example.com/post500Error')).rejects.toThrowError( + '[C0006] Request failed, Internal Server Error(500): I am not Batman', + ); + }); + + it('should throw an error on 503 response', async () => { + expect(requestController.post('http://example.com/post503Error')).rejects.toThrowError( + '[C0006] Request failed, Service Unavailable(503): I am not Batman', + ); + }); + + it('should handle multiple messages in an error response', async () => { + expect(requestController.post('http://example.com/post400MultiMessageError')).rejects.toThrowError( + '[C0006] Request failed, Bad Request(400): I am not Batman, I am not superman', + ); + }); + + it('should handle empty error response', async () => { + expect(requestController.post('http://example.com/post400ErrorEmptyJson')).rejects.toThrowError( + '[C0006] Request failed, Bad Request(400): Unknown error (no error info returned from API)', + ); + }); + + it('should handle empty error response', async () => { + expect(requestController.post('http://example.com/post400ErrorEmpty')).rejects.toThrowError( + '[C0006] Request failed, Bad Request(400): Unknown error (no error info returned from API)', + ); + }); + + it('should send the correct searchParams', async () => { + const result = await requestController.post('http://example.com/postWithSearchParams', { + name: 'Batman', + age: '30', + }); + + expect(result).toStrictEqual({ success: true }); + }); + + it('should send the correct bodyparams', async () => { + const result = await requestController.post('http://example.com/postWithBodyParams', undefined, { + name: 'Batman', + age: '30', + }); + + expect(result).toStrictEqual({ success: true }); + }); + + it('should send the correct searchparams and bodyparams', async () => { + const result = await requestController.post( + 'http://example.com/postWithSearchAndBodyParams', + { + name: 'Batman', + }, + { + age: '30', + }, + ); + + expect(result).toStrictEqual({ success: true }); + }); +}); diff --git a/packages/integration/test/core/requestController/requestControllerPut.test.ts b/packages/integration/test/core/requestController/requestControllerPut.test.ts new file mode 100644 index 0000000000..f36c5c1d48 --- /dev/null +++ b/packages/integration/test/core/requestController/requestControllerPut.test.ts @@ -0,0 +1,228 @@ +import { MoralisCore, RequestController } from '@moralisweb3/core'; +import { setupServer } from 'msw/node'; +import { rest } from 'msw'; + +const putJsonMock = rest.put(`http://example.com/putJson`, (req, res, ctx) => { + return res( + ctx.status(200), + ctx.json({ + iAm: 'Batman', + }), + ); +}); + +const putTextMock = rest.put(`http://example.com/putText`, (req, res, ctx) => { + return res(ctx.status(200), ctx.text('I am Batman')); +}); + +const put400ErrorMock = rest.put(`http://example.com/put400Error`, (req, res, ctx) => { + return res( + ctx.status(400), + ctx.json({ + message: 'I am not Batman', + }), + ); +}); + +const put404ErrorMock = rest.put(`http://example.com/put404Error`, (req, res, ctx) => { + return res( + ctx.status(404), + ctx.json({ + message: 'I am not Batman', + }), + ); +}); + +const put500ErrorMock = rest.put(`http://example.com/put500Error`, (req, res, ctx) => { + return res( + ctx.status(500), + ctx.json({ + message: 'I am not Batman', + }), + ); +}); + +const put503ErrorMock = rest.put(`http://example.com/put503Error`, (req, res, ctx) => { + return res( + ctx.status(503), + ctx.json({ + message: 'I am not Batman', + }), + ); +}); + +const put400ErrorMultiMessageMock = rest.put(`http://example.com/put400MultiMessageError`, (req, res, ctx) => { + return res( + ctx.status(400), + ctx.json({ + message: ['I am not Batman', 'I am not superman'], + }), + ); +}); + +const put400ErrorEmptyJsonMock = rest.put(`http://example.com/put400ErrorEmptyJson`, (req, res, ctx) => { + return res(ctx.status(400), ctx.json({})); +}); + +const put400ErrorEmptyMock = rest.put(`http://example.com/put400ErrorEmpty`, (req, res, ctx) => { + return res(ctx.status(400)); +}); + +const putWithSearchParams = rest.put(`http://example.com/putWithSearchParams`, (req, res, ctx) => { + const params = req.url.searchParams; + if (params.get('name') !== 'Batman' || params.get('age') !== '30') { + throw new Error(`Search Params are not being correctly sent. Got params: ${JSON.stringify(params)}`); + } + return res(ctx.status(200), ctx.json({ success: true })); +}); + +const putWithBodyParams = rest.put(`http://example.com/putWithBodyParams`, (req, res, ctx) => { + const body = req.body as Record; + if (body.name !== 'Batman' || body.age !== '30') { + throw new Error(`Body Params are not being correctly sent. Got body: ${JSON.stringify(body)}`); + } + return res(ctx.status(200), ctx.json({ success: true })); +}); + +const putWithSearchAndBodyParams = rest.put(`http://example.com/putWithSearchAndBodyParams`, (req, res, ctx) => { + const body = req.body as Record; + const params = req.url.searchParams; + + if (params.get('name') !== 'Batman' || body.age !== '30') { + throw new Error( + `Params are not being correctly sent. Got body: ${JSON.stringify(body)}, Got params: ${JSON.stringify(params)}`, + ); + } + return res(ctx.status(200), ctx.json({ success: true })); +}); + +const handlers = [ + putJsonMock, + putTextMock, + put400ErrorMock, + put404ErrorMock, + put500ErrorMock, + put503ErrorMock, + put400ErrorMultiMessageMock, + put400ErrorEmptyJsonMock, + put400ErrorEmptyMock, + putWithSearchParams, + putWithBodyParams, + putWithSearchAndBodyParams, +]; + +const mockServer = setupServer(...handlers); + +describe('RequestControllerPut', () => { + let requestController: RequestController; + + beforeAll(() => { + const core = MoralisCore.create(); + requestController = RequestController.create(core); + + mockServer.listen({ + onUnhandledRequest: 'warn', + }); + }); + + afterAll(() => { + mockServer.close(); + }); + + beforeEach(() => {}); + + afterEach(async () => { + /** + * Prevent issue with Jest and MSW when running multiple tests, + * where tests are finished before all requests are resolved + * https://github.com/mswjs/msw/issues/474 + */ + await new Promise((resolve) => setTimeout(resolve.bind(null), 0)); + }); + + it('should put a valid Json response', async () => { + const result = await requestController.put('http://example.com/putJson'); + + expect(result).toStrictEqual({ iAm: 'Batman' }); + }); + + it('should put a valid text response', async () => { + const result = await requestController.put('http://example.com/putText'); + + expect(result).toStrictEqual('I am Batman'); + }); + + it('should throw an error on 400 response', async () => { + expect(requestController.put('http://example.com/put400Error')).rejects.toThrowError( + '[C0006] Request failed, Bad Request(400): I am not Batman', + ); + }); + + it('should throw an error on 404 response', async () => { + expect(requestController.put('http://example.com/put404Error')).rejects.toThrowError( + '[C0006] Request failed, Not Found(404): I am not Batman', + ); + }); + + it('should throw an error on 500 response', async () => { + expect(requestController.put('http://example.com/put500Error')).rejects.toThrowError( + '[C0006] Request failed, Internal Server Error(500): I am not Batman', + ); + }); + + it('should throw an error on 503 response', async () => { + expect(requestController.put('http://example.com/put503Error')).rejects.toThrowError( + '[C0006] Request failed, Service Unavailable(503): I am not Batman', + ); + }); + + it('should handle multiple messages in an error response', async () => { + expect(requestController.put('http://example.com/put400MultiMessageError')).rejects.toThrowError( + '[C0006] Request failed, Bad Request(400): I am not Batman, I am not superman', + ); + }); + + it('should handle empty error response', async () => { + expect(requestController.put('http://example.com/put400ErrorEmptyJson')).rejects.toThrowError( + '[C0006] Request failed, Bad Request(400): Unknown error (no error info returned from API)', + ); + }); + + it('should handle empty error response', async () => { + expect(requestController.put('http://example.com/put400ErrorEmpty')).rejects.toThrowError( + '[C0006] Request failed, Bad Request(400): Unknown error (no error info returned from API)', + ); + }); + + it('should send the correct searchParams', async () => { + const result = await requestController.put('http://example.com/putWithSearchParams', { + name: 'Batman', + age: '30', + }); + + expect(result).toStrictEqual({ success: true }); + }); + + it('should send the correct bodyparams', async () => { + const result = await requestController.put('http://example.com/putWithBodyParams', undefined, { + name: 'Batman', + age: '30', + }); + + expect(result).toStrictEqual({ success: true }); + }); + + it('should send the correct searchparams and bodyparams', async () => { + const result = await requestController.put( + 'http://example.com/putWithSearchAndBodyParams', + { + name: 'Batman', + }, + { + age: '30', + }, + ); + + expect(result).toStrictEqual({ success: true }); + }); +}); diff --git a/packages/integration/test/streamApi/deleteAddress.test.ts b/packages/integration/test/streamApi/deleteAddress.test.ts index aa68c76f98..46563a0dbc 100644 --- a/packages/integration/test/streamApi/deleteAddress.test.ts +++ b/packages/integration/test/streamApi/deleteAddress.test.ts @@ -50,7 +50,7 @@ describe('deleteAddress', () => { expect( StreamApi.deleteAddress({ networkType: 'evm', - address: 'some-address', + address: '0x295522b61890c3672d12efbff4358a6411ce996f', id: 'ADDRESS_NOT_FOUND', }), ).rejects.toThrowError('[C0006] Request failed, Not Found(404): Address not found'); @@ -60,10 +60,12 @@ describe('deleteAddress', () => { expect( StreamApi.deleteAddress({ networkType: 'evm', - address: '0x295522b61890c3672d12efbff4358a6411ce996f', + address: '0x992eccc191d6f74e8be187ed6b6ac196b08314f7', id: 'STREAM_NOT_FOUND', }), ).rejects.toThrowError('[C0006] Request failed, Not Found(404): Stream not found'); }); + + // it should throw an error on no address }); });