2022-04-15 21:03:15 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
2023-05-29 15:32:44 +00:00
|
|
|
import { isMockMode, isTestMode } from '../utils/test-modes'
|
2022-11-30 21:10:23 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from 'next'
|
2022-04-15 21:03:15 +00:00
|
|
|
|
|
|
|
export enum HttpMethod {
|
|
|
|
GET = 'GET',
|
|
|
|
POST = 'POST',
|
|
|
|
PUT = 'PUT',
|
|
|
|
DELETE = 'DELETE'
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Intercepts a mock HTTP request, checks the used request method and responds with given response content or an error
|
|
|
|
* that the request method is not allowed.
|
|
|
|
*
|
|
|
|
* @param method The expected HTTP method.
|
|
|
|
* @param req The request object.
|
|
|
|
* @param res The response object.
|
|
|
|
* @param response The response data that will be returned when the HTTP method was the expected one.
|
|
|
|
* @param statusCode The status code with which the response will be sent.
|
2023-05-29 15:32:44 +00:00
|
|
|
* @param respondMethodNotAllowedOnMismatch If set and the method can't process the request then a 405 will be returned. Used for chaining multiple calls together.
|
2022-07-21 20:36:46 +00:00
|
|
|
* @return {@link true} if the HTTP method of the request is the expected one, {@link false} otherwise.
|
2022-04-15 21:03:15 +00:00
|
|
|
*/
|
|
|
|
export const respondToMatchingRequest = <T>(
|
|
|
|
method: HttpMethod,
|
|
|
|
req: NextApiRequest,
|
|
|
|
res: NextApiResponse,
|
|
|
|
response: T,
|
2023-05-29 15:32:44 +00:00
|
|
|
statusCode = 200,
|
|
|
|
respondMethodNotAllowedOnMismatch = true
|
2022-04-15 21:03:15 +00:00
|
|
|
): boolean => {
|
2022-09-16 09:03:29 +00:00
|
|
|
if (!isMockMode) {
|
|
|
|
res.status(404).send('Mock API is disabled')
|
|
|
|
return false
|
2023-05-29 15:32:44 +00:00
|
|
|
} else if (method === req.method) {
|
|
|
|
res.status(statusCode).json(response)
|
|
|
|
return true
|
|
|
|
} else if (respondMethodNotAllowedOnMismatch) {
|
2022-04-15 21:03:15 +00:00
|
|
|
res.status(405).send('Method not allowed')
|
2023-05-29 15:32:44 +00:00
|
|
|
return true
|
|
|
|
} else {
|
2022-04-15 21:03:15 +00:00
|
|
|
return false
|
2023-05-29 15:32:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Intercepts a mock HTTP request that is only allowed in test mode.
|
|
|
|
* Such requests can only be issued from localhost and only if mock API is activated.
|
|
|
|
*
|
|
|
|
* @param req The request object.
|
|
|
|
* @param res The response object.
|
|
|
|
* @param response The response data that will be returned when the HTTP method was the expected one.
|
|
|
|
*/
|
|
|
|
export const respondToTestRequest = <T>(req: NextApiRequest, res: NextApiResponse, response: () => T): boolean => {
|
|
|
|
if (!isMockMode) {
|
|
|
|
res.status(404).send('Mock API is disabled')
|
|
|
|
} else if (req.method !== HttpMethod.POST) {
|
|
|
|
res.status(405).send('Method not allowed')
|
|
|
|
} else if (!isTestMode) {
|
|
|
|
res.status(404).send('Route only available in test mode')
|
2023-10-24 06:57:42 +00:00
|
|
|
} else if (
|
|
|
|
req.socket.remoteAddress === undefined ||
|
|
|
|
!['127.0.0.1', '::1', '::ffff:127.0.0.1'].includes(req.socket.remoteAddress)
|
|
|
|
) {
|
2023-10-24 07:12:14 +00:00
|
|
|
res.status(403).send(`Request must come from localhost but was ${req.socket.remoteAddress}`)
|
2022-04-15 21:03:15 +00:00
|
|
|
} else {
|
2023-05-29 15:32:44 +00:00
|
|
|
res.status(200).json(response())
|
2022-04-15 21:03:15 +00:00
|
|
|
}
|
2023-05-29 15:32:44 +00:00
|
|
|
return true
|
2022-04-15 21:03:15 +00:00
|
|
|
}
|