NestJS returns 404 page for provider pact verification test - express

I have a NestJS v8.4.0 application. When I run provider verification test my application returns 404 “Cannot PUT url…”. If I copy-paste the URL into Postman and run the request myself it works as expected.
The questions are:
Why does it return 404 error for pact initiated request but doesn’t do so for the hand-made request with the same URL, headers, body?
How can I debug for NestJS/Express routes matching to see why it cannot match the route?
To do the hand-made request I remove server.close() so that I can make a request against the same environment. Environment is the same both for pact made requests and hand-mage. Compared to Jest tests pact tests doen’t have any teardown. Hence, all mocks I’ve made for pact tests are used for hand-made request as well.

Somehow the problem is in running pact verification tests with ts-node only and in this project only. If used pure JS for pacts verification - it works. Can’t explain it, but using JS fixed the issue

Related

If graphQL uses an async concept and has http capability why does it conflict with the REST API design of Node/Express with http?

I've set up a TypeScript/Node/Express API that includes 'http' for REST and 'express-graphql'. After coding the schema, resolvers and types for TypeScript conformity I run with nodemon ts-node and everything is fine with 'http' requests. When I send a graphQL Query via Postman as a POST it just hangs. I've never seen anything like this that does not time out and it could be running for minutes. Postman shows some unusual 'read RECONNECT' error. Why is graphQL this problematic. Why doesn't it just fit in instead of suggesting the replacement of a perfectly proven process like REST?

E2E Testing a frontend and mock a request the backend is making with Cypress

Currently when creating a customer during an e2e testcase, my stripe-like payment gateway says "Customer already exists" which is true because this testcase has been run once already.
What is the high level solution? If it would be a unit test, I think mocking the payment-provider request would be the way to go. But in this case I can only intercept the requests that go from my frontend, right? I could mock away the whole request to my backend but that's not the point of e2e testing I think.
I also thought about some sort of flag or environment variable so that the backend knows if it's currently e2e tested (and in this case behave differently, eg not make this specific request). But I think that's not good practice.
Okay I figured out something. I think e2e should test the integrated services too, like the integration between my backend and the payment gateway.
When the test starts, I modify the email (which was the duplicate key for the payment provider) by doing the following little code snippet. Works for now and I think it's not that bad:
user.email = user.email.split("#").join(`${Date.now()}#`);

In Karate API mocking not working as expected for me

I am exploring Karate API double (mocking) for the integration test. For the below scenarios, I'm not getting the expected mocking response. Your help will be appreciated.
My Setup :
1. Karate Mock Server up with pathMatches rules on port 8001: http://localhost:8001 ( working, validated against "/cat" and some test calls)
2. My own Application is up from docker on port 8080. From Docker exposed 8001 port as well.
Mocking Case:
1. My application REST call exposed to all users http://localhost:8080/service/v1/findUser. This exposed API, underlying calling other REST call http://dev-STG/userservice/v1/findUser which actually giving JSON response. So, I want to mock underlying API call and validate my API behavior accordingly.
Steps tried:
1. Now, in my application config, m replacing actual underlying API call to Karate mock server(http://localhost:8001/userservice/v1/findUser). Then did build & up my application docker.
In Karate, I defined test e.g "testIntgrtn.feature" which calling my application API "http://localhost:8080/service/v1/findUser" and Karate mock server up and set with pathmatch "/userservice/v1/findUser".
After executing "testIntgrtn.feature" karate not mocking for an underlying call(http://localhost:8001/userservice/v1/findUser).
Now, in "testIntgrtn.feature" file I changed my-application URL to underlying REST URL i.e (http://localhost:8001/userservice/v1/findUser) then mocking will work like charm.
I'm not understanding why underlying API call not getting mocked here? Did I miss something here?
Also, in Karate can we monitor all REST calls (like cypress mocking).
Thanks for this wonderful framework. Which is intuitive for writing automation cases.
Karate cannot automatically intercept calls.
The recommended approach is when you boot the application running at localhost:8080 you change the configuration so that instead of calling http://dev-stg/userservice/v1/findUser it calls something like http://localhost:8001/v1/findUser. This is what most teams do, and is easy because you should anyway be defining external URL-s as application.properties (or equivalent) as a best-practice.
It is very easy to over-ride an application property in Spring Boot for example, you can do this via the command-line: https://stackoverflow.com/a/37053004/143475
If you want, you can dynamically provision a port for the mock. So your unit test can first start a mock, get the port, and then start the server. You can find details in the Karate documentation.
All this said, if you are able to change the (system) HTTP proxy before the app at localhost:8080 starts, you may be able to do this without modifying the configuration. (But it is tricky, so I recommend the approach explained above.) So in this case, Karate can actually "intercept" the outgoing HTTP calls that the app at localhost:8080 makes.
See the second-last row (5a) in the table here: https://github.com/intuit/karate/tree/master/karate-netty#consumer-provider-example

Karate API Tests - Post Request failing but works using Jmeter

I am trying to implement testing POST request for my APIs using KARATE, but getting 403 - expected csrf token not found.
However, I am able to perform POST Request using Jmeter - Login to the application, perform GET request and get the XSRF Token from GET request RESPONSE HEADERS and pass it in the POST request in Header Manager in Jmeter. That way, I am able to create a new resource using POST. I am not sure why this approach isn't working in Karate.
You must be missing some header. The fastest way you can solve this is by collaborating with someone on the server-side dev team.
Refer to this example in the Karate demos that uses CSRF:

How to stub an API when testing with Selenium Server

I'm using Nightwatch.js to test a MERN stack application—it makes calls to the Twilio API when it receives a POST request at /sms/outgoing/.
I would rather not send SMS messages every time I run my e2e tests—instead I'd like to stub out the behaviour and, ideally, return the request data, so that I can check my app is submitting the correct info.
I have looked at libraries such as nock and fetch-mock, but neither of these will work with tests that rely on selenium.
Is there a library that can intercept requests made by Selenium Server and provide the stubbing behaviour I'm after?
As you have mentioned nock and fetch-mock, I assume you're using a node server?
If that is the case, you could modify the server to detect an environment variable process.env.MOCK_API and return hard-coded responses to fetch request using fetch-reply-with