Project

realweb

0.01
No commit activity in last 3 years
No release in over 3 years
Easily runs a rack app for tests that hit web APIs
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
~> 2.0
>= 0

Runtime

>= 1.1.0
 Project Readme

RealWeb¶ ↑

Boot a rack app to use as a real endpoint for tests.

Why?¶ ↑

We found this very useful for gems that hit an API. Rather than mocking out all the URIs with FakeWeb, you can just write a sample rack app that looks like the API you’re expecting. It’s not uncommon to have 5+ different small rack apps that reflect different behaviors of your remote server. We created things like broken.ru that always responds with 500s, forbidden.ru that is always 403, and healthy.ru that always returns happy path responses.

Another useful way to use this is with paired client/server libraries. We use RealWeb to boot up the real rack server and hit it with the client from within the specs.

Usage¶ ↑

# fixtures/config.ru
run lambda { |env| [200, { 'Content-Type' => 'application/json' }, '[{thing: 1}, {thing: 2}]'] }

# thing_api_spec.rb
describe MyLibrary do
  before(:each) do
    @server = RealWeb.start_server("fixtures/config.ru")
  end

  after(:each) do
    @server.stop
  end

  it "runs an action that would normally hit the api" do
    MyLibrary.get_list_of_things_from_api(@server.base_uri).should_not be_empty
  end
end

RealWeb.start_server boots the given config.ru in a fork. When you call stop on the server object, the server process is killed.

The example above is an extremely simplistic rack app. We usually use Sinatra apps for RealWeb backends.

Threaded Server¶ ↑

You can specifically boot a server in a thread instead of a fork with start_server_in_thread. This can allow you to manipulate the state of the RealWeb server. Use at your own risk. This can lead to harder to comprehend tests. Actually reaching behind a “mock” api to change it’s behavior during the test run is not ideal.