Permalink
Browse files

Merge branch 'verify-double'

  • Loading branch information...
artemave committed Nov 14, 2011
2 parents 379254d + 1777ef3 commit 61e1b7749725ff98db8ad007cbabaca4832c2b12
Showing with 1,821 additions and 513 deletions.
  1. +0 −1 .gitignore
  2. +3 −0 Gemfile
  3. +142 −0 Gemfile.lock
  4. +1 −1 Guardfile
  5. +124 −37 README.markdown
  6. +1 −2 bin/console
  7. +50 −7 bin/rest-assured
  8. +15 −0 db/migrate/20111013122857_create_requests.rb
  9. +9 −0 db/migrate/20111016174101_rename_method_to_verb.rb
  10. +9 −0 db/migrate/20111021113953_add_status_to_doubles.rb
  11. +74 −0 features/command_line_options.feature
  12. +0 −24 features/persistence.feature
  13. +18 −16 features/{doubles_via_api.feature → rest_api/doubles.feature}
  14. +11 −8 features/{redirect_rules_via_api.feature → rest_api/redirects.feature}
  15. +15 −0 features/ruby_api/verify_requests.feature
  16. +39 −0 features/ruby_api/wait_for_requests.feature
  17. +47 −0 features/step_definitions/command_line_options_steps.rb
  18. +24 −17 features/step_definitions/doubles_steps.rb
  19. +0 −13 features/step_definitions/persistence_steps.rb
  20. +24 −5 features/step_definitions/redirect_rules_steps.rb
  21. +69 −0 features/step_definitions/ruby_api_steps.rb
  22. +25 −2 features/support/env.rb
  23. +46 −0 features/support/test-server.rb
  24. +31 −0 features/support/world_helpers.rb
  25. 0 features/{doubles_via_ui.feature → web_ui/doubles.feature}
  26. 0 features/{redirect_rules_via_ui.feature → web_ui/redirects.feature}
  27. 0 lib/active_record/{leak_connection_patch.rb → leaky_connections_patch.rb}
  28. +9 −31 lib/rest-assured.rb
  29. +17 −0 lib/rest-assured/client.rb
  30. +18 −0 lib/rest-assured/client/resources.rb
  31. +162 −16 lib/rest-assured/config.rb
  32. +0 −25 lib/rest-assured/init.rb
  33. +36 −21 lib/rest-assured/models/double.rb
  34. +33 −31 lib/rest-assured/models/redirect.rb
  35. +16 −0 lib/rest-assured/models/request.rb
  36. +25 −11 lib/rest-assured/routes/double.rb
  37. +10 −7 lib/rest-assured/routes/redirect.rb
  38. +22 −0 lib/rest-assured/routes/response.rb
  39. +1 −1 lib/rest-assured/version.rb
  40. +25 −0 lib/sinatra/handler_options_patch.rb
  41. 0 views/base.scss → public/css/base.css
  42. +1 −3 rest-assured.gemspec
  43. +83 −0 spec/client/resource_double_spec.rb
  44. +133 −0 spec/config_spec.rb
  45. +7 −0 spec/custom_matchers.rb
  46. +117 −83 spec/functional/double_routes_spec.rb
  47. +86 −75 spec/functional/redirect_routes_spec.rb
  48. +80 −0 spec/functional/response_spec.rb
  49. +67 −49 spec/models/double_spec.rb
  50. +28 −25 spec/models/redirect_spec.rb
  51. +17 −0 spec/models/request_spec.rb
  52. +22 −2 spec/spec_helper.rb
  53. +14 −0 ssl/localhost.crt
  54. +15 −0 ssl/localhost.key
