cancel
Showing results for 
Search instead for 
Did you mean: 

OPA Mockserver for batch request

0 Kudos

Hi,

We are using ODataModel batch operation (where we are sending one GET and one PATCH call in a single batch operation). As per MockServer documentation, the supported method in a requests are "GET"|"POST"|"DELETE|"PUT". Does this apply for batch as well. Somehow I noticed when I run the app on Mock server, it returns dummy response for such requests. How to stub a batch request in Mockserver?

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member603382
Participant
0 Kudos

Hi there,
I do not know how you handled your "GET"|"POST"|"DELETE"|"PUT" requests, but you could always look into function imports (see https://help.sap.com/viewer/468a97775123488ab3345a0c48cadd8f/1709.000/en-US/95e5b87804ef4059bb68dd51... ).
Note: The path defined in the RegExp is the path of the page from the root file of your mock server.

Notice they use RegExp to define the path. This makes the path flexible: you can handle requests going through a same page differently because of the parameters that are enclosed in the URL.

Of course, you cannot handle directly a batch request since it is a collection of requests, not a single request. You will need to analyze your web app's requests in the Network tab of your Developer Tools in Chrome as you do actions that trigger the different requests. When you click on a batch request, you will be able to look into the Headers section once you click on a batch request to see the different requests that are made within a single batch request.

If you feel confused by what I just said, or feel it is too complicated, you can always go into your manifest.json file and put the parameter "useBatch" to false. This way, you will be able to see every individual request (through Chrome Developer Tools in the Network tab). Remember to put it back on when you are done with your mock server.

The best way to do a function import, from my experience, is simply to copy the JSON response of the request you are sending (found in the Network tab of the Chrome Developer Tools, as you click on a batch request, and under that generated Response tab. The JSON response is enclosed with {"d": _someJSONResponse_}) and makes the mock server understand that this is what you want it to send.

Here is an example:

aRequests.push({
			method: "POST",
			path: "(.*)",
			response: function (oXhr, sUrlParams) {
                                //takes the parameters of the URL
				var oUrlParams = jQuery.sap.getUriParameters(oXhr.url);
                                var oJSONResponse = {...your_JSON_Response...};
                                //This needs to be put so the mock server sends that information back
                                //first param: HTTP response status, second param: Type of response,
                                // third param: the actual response
                                oXhr.respond(200, {
					"Content-Type": "application/json"
				}, JSON.stringify({
					"d": oJSONResponse
				}));
                                //indicates that the request was handled. If omitted or false, the mock server will
                                //use its own dummy response to handle the request as well as your own
                                return true;
			}
		});
 

Where aRequests is the same as what is described in the link above. I have not used RegExp here, but it is highly suggested to do so, as the mock server might behave differently.
Note: The "$" character might cause you some issues. I never had a RegExp work with that character.

I hope this can help you!

-Alex

0 Kudos

Hi Alex,

I am facing a similar issue. But the implementation is somewhat different in our project. Can we have a call regarding the same?