Skip to content
Boot up a real server to serve testing replies
Ruby
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
benchmark
lib
test
.rubocop.yml
.travis.yml
Gemfile
Gemfile.lock
MIT-LICENSE
Rakefile
Readme.md
stub_server.gemspec

Readme.md

Boot up a real server to serve testing replies

Install

gem install stub_server

Usage

require 'stub_server'
require 'open-uri'

describe "Stub Server" do
  let(:port) { 9123 }
  let(:replies) { { "/hello" => [200, {}, ["World"]] } }
  
  it "can connect" do
    StubServer.open(port, replies) do |server|
      server.wait # ~ 0.1s
      open("http://localhost:#{port}/hello").read.must_equal "World"
    end
  end
  
  it "fails on unknown paths" do
    StubServer.open(port, replies) do |server|
      server.wait
      assert_raises(OpenURI::HTTPError) { open("http://localhost:#{port}/no").read }
    end
  end
end
  • Enable ssl ssl: {cert: File.read(cert), key: File.read(key)}
  • override other options by passing in WebBrick options, see lib/stub_server.rb
  • Use json: true to make all replies .to_json before sending, this is useful when modifying replies inside of tests

Author

Michael Grosser
michael@grosser.it
License: MIT
Build Status

You can’t perform that action at this time.