How To Test REST API Methods without referring to a Web Server?
Hi folks!
I'm building a very simple REST API.
But before testing it via a Web Server what I want to make sure that REST API methods work in principle.
Is it an easy way to "fake" the web-server request and get a result e.g. of the method with signature like that?
ClassMethod GetAllRecords(pRequest As %CSP.Request, pResponse As %CSP.Response) As %Status
e.g. calling it in Terminal?
Comments
To my knowledge what can be done it's making a call via Web Gateway using %Net.HttpRequest.
I'm afraid that without going trough the Web Gateway it's not possible, but I'd love to be proven wrong.
I don't see why you can't create another ClassMethod to test GetAllRecords()
Your test ClassMethod would need to create instances of %CSP.Request and %CSP.Response and set any properties required but it should be possible.
Looking at the Documentation for %CSP.Request it actually mentions the Command Line:
http://cg-az-cendev01/csp/documatic/%25CSP.Documatic.cls
property CSPGatewayRequest as %Boolean [ InitialExpression = 0 ];
True if the request came the CSP Gateway, and false if it was from the command line or the built in web server.
Never tried it for long time , but $system.CSP.Shell() might be a starting point
$system.CSP.* seems to have some more useful methods.
Yes, if you set up a response and request programatically as Oliver Thompson said, then you can just call the ClassMethods rather than going through HTTP to Rest.
If it's a simple get request, you can test from the terminal without setting up a %Net.HttpRequest. I usually do it like this, in a terminal (for more complicated ones with a request body, you have to initialize %request to a %CSP.Request and set it up too):
set %response = ##class(%CSP.Response).%New()
do ##class(Your.Class).YourMethod(yourarguments)
zw %responseThanks @Enrico Parisi @Oliver Thompson @Robert Cemper @Chris Stewart @David Hockenbroch for sharing your experience!
Ideally it should be an "out-of-the-box" feature as unit-testing is quite a helpful thing for every production solution.
I'll give it a try!