pactum icon indicating copy to clipboard operation
pactum copied to clipboard

Need an in-built logger to log response [to be displayed in console]

Open vinayak-kaladhar opened this issue 3 years ago • 6 comments

        const response = await pactum.spec()
            .post(endpoints.baseUrl + endpoints.requestShift)
            .withBody(body)
            .withHeaders('Accept', 'application/json')
            .withHeaders('Authorization', configData.HCPAuthorization)
            .expectStatus(200)
         console.log(response.json)

In this case, if the response does not match the expected status:200, step: console.log(response.json) gets skipped

The above step helps in debugging, can we have a method exposed? like - print (response.json) within the pactum spec and this should help us with printing the response + debugging.

vinayak-kaladhar avatar Feb 02 '22 04:02 vinayak-kaladhar

By default pactum prints the request and response to the terminal when any expectation is failed. To print the req & res, we can invoke inspect method.

await pactum.spec()
  .post(endpoints.baseUrl + endpoints.requestShift)
  .withBody(body)
  .inspect()

ASaiAnudeep avatar Feb 02 '22 04:02 ASaiAnudeep

true @ASaiAnudeep , but in this case the console log shows as:{

"statusCode": 500, "headers": { "server": "openresty", "date": "Wed, 02 Feb 2022 03:45:24 GMT", "content-type": "text/plain; charset=utf-8", "content-length": "21", "connection": "close", "x-powered-by": "Express", "access-control-allow-origin": "*", "access-control-allow-methods": "GET, POST, PUT, DELETE", "access-control-allow-headers": "X-Requested-With,content-type,access_token,multipart/form-data", "etag": "W/"15-/6VXivhc2MKdLfIkLcUE47K6aH0"" }, "body": "Internal Server Error"

so, in this case if i have a need to just print: "body": "Internal Server Error", it becomes impossible. also the request contains sensitive keys, and there is a need not to expose it, inspect displays all the details

vinayak-kaladhar avatar Feb 02 '22 05:02 vinayak-kaladhar

Make sense. Please use setLogger function to implement your own logger. Based on the message you receive in the warn method, you can filter and print the required data.

ASaiAnudeep avatar Feb 02 '22 05:02 ASaiAnudeep

I'm also tagging this as an enhancement to customise the inspect method.

ASaiAnudeep avatar Feb 02 '22 05:02 ASaiAnudeep

Also custom logger does not help because :

const myCustomLogger = { trace(messages) { console.log("") }, debug(messages) { console.log("") }, info(messages) { console.log("") }, warn(messages) { }, error(messages) { pactum.response() + pactum.spec().response()} }; settings.setLogger(myCustomLogger);

on error , it says: Error: 'response()' should be called after resolving 'toss()'

vinayak-kaladhar avatar Feb 02 '22 05:02 vinayak-kaladhar

setLogger overrides the internal logger used by pactum. We are not supposed to call anything else inside them. You need to act on the messages that are received as params. Please take a look at this example

ASaiAnudeep avatar Feb 02 '22 05:02 ASaiAnudeep