With Karate, I'm trying to work out how to return different mock responses based on the content of the request body.
I've got
Feature: ...
Scenario: pathMatches('/users/login') && methodIs('post') && request == {"username": "gooduser", "password": "goodpassword"}
* def responseStatus = 200
* def response = {"status: login ok"}
Scenario: pathMatches('/users/login') && methodIs('post') && request == {"username": "baduser", "password": "badpassword"}
* def responseStatus = 401
* def response = {"status: login not ok"}
Scenario:
* print request
* print requestHeaders
When I send a request with either the "gooduser" or "baduser" details, they're falling through to the default scenario. This prints the request, which looks like I'd expect.
For example, if I run
curl -X POST -d '{"username":"baduser","password":"badpassword"}' http://localhost:8999/users/login
I can see in the Karate logs that the first 2 scenarios are being skipped and the match is on (empty). However, the logs are also printing out the request body which looks correct, so I'm surprised the 2nd scenario isn't matching the request I'm sending.
Also, if I remove the '&& request = {...}' clause from the scenario, the match works fine.
Feels like I'm missing something obvious - can anyone please point me in the right direction?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…