如何从服务器端模拟复杂的 REST 调用?
在使用广泛使用 REST 服务的 javascript 时——包括使用 GET、PUT、POST、DELETES 等词汇;我发现很难模拟服务器端,因此前端开发可以独立(后端)进行。
有时捕获多步骤数据也很有用,因此我们甚至可以帮助重现整个 REST 链(或从这些链触发的与前端相关的错误)
我可以使用哪些工具来模拟 REST 调用,尤其是有状态的调用那些? (即,如果我对某些资源执行 PUT,我希望下一个 GET 会以某种方式改变)
我尝试了 SOAPUI 4.0.1,它的 REST 模拟令人失望。另外,我的需求超出了单一状态模拟(任何人都可以使用静态 .json 文件来完成)。我需要进行状态转换类型的模拟;使用 Content-Range 标头是最好的。
有人吗?
While working with javascript that uses REST services extensively -- including using vocabs like GET, PUT, POST, DELETES, etc; I have found it hard to mock the server side so front end development can go on independently (of back end).
It is also useful to sometimes capture multi-step data, so we can help reproduce the entire chain of REST even (or bugs related to the front end that are triggered from these chains)
What tools can I use to mock REST calls, esp stateful ones? (i.e. if I do a PUT on some resource, I expect the next GET on it to change somehow)
I tried SOAPUI 4.0.1 and it's REST mocking is disappointing. Plus, my need is beyond single state mocking (which anyone can do with a static .json file). I need to do state transition type of mocks; working with Content-Range headers would be best.
Anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,我最终创建了自己的 Java REST 模拟引擎,它基本上可以模拟任何响应。只要您可以手工制作或剪切粘贴模拟整个 http 响应的文本文件,您就可以使用我的解决方案来模拟该服务。
这是 servlet:
要配置它,请将预构建的响应文件放入
WebContent
文件夹中。我通常以.http
扩展名结束这些文件。下面是一个
init.http
文件示例。假设我们将此文件放置在 WebContent 内名为data
的文件夹中:标头必须与正文用空行分隔(没有空格,nada)。熟悉 http 的人会注意到这是一个纯粹的 http 响应。这是故意的。
您可以使用此工具来模拟您希望响应具有的任何 http 标头;甚至用不同的服务器标头进行响应(在我的示例中,我模拟了假装是 IIS 6.0 的响应);或不同的 HTTP 状态代码等。
从您的浏览器/javascript 调用它;首先用:
一起使用
然后在您的 javascript 或 REST AJAX 调用中,如果它与任何方法或参数 ;它将获得您之前制作的 http 响应;甚至细化到内容范围;缓存标头;等等。如果您需要后续 AJAX 调用返回其他内容,只需再次使用
__setdata
进行调用即可。我建议您设置一些按钮来在您的网络应用程序中进行显式状态转换。假设一切都已设置完毕,对于模拟的 REST 链,开发人员可以执行以下操作:
调用
运行一个 javascript 模块,该模块将导致调用(例如,使用 GET)
单击一个按钮,然后执行以下操作:
运行另一个将导致调用的 javascript 步骤(例如,使用 PUT)
单击另一个按钮,然后执行以下操作:
运行另一个将导致调用的 javascript 步骤(例如,使用 GET)
但这次期望的结果与 #4 不同。
这甚至应该适用于二进制和压缩响应,但我还没有测试过。
I actually ended up creating my own Java REST Mock Engine that can basically mock any response. As long as you can handcraft or cut-paste a text file that simulates the entire http response, you can use my solution to mock the service.
Here's the servlet:
To configure it, place prebuilt response files inside your
WebContent
folder. I usually end these files with.http
extensions.An example
init.http
file is below. Pretend we placed this file inside a folder calleddata
inside WebContent:Headers must separate with body by an empty line (no spaces, nada). People familiar with http will notice it's a pure http response. This is on purpose.
You can use this tool to simulate any of the http headers you want the response to have; even going so far to respond with different server header(in my example, I simulated the response pretending to be IIS 6.0); or a different HTTP status code, etc.
To invoke it from your browser/javascript; first prime it with:
Then in your javascript or REST AJAX call, if it goes to
with any method or parameter; it will get the http response you previously crafted with; even down to the Content-Range; Cache headers; etc. If you then need the subsequent AJAX call to return something else, simply call with
__setdata
again. I suggest you setup a few buttons to do the explicit state transition in your web app.Assuming everything is setup, for a simulated REST chain, a developer may do:
invoke
run a javascript module that will result in calling (say, with GET)
click a button that then does:
run another javascript step that will result in calling (say, with PUT)
click another button that then does:
run another javascript step that will result in calling (say, with GET)
but this time expecting different result than #4.
This should even work with binary and gzipped responses, but I haven't tested that.
这是另一个自制的休息模拟工具:https://github.com/mkotsur/restito。
Here is another homegrown rest-mocking tool: https://github.com/mkotsur/restito.