View
@@ -1,6 +1,5 @@
*.gem
.bundle
-Gemfile.lock
pkg/*
bundle_bin
*.db
View
@@ -17,4 +17,7 @@ gem 'launchy'
gem 'rake'
gem "spork", "> 0.9.0.rc"
gem "guard-spork"
+gem 'rb-fsevent' if RUBY_PLATFORM =~ /darwin/
gem 'sinatra-activerecord'
+gem 'mysql'
+gem 'sqlite3', '~> 1.3.4'
View
@@ -0,0 +1,142 @@
+PATH
+ remote: .
+ specs:
+ rest-assured (0.2.0)
+ activerecord (~> 3.1.0)
+ activeresource (~> 3.1.0)
+ haml (>= 3.1.3)
+ rack-flash (>= 0.1.2)
+ sinatra (>= 1.3.1)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activemodel (3.1.1)
+ activesupport (= 3.1.1)
+ builder (~> 3.0.0)
+ i18n (~> 0.6)
+ activerecord (3.1.1)
+ activemodel (= 3.1.1)
+ activesupport (= 3.1.1)
+ arel (~> 2.2.1)
+ tzinfo (~> 0.3.29)
+ activeresource (3.1.1)
+ activemodel (= 3.1.1)
+ activesupport (= 3.1.1)
+ activesupport (3.1.1)
+ multi_json (~> 1.0)
+ addressable (2.2.6)
+ arel (2.2.1)
+ awesome_print (0.4.0)
+ builder (3.0.0)
+ capybara (1.1.1)
+ mime-types (>= 1.16)
+ nokogiri (>= 1.3.3)
+ rack (>= 1.0.0)
+ rack-test (>= 0.5.4)
+ selenium-webdriver (~> 2.0)
+ xpath (~> 0.1.4)
+ capybara-firebug (0.0.10)
+ capybara (~> 1.0)
+ childprocess (0.2.2)
+ ffi (~> 1.0.6)
+ columnize (0.3.4)
+ cucumber (1.1.0)
+ builder (>= 2.1.2)
+ diff-lcs (>= 1.1.2)
+ gherkin (~> 2.5.0)
+ json (>= 1.4.6)
+ term-ansicolor (>= 1.0.6)
+ database_cleaner (0.6.7)
+ diff-lcs (1.1.3)
+ ffi (1.0.9)
+ gherkin (2.5.4)
+ json (>= 1.4.6)
+ guard (0.8.8)
+ thor (~> 0.14.6)
+ guard-spork (0.3.1)
+ guard (>= 0.8.4)
+ spork (>= 0.8.4)
+ haml (3.1.3)
+ i18n (0.6.0)
+ interactive_editor (0.0.10)
+ spoon (>= 0.0.1)
+ json (1.6.1)
+ json_pure (1.6.1)
+ launchy (2.0.5)
+ addressable (~> 2.2.6)
+ linecache (0.46)
+ rbx-require-relative (> 0.0.4)
+ mime-types (1.16)
+ multi_json (1.0.3)
+ mysql (2.8.1)
+ nokogiri (1.5.0)
+ rack (1.3.5)
+ rack-flash (0.1.2)
+ rack
+ rack-protection (1.1.4)
+ rack
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ rake (0.9.2.2)
+ rb-fsevent (0.4.3.1)
+ rbx-require-relative (0.0.5)
+ rspec (2.7.0)
+ rspec-core (~> 2.7.0)
+ rspec-expectations (~> 2.7.0)
+ rspec-mocks (~> 2.7.0)
+ rspec-core (2.7.1)
+ rspec-expectations (2.7.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.7.0)
+ ruby-debug (0.10.4)
+ columnize (>= 0.1)
+ ruby-debug-base (~> 0.10.4.0)
+ ruby-debug-base (0.10.4)
+ linecache (>= 0.3)
+ rubyzip (0.9.4)
+ selenium-webdriver (2.9.1)
+ childprocess (>= 0.2.1)
+ ffi (= 1.0.9)
+ json_pure
+ rubyzip
+ shoulda-matchers (1.0.0.beta3)
+ sinatra (1.3.1)
+ rack (~> 1.3, >= 1.3.4)
+ rack-protection (~> 1.1, >= 1.1.2)
+ tilt (~> 1.3, >= 1.3.3)
+ sinatra-activerecord (0.1.3)
+ sinatra (>= 0.9.4)
+ spoon (0.0.1)
+ spork (0.9.0.rc9)
+ sqlite3 (1.3.4)
+ term-ansicolor (1.0.7)
+ thor (0.14.6)
+ tilt (1.3.3)
+ tzinfo (0.3.31)
+ xpath (0.1.4)
+ nokogiri (~> 1.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ awesome_print
+ capybara
+ capybara-firebug
+ cucumber
+ database_cleaner
+ guard-spork
+ interactive_editor
+ launchy
+ mysql
+ rack-test
+ rake
+ rb-fsevent
+ rest-assured!
+ rspec
+ ruby-debug
+ shoulda-matchers
+ sinatra-activerecord
+ spork (> 0.9.0.rc)
+ sqlite3 (~> 1.3.4)
View
@@ -4,7 +4,7 @@
guard 'spork', :cucumber_env => { 'RACK_ENV' => 'test' }, :rspec_env => { 'RACK_ENV' => 'test' } do
watch('Gemfile')
watch('Gemfile.lock')
- watch('spec/spec_helper.rb')
+ watch(%r{spec/.+\.rb})
watch(%r{features/support/.+\.rb$})
watch(%r{^lib/.+\.rb$})
end
View
@@ -2,82 +2,169 @@
## Overview
-A tool for stubbing/mocking external http based services that app under test is talking to. This is useful for blackbox testing or in cases where it is not possible to access application objects directly from test code.
+A tool for stubbing/mocking external http based services that your app under test interacts with. This is useful for blackbox/integration testing.
There are three main use cases:
-* stubbing out external data sources with predefined data, so that test code has known data to assert against
-* setting expectations on messages to external services (currently not yet implemented)
-* mimic different responses from external services during development. For that purpose there is web UI
+* stubbing out external data sources with predefined data
+* verify requests to external services
+* quickly emulate different behavior of external services during development (using web UI)
## Usage
-You are going to need ruby >= 1.8.7. Install gem and run:
+You are going to need ruby >= 1.8.7.
- bash$ sudo gem install rest-assured # omit sudo if using rvm
- bash$ rest-assured &
+First make sure there is database adapter:
+
+ bash$ gem install mysql # or sqlite
+
+If using mysql, rest-assured expects database 'rest\_assured' to be accessible by user 'root' with no password. Those are defaults and are changeable with command line options.
+
+Then install gem and run:
+
+ bash$ gem install rest-assured
+ bash$ rest-assured -a mysql &
Or clone from github and run:
bash$ git clone git@github.com:BBC/rest-assured.git
- bash$ cd rest-assured && bundle install # `gem install bundler` if command not found
- bash$ ./bin/rest-assured &
+ bash$ cd rest-assured && bundle install
+ bash$ ./bin/rest-assured -a mysql &
+
+This starts an instance of rest-assured on port 4578 (changable with --port option). You can now access it via REST or web interfaces on 'http://localhost:4578'
-This starts an instance of rest-assured on port 4578 (changable with --port option) and creates rest-assured.db (changable with --database option) in the current directory. You can now access it via REST or web interfaces on http://localhost:4578
+Various options (such as ssl, port, db credentials, etc.) are available through command line options. Check out `rest-assured -h` to see what they are.
+
+NOTE that although sqlite is an option, I found it locking under any non-trivial load. Mysql feels much more reliable. But may be that is me sqliting it wrong.
+
+## REST API
### Doubles
-Double is a stub/mock of a particular external call. There is the following rest API for setting up doubles:
+Double is a stub/mock of a particular external call.
+
+#### Ruby Client API
+
+Rest-assured provides client library which partially implements ActiveResource (create and get). To make it available put the following in your test setup code (e.g. env.rb)
+
+```ruby
+require 'rest-assured/client'
+
+RestAssured::Client.config.server_address = 'http://localhost:4578' # or wherever your rest-assured is
+```
+
+You can then create doubles in your tests
+
+```ruby
+RestAssured::Double.create(fullpath: '/products', content: 'this is content')
+```
+
+Or, in case you need verifications, create double in a Given part
+
+```ruby
+@double = RestAssured::Double.create(fullpath: '/products', verb: 'POST')
+```
+
+And verify requests happened on that double in a Then part
+
+```ruby
+@double.wait_for_requests(1, :timeout => 10) # default timeout 5 seconds
+
+req = @double.requests.first
-* `POST '/doubles', { fullpath: path, content: content, method: method }`
- Creates double with the following parameters:
+req.body.should == expected_payload
+JSON.parse(req.params).should == expected_params_hash
+JSON.parse(req.rack_env)['ACCEPT'].should == 'Application/json'
+```
- - __fullpath__ - e.g., `/some/api/object`, or with parameters in query string (useful for doubling GETs) - `/some/other/api/object?a=2&b=c`. Mandatory.
- - __content__ - whatever you want this double to respond with. Mandatory.
- - __method__ - one of http the following http verbs: GET, POST, PUT, DELETE. Optional. GET is default.
+#### Plain REST API
+
+##### Create double
+ HTTP POST to '/doubles.json' creates double and returns its json representation.
+ The following options can be passed as request parameters:
+
+ - __fullpath__ - e.g., '/some/api/object', or with parameters in query string (useful for doubling GETs) - '/some/other/api/object?a=2&b=c'. Mandatory.
+ - __content__ - whatever you want this double to respond with. Optional.
+ - __verb__ - one of http the following http verbs: GET, POST, PUT, DELETE. Optional. GET is default.
+ - __status__ - status returned when double is requested. Optional. 200 is default.
Example (using ruby RestClient):
- RestClient.post 'http://localhost:4578/doubles', { fullpath: '/api/v2/products?type=fresh', method: 'GET', content: 'this is list of products' }
+```ruby
+response = RestClient.post 'http://localhost:4578/doubles', { fullpath: '/api/v2/products?type=fresh', verb: 'GET', content: 'this is list of products', status: 200 }
+puts response.body
+```
+ Produces:
+
+ "{\"double\":{\"fullpath\":\"/api/v2/products?type=fresh\",\"verb\":\"GET\",\"id\":123,\"content\":\"this is list of products\",\"description\":null,\"status\":null,\"active\":true}}"
- Now GETting http://localhost:4578/api/v2/products?type=fresh (in browser for instance) should return "this is list of products".
+ And then GETting 'http://localhost:4578/api/v2/products?type=fresh' (in browser for instance) should return "this is list of products".
- If there is more than one double for the same request\_fullpath and method, the last created one gets served. In UI you can manually control which double is 'active' (gets served).
+ If there is more than one double for the same fullpath and verb, the last created one gets served. In UI you can manually control which double is 'active' (gets served).
-* `DELETE '/doubles/all'`
- Deletes all doubles.
+##### Get double state
+ HTTP GET to '/double/:id.json' returns json with double current state. Use id from create json as :id.
+
+ Example (using ruby RestClient):
+
+```ruby
+response = RestClient.get 'http://localhost:4578/doubles/123.json'
+puts response.body
+```
+
+ Assuming the above double has been requested once, this call would produce
+
+ "{\"double\":{\"fullpath\":\"/api/v2/products?type=fresh\",\"verb\":\"GET\",\"id\":123,\"requests\":[{\"rack_env\":\"LOOK FOR YOUR HEADERS HERE\",\"created_at\":\"2011-11-07T18:34:21+00:00\",\"body\":\"\",\"params\":\"{}\"}],\"content\":\"this is list of products\",\"description\":null,\"status\":null,\"active\":true}}"
+
+ The important bit here is 'requests' array. This is history of requests for that double (in chronological order). Each element contains the following data (keys):
+
+ - __body__ - request payload
+ - __params__ - request parameters
+ - __created_at__ - request timestamp
+ - __rack_env__ - raw request dump (key value pairs). Including request headers
+
+##### Delete all doubles
+ HTTP DELETE to '/doubles/all' deletes all doubles. Useful for cleaning up between tests.
### Redirects
It is sometimes desirable to only double certain calls while letting others through to the 'real' services. Meet Redirects. Kind of "rewrite rules" for requests that didn't match any double. Here is the rest API for managing redirects:
-* `POST '/redirects', { pattern: pattern, to: uri }` Creates redirect with the following parameters:
+#### Create redirect
+ HTTP POST to '/redirects' creates redirect.
+ The following options can be passed as request parameters:
- __pattern__ - regex (perl5 style) tested against request fullpath. Mandatory
- - __to__ - url base e.g., `https://myserver:8787/api`. Mandatory
+ - __to__ - url base e.g., 'https://myserver:8787/api'. Mandatory
Example (using ruby RestClient):
- RestClient.post 'http://localhost:4578/redirects', { pattern: '^/auth', to: 'https://myserver.com/api' }
+```ruby
+RestClient.post 'http://localhost:4578/redirects', { pattern: '^/auth', to: 'https://myserver.com/api' }
+```
- Now request (any method) to http://localhost:4578/auth/services/1 will get redirected to https://myserver.com/api/auth/services/1. Provided of course there is no double matched for that fullpath and method.
+ Now request (any verb) to 'http://localhost:4578/auth/services/1' will get redirected to 'https://myserver.com/api/auth/services/1.' Provided of course there is no double matched for that fullpath and verb.
Much like rewrite rules, redirects are evaluated in order (of creation). In UI you can manually rearrange the order.
-### Storage
-
-By default when you start rest-assured it creates (unless already exists) sqlite database and stores it into file in the current directory. This is good for using it for development - when you want doubles/redirects to persist across restarts - but may not be so desirable for using with tests, where you want each test run to start from blank slate. For that reason, you can specify `--database :memory:` so that database is kept in memory.
-
-### Logging
-
-It is sometimes useful to see what requests rest-assured is being hit. Either to explore what requests your app is making or to check that test setup is right and doubles indeed get returned. By default, when started, rest-assured creates log file in the current directory. This is configurable with `--logfile` option.
+#### Delete all redirects
+ HTTP DELETE to '/redirects/all' deletes all redirects. Useful for cleaning up between tests.
## TODO
-* Implement expectations
-* Support headers (extends previous point)
-* Ruby client library
-* Support methods in UI (at the moment it is always GET)
-* Don't allow to double internal routes. Just in case
+* Hide wiring rest-assured into ruby project behind client api
+* Bring UI upto date with rest-api (add verbs, statuses, request history)
+* Add custom response headers
## Author
[Artem Avetisyan](https://github.com/artemave)
+
+## Changelog
+
+0.2
+ - adds verifications
+ - adds ruby client
+ - adds custom return statuses
+ - adds ssl
+ - adds mysql support
+0.1 initial public release
+
View
@@ -1,11 +1,10 @@
#!/usr/bin/env ruby
require "irb"
-require 'bundler/setup'
$:.push File.expand_path('../../lib', __FILE__)
require 'rest-assured/config'
-AppConfig[:database] = ENV['FRS_DB'] || File.expand_path('../../db/development.db', __FILE__)
+RestAssured::Config.build :database => ( ENV['FRS_DB'] || File.expand_path('../../db/development.db', __FILE__) )
require 'rest-assured'
Oops, something went wrong.

0 comments on commit 61e1b77

Please sign in to comment.