Commit 919fc819 authored by Guillermo Sanz López's avatar Guillermo Sanz López
Browse files

add mocks

parent a6a4d5af
Loading
Loading
Loading
Loading
+106 −0
Original line number Diff line number Diff line
// For a detailed explanation regarding each configuration property, visit:
// https://www.mocks-server.org/docs/configuration/how-to-change-settings
// https://www.mocks-server.org/docs/configuration/options

module.exports = {
  // Log level. Can be one of silly, debug, verbose, info, warn or error
  //log: "info",
  config: {
    // Allow unknown arguments
    //allowUnknownArguments: false,
  },
  plugins: {
    // Plugins to be registered
    //register: [],
    proxyRoutesHandler: {
    },
    adminApi: {
      // Port number for the admin API server to be listening at
      //port: 3110,
      // Host for the admin API server
      //host: "0.0.0.0",
      https: {
        // Use https protocol or not
        //enabled: false,
        // Path to a TLS/SSL certificate
        //cert: undefined,
        // Path to the certificate private key
        //key: undefined,
      },
    },
    inquirerCli: {
      // Start interactive CLI or not
      //enabled: true,
      // Render emojis or not
      //emojis: true,
    },
    openapi: {
      collection: {
        // Name for the collection created from OpenAPI definitions
        //id: "openapi",
        // Name of the collection to extend from
        //from: undefined,
      },
    },
  },
  mock: {
    routes: {
      // Global delay to apply to routes
      //delay: 0,
    },
    collections: {
      // Selected collection
      //selected: "base",
    },
  },
  server: {
    // Port number for the server to be listening at
    //port: 3100,
    // Host for the server
    //host: "0.0.0.0",
    cors: {
      // Use CORS middleware or not
      //enabled: true,
      // Options for the CORS middleware. Further information at https://github.com/expressjs/cors#configuration-options
      //options: {"preflightContinue":false},
    },
    jsonBodyParser: {
      // Use json body-parser middleware or not
      //enabled: true,
      // Options for the json body-parser middleware. Further information at https://github.com/expressjs/body-parser
      //options: {},
    },
    urlEncodedBodyParser: {
      // Use urlencoded body-parser middleware or not
      //enabled: true,
      // Options for the urlencoded body-parser middleware. Further information at https://github.com/expressjs/body-parser
      //options: {"extended":true},
    },
    https: {
      // Use https protocol or not
      //enabled: false,
      // Path to a TLS/SSL certificate
      //cert: undefined,
      // Path to the certificate private key
      //key: undefined,
    },
  },
  files: {
    // Allows to disable files load
    //enabled: true,
    // Define folder from where to load collections and routes
    //path: "mocks",
    // Enable/disable files watcher
    //watch: true,
    babelRegister: {
      // Load @babel/register
      //enabled: false,
      // Options for @babel/register
      //options: {},
    },
  },
  variantHandlers: {
    // Variant Handlers to be registered
    //register: [],
  },
};
+21 −0
Original line number Diff line number Diff line
[
  {
    "id": "base",
    "routes": ["add-headers:enabled", "get-users:success", "get-user:success"]
  },
  {
    "id": "no-headers",
    "from": "base",
    "routes": ["add-headers:disabled"]
  },
  {
    "id": "all-users",
    "from": "base",
    "routes": ["get-users:all", "get-user:id-3"]
  },
  {
    "id": "user-real",
    "from": "no-headers",
    "routes": ["get-user:real"]
  }
]
+29 −0
Original line number Diff line number Diff line
// Use this file only as a guide for first steps using middleware variants. You can delete it when you have understood the concepts.
// For a detailed explanation about using middlewares, visit:
// https://mocks-server.org/docs/usage/variants/middlewares

module.exports = [
  {
    id: "add-headers", //route id
    url: "*", // url in express format
    method: ["GET", "POST", "PUT", "PATCH"], // HTTP methods
    variants: [
      {
        id: "enabled", // variant id
        type: "middleware", // variant handler id
        options: {
          // Express middleware to execute
          middleware: (_req, res, next, core) => {
            res.set("x-mocks-server-example", "some-value");
            core.logger.info("Custom header added by route variant middleware");
            next();
          },
        },
      },
      {
        id: "disabled", // variant id
        disabled: true,
      },
    ],
  },
];
+31 −0
Original line number Diff line number Diff line
// Use this file only as a guide for first steps using routes. Delete it when you have added your own route files.
// For a detailed explanation regarding each routes property, visit:
// https://mocks-server.org/docs/usage/routes

