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

Pactum mock server is not starting up with https configuration #376

Closed JyotiPMallick closed 2 weeks ago

JyotiPMallick commented 2 weeks ago

Describe the bug

I am trying to run the Pactum mock server on https with a self-signed certificate giving an error.

To Reproduce Steps to reproduce the behavior:

Used with following configuration:

const mockOptions = {
  port: 3010,
  host: "localhost",
  httpsOptions: {
    key: "./certification/i4epact.pem",
    cert: "./certification/i4epact.crt",
  },
};
mock.setDefaults(mockOptions);
mock.start(3009);

Expected behavior I should access mock server with http on 3009 and https on 3010

Screenshots I am getting

opensslErrorStack: [
    'error:11800074:PKCS12 routines::pkcs12 cipherfinal error',
    'error:1C800064:Provider routines::bad decrypt',
    'error:11800074:PKCS12 routines::pkcs12 cipherfinal error'
  ],
  library: 'Provider routines',
  reason: 'bad decrypt',
  code: 'ERR_OSSL_BAD_DECRYPT'

Software (please complete the following information):

JyotiPMallick commented 2 weeks ago

Note: i4epact.pem and i4epact.crt are generated using openssl.

leelaprasadv commented 2 weeks ago

@JyotiPMallick The configuration requires .crt and .key files.

Try extracting .key from .pem https://stackoverflow.com/a/14484363

And an example of using mock server with cert/key - https://github.com/pactumjs/pactum/pull/293

JyotiPMallick commented 2 weeks ago

@leelaprasadv thank you so much, I tried configuring with below configuration

const { mock } = require("pactum");

mock.addInteraction({
  request: {
    method: "GET",
    path: "/api/v1/health/",
  },
  response: {
    status: 200,
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ message: "I am listening 👋" }),
  },
});

mock.addInteraction({
  request: {
    method: "GET",
    path: /.*/, 
  },
  response: {
    status: 404,
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ error: "Broken link" }), // Response for invalid URLs
  },
});

const mockOptions = {
  port: 3009,
  host: "localhost",
  httpsOptions: {
    key: "./certification/i4epact.key",
    cert: "./certification/i4epact.crt",
  },
};
mock.setDefaults(mockOptions);

mock.start(mockOptions);

I am getting

  throw new PactumConfigurationError(`Invalid port number provided - ${port}`);
      ^

PactumConfigurationError: Invalid port number provided - [object Object]
leelaprasadv commented 2 weeks ago

@JyotiPMallick - mock.start() doesn't accept an object as parameter. Please check the signature here - https://pactumjs.github.io/api/mock/start.html As you are already setting defaults, you could simply start the server mock.start()

JyotiPMallick commented 2 weeks ago

Thank you @leelaprasadv