I use SAP Cloud SDK (js) to fetch sales orders and business partners from S/4HANA.
In tests, I use nock to mock the S/4. I want to match the response only if the destination is correct, i.e., authorization is correct.
Year ago I was using axios to make the call, and I was using matchHeader
function to achieve that, something like this:
nock('https://my123456-api.s4hana.ondemand.com') // ensures the correct url
.persist()
.matchHeader('Authorization', 'Basic dXNlck5hbWU6dXNlclBhc3N3b3Jk') // ensures the correct technical user details
.get(/sap\/opu\/odata\/sap\/API_SALES_ORDER_SRV\/A_SalesOrder(.*)correctSalesOrderId(.*)/) // ensures the correct id
.reply(200, salesOrder);
nock('https://my123456-api.s4hana.ondemand.com')
.persist()
.matchHeader('Authorization', 'Basic d3JvbmdVc2VyTmFtZTp1c2VyUGFzc3dvcmQ=') // ensures the wrong user name
.get(/(.*)/)
.reply(401, "Wrong credentials etc.");
After I started using the sdk, whatever destination I provide (e.g., with wrong username) matches the first one and returns 200 with the data provided. I have done a little search in the github and found something like that:
function basicCredentials(credentials) {
return `Basic ${Buffer.from(`${credentials.username}:${credentials.password}`, 'ascii').toString('base64')}`;
}
nock('https://my123456-api.s4hana.ondemand.com', {
reqheaders: {
authorization: basicCredentials(givenCorrectBackendDetails())
}
})
.persist()
.get(/sap\/opu\/odata\/sap\/API_SALES_ORDER_SRV\/A_SalesOrder(.*)correctSalesOrderId(.*)/) // ensures the correct id
.reply(200, salesOrder);
nock('https://my123456-api.s4hana.ondemand.com', {
reqheaders: {
authorization: basicCredentials(givenWrongUserName())
}
})
.persist()
.get(/(.*)/) // captures everything
.reply(401, "Wrong credentials etc.");
Unfortunately this is giving the same result. I am using following code to make the request:
function getSalesOrder(Id, backendDetails) {
return SalesOrder.requestBuilder()
.getByKey(Id)
.execute(backendDetails)
.catch(function (error) {
console.error(error);
return null;
});
}
So I mock all the endpoints and run all my tests. Mocking only necessary end point in each and every test is a solution, and could be even better. I dont want to write test to check if sdk does its job, we all know it does :) Nonetheless, I would like to learn the way to include the authorization in the matching process. What shall I do?
Edit after Dennis' answer
So I realized that I should have ask the question a bit more clear, sorry for the confusion. I use the getSalesOrder
function (code above) to fetch data. Now I create mocks for the same endpoint; once with 'correct username' and other one is with 'wrong username' and persist them both (second code block above, the one with the reqheaders)
My expectation is that, in the test
- when I provide 'correct username' it should match the first one, i.e., should return me 200 and mock data I provided.
- when I provide 'wrong username' it should match the second one, i.e., should return 401, which then will cause sdk to throw an error etc.
What happens instead is, when I provide 'wrong username', nock returns 200 with the data I provided, i.e., it matches the first one no mather the username I provide.
This indicates that, I am not able to check the username at all. Obviously I do a mistake, there is something I do not understand. Thats what I am after :)