Code Monkey home page Code Monkey logo

rest_easy's Introduction

RestEasy

This gem is a little testing tool I created to help with synchronization issues in a test suite. I was validating automated UI actions against a low-resource test environment API, and those validations were failing in the first second or two following an action. The rest_easy gem wraps my API calls in a timeout functionality, letting me iterate until the validation passes or times out after a number of seconds.

Example

I took a test that once looked like this (because of a slow test environment):

it "saves my edit" do
  page_object.field = "Something"
  page_object.save
  sleep 5
  expect(JSON.load(RestClient.get "/call/to/my/API")['data']['field']).to eq('Something')
end

And turned it into this:

it "saves my edit" do
  page_object.field = "Something"
  page_object.save
  expect(get_until("/call/to/my/API", 5){|data| JSON.load(data)['data']['field'] == 'Something'}).to be true
end

My hard-coded sleep of 5 seconds was wasteful. Now, the test only waits as long as it absolutely needs. It might be 1 second today or 4.5 seconds tomorrow. If it's longer than 5 seconds, the test will fail.

Installation

Add this line to your application's Gemfile:

gem 'rest_easy'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rest_easy

Usage

# fails after 10 seconds
RestEasy.get_until('http://twitter.com'){ 1 == 0 }

# fails after 3 seconds
RestEasy.get_while('http://twitter.com', 3){ 1 == 1 }

# pass the GET response to your validation block
RestEasy.get_until('http://twitter.com'){ |response| response.code == 200 }

# add cookies to your GET request with the options hash
RestEasy.get_while('http://twitter.com', 10, cookies: {auth_token: 'blah'}){ |response| response.code == 403 }

Todo

The gem is tied to rest-client, but I'd like to make any REST gem work. Also, I've only written methods for GETs. I need to add POSTs, PUTs, etc. Pull requests are welcome!

Contributing

  1. Fork it ( https://github.com/[my-github-username]/rest_easy/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

rest_easy's People

Contributors

jdenen avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.