"use strict";
/*
 * @adonisjs/shield
 *
 * (c) AdonisJS
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.defineTestsBindings = void 0;
/// <reference path="../../adonis-typings/index.ts" />
const csrf_1 = __importDefault(require("csrf"));
/**
 * Define test bindings
 */
function defineTestsBindings(ApiRequest, ApiClient) {
    /**
     * Set CSRF token during the HTTP request
     */
    ApiRequest.macro('withCsrfToken', function () {
        this['setCsrfToken'] = true;
        return this;
    });
    ApiClient.setup(async (request) => {
        const setCsrfToken = request['setCsrfToken'];
        if (!setCsrfToken) {
            return;
        }
        const tokens = new csrf_1.default();
        const secret = await tokens.secret();
        const token = tokens.create(secret);
        request.session({ 'csrf-secret': secret });
        request.header('x-csrf-token', token);
    });
}
exports.defineTestsBindings = defineTestsBindings;
