• Stars
    star
    150
  • Rank 247,323 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 14 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Ruby gem for faking external web services for testing

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.

More Repositories

1

LRResty

Yet another Objective-C REST client library, inspired by Ruby's restclient gem.
Objective-C
461
star
2

LROAuth2Client

OAuth2 client for iPhone and iPad apps
Objective-C
303
star
3

betabuilder

A collection of Rake tasks for managing and distributing iOS ad-hoc builds
Ruby
214
star
4

LRSlidingTableViewCell

A simple implementation of sliding table cells, ala Twitter for iPhone
Objective-C
189
star
5

RestfulCoreData

An attempt to come up with a sane web API to Core Data synching pattern, using the Pivotal Tracker API.
Objective-C
155
star
6

xcodebuild-rb

Ruby
143
star
7

clickatell

NO LONGER SUPPORTED - Ruby interface to the Clickatell SMS Gateway API
Ruby
138
star
8

simpleconfig

Simple Config is a plugin designed to make application-wide configuration settings easy to set and access in an object-oriented fashion.
Ruby
128
star
9

swift-responsive-textfield

A SwiftUI wrapper around UITextField with binding-based state and responder control
Swift
86
star
10

xcodesnippets

A command-line utility for managing Xcode 4 code snippets
Ruby
85
star
11

LROAuth2Demo

A demo project for the LROAuth2Client library
Objective-C
59
star
12

LRTableModel

The missing abstraction between UITableView, it's data source, and your domain model
Objective-C
52
star
13

LRMocky

A port of jMock 2.0 for Objective-C
Perl
38
star
14

PusherChat-iPhone

An iPhone client for the PusherChat-Rails example app, using libPusher
Objective-C
35
star
15

LRToolkit

A collection of useful classes and categories I use in my iPhone and iPad projects
Objective-C
32
star
16

session-timeout

A simple dynamic session expiry/timeout plugin for Rails
Ruby
27
star
17

LRLinkableLabel

UILabel replacement with support for linkable strings
Objective-C
25
star
18

LRFetchedResultSet

Objective-C
21
star
19

AutomationKit

An automated UI testing framework for iOS
Objective-C
20
star
20

LRMimic

An Objective-C client for the Mimic REST API
Objective-C
17
star
21

hsbcscraper

Web scraper for downloading statements from HSBC Business Banking
Ruby
15
star
22

swift-coding

A protocol-witness oriented library built on top of Codable
Swift
14
star
23

swift-validations

A high-level functional validation library, written in Swift
Swift
13
star
24

oauth-pkce-ios-client

A small example of how to use OAuth in a SwiftUI app with PKCE
Swift
13
star
25

beanstalk-messaging

Ruby
12
star
26

xcodebuild-outputparser

A small Ruby library for parsing the results of the xcodebuild command, particularly tests
Ruby
10
star
27

iPhoneAuctionSniper

An example of TDD iPhone development using mocks and Cucumber for high-level tests
Objective-C
9
star
28

ripperhud-toolkit

A Sinatra app and collection of Ruby scripts to help organise your downloaded and ripped DVD collection
8
star
29

ujs4rails

Unobtrusive Javascript for Rails
Ruby
8
star
30

oauth-pkce-proxy

Proof of concept OAuth provider proxy server that adds PKCE support for public clients
Ruby
8
star
31

LRRemoteImage

A really simple class to handle remote images
Objective-C
7
star
32

coopexport

Statement exporter for The Co-Operative Bank internet banking website
Ruby
7
star
33

dotfiles

My public dotfiles
Shell
7
star
34

uiautomation-rb

Ruby
6
star
35

SqueezeSlaveMenu

An OSX menu bar app for controlling SqueezeSlave
Objective-C
5
star
36

AdventOfCode2022

Swift
4
star
37

github-downloads

A simple gem for managing your Github project uploads
Ruby
4
star
38

blogdata

Data for my Marley-powered blog
3
star
39

tca-navigation-demo

Experimenting with swiftui-navigation and TCA.
Swift
3
star
40

AdventOfCode2021

Swift
3
star
41

TCANavigationStacks

Exploring the new iOS 16 APIs with TCA.
Swift
2
star
42

LRRestyMacDemo

A Mac demo project that uses LRResty as a framework
Objective-C
2
star
43

Bundler.tmbundle

Ruby
2
star
44

minisculus

Ruby
2
star
45

moviesort

A ruby script for sorting TV shows
Ruby
2
star
46

mpsontwitter.co.uk

Source code for mpsontwitter.co.uk
Ruby
2
star
47

lukeredpath

2
star
48

appium-ios-examples

Examples of using the appium-ios-driver library
Ruby
1
star
49

.vim

Vim Script
1
star
50

LRMiniServerKit

1
star
51

squeezeslave

A Git mirror of the Squeezeslave project
C
1
star
52

musickit-graphql

An experimental GraphQL wrapper for the MusicKit REST API, written in Ruby
Ruby
1
star
53

MimicCocoaSpecDemo

An example of using the Mimic gem to stub network requests from within an Objective-C test case
C
1
star
54

LRBindableObjects

THIS IS JUST A PROOF OF CONCEPT - DO NOT USE!
Objective-C
1
star