pactumjs / pactum

REST API Testing Tool for all levels in a Test Pyramid
https://pactumjs.github.io
MIT License
538 stars 52 forks source link

feat: Https Mock Server Support #293

Closed leelaprasadv closed 1 year ago

leelaprasadv commented 1 year ago

#148

Support for HTTPS mock server.

Changes:

Example with self signed certs

const pactum = require('pactum');
const fs = require('fs')
const https = require('https')

const mock = pactum.mock;

const key = fs.readFileSync("server.key")
const cert = fs.readFileSync("server.crt")

const agent = new https.Agent({
  cert: cert,
  key: key,
  rejectUnauthorized: false // Ignore self-signed certificate errors
});

before(async () => {
  await mock.setDefaults({port: 3001, host: '127.0.0.1', httpsOpts: {key: "server.key", cert: "server.crt"}})
  await mock.start();
})

after(async () => {
  await mock.stop();
})

const { addInteractionHandler } = pactum.handler;

addInteractionHandler('get users with id 1 & 2', (ctx) => {
  return [
  {
    request: {
      method: 'GET',
      path: `/api/users/${ctx.data[0]}`
    },
    response: {
      status: 200
    }
  }
];
});

it('GET - multiple interactions', async () => {
  await pactum.spec()
    .useInteraction('get users with id 1 & 2', [2])
    .get('https://localhost:3001/api/users/2')
    .withCore({agent: agent })
    .expectStatus(200)
  mock.clearInteractions();
});

image