Reputation: 83
I need to test web-api of one app. Test-case is if 2 simular requests came to the 2 nodes of web app at the same time, it returns erorr. So i need to send 2 requests at one moment on a two separate nodes of web app. I know how to send 1 request and how to send requests in rotation. Here is the example of request i made:
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();
var url = "url";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var json = JSON.parse(xhr.responseText);
console.log(json.result);
}
};
var data = JSON.stringify();
xhr.send(data);
Upvotes: 1
Views: 395
Reputation: 434
First, and it's mostly my opinion, I would use the fetch API to make the call to the resources you want to pull to your web-app.
I think it needs less boilerplate than a XMLHttpRequest.
On the other hand, it isn't clear to me if you want to check if your web-app is making two request to the same resource, or if want to know how to know and handle the case when your back-end get a cloned request.
In the first case, you could use something like RxJs to avoid making a request until you have a response. But maybe it is a really big dependency for your project.
An other way could be disabling the user input (like the button that is making the request) until you have a response.
But, if your question is about how to know in your backend app when you have a duplicated request from the same client, it would depend on your backend architecture. Like, if you have one load balancer and many backend services, or if you have only one nodejs backend facing the public internet, etc.
I would recommend you to somehow persist the client data some were (memory, a db, or something), like IP, or session-ID, or something useful to know who is making that API call; and then check if there is already an ongoing request to the same resource so you can make a pertinent response. Once the call is done, you can remove that from your persistence layer.
Maybe you were expecting some code snippets, but I hope this can be useful to you.
Edit after your comments:
I have host1.webapp/api/methodX and host2.webapp/api/methodX. Simply I have to send 2 requests ( 1 for host1 and 1 for host2) at same time. I don't need to wait a response from host1 and then send request to host2, i need to send them togheter just to the different hosts. And i don't care about response time/ request processing time/etc.
like if i'd used curl it'd looks like: curl request1 & request2
In that case, it depends on what behavior you want to test. You cant make two AJAX calls using the same XMLHttpRequest object. If I were you I would make two request, maybe wrap them in a promise and then use the Promise.allSettled() method. Then you check what happened on the returned values. Another (but more complex way) should be to have a service worker that intercepts all http traffic, and there you can see if the API calls are made as you wanted.
Upvotes: 3