3 Star 1 Fork 0

Gitee 极速下载 / gems-mimic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/lukeredpath/mimic
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Mimic, simple web service stubs for testing Build Status

What is Mimic?

Mimic is a testing tool that lets you set create a fake stand-in for an external web service to be used when writing integration/end-to-end tests for applications or libraries that access these services.

Why not stub?

There are already some good tools, like FakeWeb which let you stub requests at a low-level which is fine for unit and functional tests but when exercising our code through integration or end-to-end tests we want to exercise as much of the stack as possible.

Mimic aims to make it possible to test your networking code without actually hitting the real services by starting up a real web server and responding to HTTP requests. This lets you test your application against canned responses in an as-close-to-the-real-thing-as-possible way.

Also, because Mimic responds to real HTTP requests, it can be used when testing non-Ruby applications too.

Examples

Registering to a single request stub:

Mimic.mimic.get("/some/path").returning("hello world")

And the result, using RestClient:

$ RestClient.get("http://www.example.com:11988/some/path") # => 200 | hello world

Registering multiple request stubs; note that you can stub the same path with different HTTP methods separately.

Mimic.mimic do
  get("/some/path").returning("Hello World", 200)
  get("/some/other/path").returning("Redirecting...", 301, {"Location" => "somewhere else"})
  post("/some/path").returning("Created!", 201)
end

You can even use Rack middlewares, e.g. to handle common testing scenarios such as authentication:

Mimic.mimic do
  use Rack::Auth::Basic do |user, pass|
    user == 'theuser' and pass == 'thepass'
  end
  
  get("/some/path")
end

Finally, because Mimic is built on top of Sinatra for the core request handling, you can create your stubbed requests like you would in any Sinatra app:

Mimic.mimic do
  get "/some/path" do
    [200, {}, "hello world"]
  end
end

Using Mimic with non-Ruby processes

Mimic has a built-in REST API that lets you configure your request stubs over HTTP. This makes it possible to use Mimic from other processes that can perform HTTP requests.

First of all, you'll need to run Mimic as a daemon. You can do this with a simple Ruby script and the daemons gem:

#!/usr/bin/env ruby
require 'mimic'
require 'daemons'

Daemons.run_proc("mimic") do
  Mimic.mimic(:port => 11988, :fork => false, :remote_configuration_path => '/api') do
    # configure your stubs here
  end
end

Give the script executable permissions and then start it:

$ your_mimic_script.rb start (or run)

The remote configuration path is where the API endpoints will be mounted - this is configurable as you will not be able this path or any paths below it in your stubs, so choose one that doesn't conflict with the paths you need to stub.

The API supports both JSON and Plist payloads, defaulting to JSON. Set the request Content-Type header to application/plist for Plist requests.

For the following Mimic configuration (using the Ruby DSL):

Mimic.mimic.get("/some/path").returning("hello world")

The equivalent stub can be configured using the REST API as follows:

$ curl -d'{"path":"/some/path", "body":"hello world"}' http://localhost:11988/api/get

Likewise, a POST request to the same path could be stubbed like so:

$ curl -d'{"path":"/some/path", "body":"hello world"}' http://localhost:11988/api/post

The end-point of the API is the HTTP verb you are stubbing, the path, response body, code and headers are specified in the POST data (a hash in JSON or Plist format). See the HTTP API Cucumber features for more examples.

An Objective-C wrapper for the REST API is available, allowing you to use mimic for your OSX and iOS apps.

Contributors

License

As usual, the code is released under the MIT license which is included in the repository.

Copyright (c) 2010 Luke Redpath Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Mimic, 轻量级Web服务测试桩 开发语言:Ruby 注册一个Web请求测试桩: Mimic.mimic.get("/some/path").returning("hell 展开 收起
Ruby
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Ruby
1
https://gitee.com/mirrors/gems-mimic.git
git@gitee.com:mirrors/gems-mimic.git
mirrors
gems-mimic
gems-mimic
develop

搜索帮助

344bd9b3 5694891 D2dac590 5694891