// users data
const USER = [
    {
      token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTcwMDc2MDk3MiwianRpIjoiMWZjZTdiZGMtMGU3My00OTgwLTlmNzktMDg5MmZmYjY0NmQwIiwidHlwZSI6ImFjY2VzcyIsInN1YiI6InN1cGVyYWRtaW4iLCJuYmYiOjE3MDA3NjA5NzJ9.A2ogmeWRY5lHPAK0fT-dnv_ru21bTKifLMqmRGX2Dzo",
      refresh_token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTcwMDc2MDk3MiwianRpIjoiMWZjZTdiZGMtMGU3My00OTgwLTlmNzktMDg5MmZmYjY0NmQwIiwidHlwZSI6ImFjY2VzcyIsInN1YiI6InN1cGVyYWRtaW4iLCJuYmYiOjE3MDA3NjA5NzJ9.A2ogmeWRY5lHPAK0fT-dnv_ru21bTKifLMqmRGX2Dzo",
    },
  ];
  
  
  module.exports = [
    {
      id: "login", // route id
      url: "/login", // url in express format
      method: "POST", // HTTP method
      variants: [
        {
          id: "success", // variant id
          type: "json", // variant handler id
          options: {
            status: 200, // status to send
            body: USER, // body to send
          },
        },
      ],
    },
  ];
  
 No newline at end of file
+132 −0
Original line number Diff line number Diff line
// Use this file only as a guide for first steps using routes. Delete it when you have added your own route files.
// For a detailed explanation regarding each routes property, visit:
// https://mocks-server.org/docs/usage/routes

// users data
const USERS = [
  {
    id: 1,
    name: "John Doe",
  },
  {
    id: 2,
    name: "Jane Doe",
  },
];

const USERLOGIN = [
  {
    id: 1,
    token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTcwMDc2MDk3MiwianRpIjoiMWZjZTdiZGMtMGU3My00OTgwLTlmNzktMDg5MmZmYjY0NmQwIiwidHlwZSI6ImFjY2VzcyIsInN1YiI6InN1cGVyYWRtaW4iLCJuYmYiOjE3MDA3NjA5NzJ9.A2ogmeWRY5lHPAK0fT-dnv_ru21bTKifLMqmRGX2Dzo",
    refresh_token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTcwMDc2MDk3MiwianRpIjoiMWZjZTdiZGMtMGU3My00OTgwLTlmNzktMDg5MmZmYjY0NmQwIiwidHlwZSI6ImFjY2VzcyIsInN1YiI6InN1cGVyYWRtaW4iLCJuYmYiOjE3MDA3NjA5NzJ9.A2ogmeWRY5lHPAK0fT-dnv_ru21bTKifLMqmRGX2Dzo",
  },
];



const ALL_USERS = [
  ...USERS,
  {
    id: 3,
    name: "Tommy",
  },
  {
    id: 4,
    name: "Timmy",
  },
];

module.exports = [
  {
    id: "get-users", // route id
    url: "/api/users", // url in express format
    method: "GET", // HTTP method
    variants: [
      {
        id: "success", // variant id
        type: "json", // variant handler id
        options: {
          status: 200, // status to send
          body: USERS, // body to send
        },
      },
      {
        id: "all", // variant id
        type: "json", // variant handler id
        options: {
          status: 200, // status to send
          body: ALL_USERS, // body to send
        },
      },
      {
        id: "error", // variant id
        type: "json", // variant handler id
        options: {
          status: 400, // status to send
          // body to send
          body: {
            message: "Error",
          },
        },
      },
    ],
  },
  {
    id: "get-user", // route id
    url: "/api/users/:id", // url in express format
    method: "GET", // HTTP method
    variants: [
      {
        id: "success", // variant id
        type: "json", // variant handler id
        options: {
          status: 200, // status to send
          body: USERS[0], // body to send
        },
      },
      {
        id: "id-3", // variant id
        type: "json", // variant handler id
        options: {
          status: 200, // status to send
          body: ALL_USERS[2], // body to send
        },
      },
      {
        id: "real", // variant id
        type: "middleware", // variant handler id
        options: {
          // Express middleware to execute
          middleware: (req, res) => {
            const userId = req.params.id;
            const user = USERS.find((userData) => userData.id === Number(userId));
            if (user) {
              res.status(200);
              res.send(user);
            } else {
              res.status(404);
              res.send({
                message: "User not found",
              });
            }
          },
        },
      },
      {
        id: "login", // route id
        url: "/login", // url in express format
        method: "POST", // HTTP method
        variants: [
          {
            id: "success", // variant id
            type: "json", // variant handler id
            options: {
              status: 200, // status to send
              body: USERLOGIN, // body to send
            },
          },
        ],
      },
    ],
  },
];
Loading