Created
June 11, 2021 15:02
-
-
Save diasjuniorr/6165b33a6b3c225ef088ee9b71155415 to your computer and use it in GitHub Desktop.
Supertest query params
This file contains hidden or 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
| import request from 'supertest' | |
| import http, { IncomingMessage, ServerResponse } from 'http' | |
| import { apiResolver } from 'next/dist/next-server/server/api-utils' | |
| import handler from '../applicantsGet' | |
| describe('testing request query params', () => { | |
| // mock for `apiResolver`'s 5th parameter to please TS | |
| const apiPreviewPropsMock = { | |
| previewModeId: 'id', | |
| previewModeEncryptionKey: 'key', | |
| previewModeSigningKey: 'key', | |
| } | |
| const requestListener = (req: IncomingMessage, res: ServerResponse) => { | |
| apiResolver(req, res, undefined, handler, apiPreviewPropsMock, undefined) | |
| } | |
| it('query params', async () => { | |
| const server = http.createServer(requestListener) | |
| const response = await request | |
| .agent(server) | |
| .get('/api/applicantsGet') | |
| .query({ 'filterBy[hired]': true }) | |
| .set('Authorization', authToken) | |
| .set('x-tenant', tenantId) | |
| // console.log('debugando : ', response.body) | |
| expect(response.statusCode).toEqual(200) | |
| server.close() | |
| }) | |
| }) |
This file contains hidden or 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
| import { NextApiRequest, NextApiResponse } from 'next' | |
| import { validateRequest } from '../../util/middlewares/validateRequest' | |
| import { authenticate } from '../../util/middlewares/authenticate' | |
| import { ProxyService } from '../../util/services/proxyService' | |
| import { makeNewCustomError } from '../../util/errors/errorHandler' | |
| const handler = async (req: NextApiRequest, res: NextApiResponse) => { | |
| const { method, headers, query } = req | |
| console.log('veja: ', req.url) | |
| console.log('veja: ', req.query) | |
| if (method === 'GET') { | |
| try { | |
| const tenantId = headers['x-tenant'] as string | |
| const proxyService = new ProxyService(tenantId, query) | |
| const response = await proxyService.execute() | |
| if (response.error) { | |
| const error = makeNewCustomError(response.error) | |
| return res.status(error.data.code).json(error.data) | |
| } | |
| return res.json(response) | |
| } catch (error) { | |
| console.log('error: ', error) | |
| const response = makeNewCustomError(error) | |
| return res.status(response.data.code).json(response.data) | |
| } | |
| } | |
| res.setHeader('Allow', ['GET']) | |
| return res.status(405).end(`Method ${method} Not Allowed`) | |
| } | |
| export default validateRequest(authenticate(handler)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The thing is
apiResolver(req, res, undefined, handler, apiPreviewPropsMock, undefined)3rd arg is query params so if you have a route/api/posts/:idinside Next.js controllerreq.query.idwill not be passed and will be hardcoded undefined. Do you know how to pass api dynamic route params?