rest-assured 0.2.0.rc4 → 0.2.0.rc5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Guardfile CHANGED
@@ -4,7 +4,7 @@
4
4
  guard 'spork', :cucumber_env => { 'RACK_ENV' => 'test' }, :rspec_env => { 'RACK_ENV' => 'test' } do
5
5
  watch('Gemfile')
6
6
  watch('Gemfile.lock')
7
- watch('spec/spec_helper.rb')
7
+ watch(%r{spec/.+\.rb})
8
8
  watch(%r{features/support/.+\.rb$})
9
9
  watch(%r{^lib/.+\.rb$})
10
10
  end
@@ -0,0 +1,9 @@
1
+ class AddStatusToDoubles < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :doubles, :status, :integer
4
+ end
5
+
6
+ def self.down
7
+ remove_column :doubles, :status
8
+ end
9
+ end
@@ -3,29 +3,32 @@ Feature: use doubles via api
3
3
  As a developer
4
4
  I want to mock rest services my app is consuming from
5
5
 
6
+ @now
6
7
  Scenario Outline: create double
7
- When I create a double with "<fullpath>" as fullpath, "<content>" as response content and "<verb>" as request verb
8
- Then there should be 1 double with "<fullpath>" as fullpath, "<content>" as response content and "<result_verb>" as request verb
8
+ When I create a double with "<fullpath>" as fullpath, "<content>" as response content, "<verb>" as request verb and status as "<status>"
9
+ Then there should be 1 double with "<fullpath>" as fullpath, "<content>" as response content, "<result_verb>" as request verb and status as "<result_status>"
9
10
 
10
11
  Examples:
11
- | fullpath | content | verb | result_verb |
12
- | /api/something | created | POST | POST |
13
- | /api/sss | changed | PUT | PUT |
14
- | /api/asdfsf | removed | DELETE | DELETE |
15
- | /api/some | text content | GET | GET |
16
- | /api/some?a=3&b=dd | more content | | GET |
12
+ | fullpath | content | verb | result_verb | status | result_status |
13
+ | /api/something | created | POST | POST | 200 | 200 |
14
+ | /api/sss | changed | PUT | PUT | 201 | 201 |
15
+ | /api/asdfsf | removed | DELETE | DELETE | 300 | 300 |
16
+ | /api/some | text content | GET | GET | 303 | 303 |
17
+ | /api/some?a=3&b=dd | more content | | GET | | 200 |
18
+ | /api/empty | | POST | POST | | 200 |
17
19
 
18
20
  Scenario Outline: request fullpath that matches double
19
- Given there is double with "<fullpath>" as fullpath, "<content>" as response content and "<verb>" as request verb
21
+ Given there is double with "<fullpath>" as fullpath, "<content>" as response content, "<verb>" as request verb and "<status>" as status
20
22
  When I "<verb>" "<fullpath>"
21
- Then I should get "<content>" in response content
23
+ Then I should get "<status>" as response status and "<content>" in response content
22
24
 
23
25
  Examples:
24
- | fullpath | content | verb |
25
- | /api/something | created | POST |
26
- | /api/sss | changed | PUT |
27
- | /api/asdfsf | removed | DELETE |
28
- | /api/some?a=3&b=dd | more content | GET |
26
+ | fullpath | content | verb | status |
27
+ | /api/something | created | POST | 200 |
28
+ | /api/sss | changed | PUT | 201 |
29
+ | /api/asdfsf | removed | DELETE | 202 |
30
+ | /api/some?a=3&b=dd | more content | GET | 203 |
31
+ | /other/api | | GET | 303 |
29
32
 
30
33
  # current rule: last added double gets picked
31
34
  Scenario Outline: request fullpath that matches multiple doubles
@@ -9,25 +9,25 @@ When /^I create a double with "([^"]*)" as fullpath and "([^"]*)" as response co
9
9
  last_response.should be_ok
10
10
  end
11
11
 
12
- When /^I create a double with "([^"]*)" as fullpath, "([^"]*)" as response content and "([^"]*)" as request verb$/ do |fullpath, content, verb|
13
- post '/doubles', { :fullpath => fullpath, :content => content, :verb => verb }
12
+ When /^I create a double with "([^""]*)" as fullpath, "([^""]*)" as response content, "([^""]*)" as request verb and status as "([^""]*)"$/ do |fullpath, content, verb, status|
13
+ post '/doubles', { :fullpath => fullpath, :content => content, :verb => verb, :status => status }
14
14
  last_response.should be_ok
15
15
  end
16
16
 
17
17
  Then /^there should be (#{CAPTURE_A_NUMBER}) double with "([^"]*)" as fullpath and "([^"]*)" as response content$/ do |n, fullpath, content|
18
- Double.where(:fullpath => fullpath, :content => content).count.should == 1
18
+ Double.where(:fullpath => fullpath, :content => content).count.should == n
19
19
  end
20
20
 
21
- Then /^there should be (#{CAPTURE_A_NUMBER}) double with "([^"]*)" as fullpath, "([^"]*)" as response content and "([^"]*)" as request verb$/ do |n, fullpath, content, verb|
22
- Double.where(:fullpath => fullpath, :content => content, :verb => verb).count.should == n
21
+ Then /^there should be (#{CAPTURE_A_NUMBER}) double with "([^""]*)" as fullpath, "([^""]*)" as response content, "([^""]*)" as request verb and status as "(#{CAPTURE_A_NUMBER})"$/ do |n, fullpath, content, verb, status|
22
+ Double.where(:fullpath => fullpath, :content => content, :verb => verb, :status => status).count.should == n
23
23
  end
24
24
 
25
25
  Given /^there is double with "([^"]*)" as fullpath and "([^"]*)" as response content$/ do |fullpath, content|
26
26
  Double.create(:fullpath => fullpath, :content => content)
27
27
  end
28
28
 
29
- Given /^there is double with "([^"]*)" as fullpath, "([^"]*)" as response content and "([^"]*)" as request verb$/ do |fullpath, content, verb|
30
- Double.create(:fullpath => fullpath, :content => content, :verb => verb)
29
+ Given /^there is double with "([^"]*)" as fullpath, "([^"]*)" as response content, "([^"]*)" as request verb and "([^"]*)" as status$/ do |fullpath, content, verb, status|
30
+ Double.create(:fullpath => fullpath, :content => content, :verb => verb, :status => status)
31
31
  end
32
32
 
33
33
  Given /^I register "([^"]*)" as fullpath and "([^"]*)" as response content$/ do |fullpath, content|
@@ -43,7 +43,8 @@ When /^I "([^"]*)" "([^"]*)"$/ do |verb, fullpath|
43
43
  send(verb.downcase, fullpath)
44
44
  end
45
45
 
46
- Then /^I should get "([^"]*)" in response content$/ do |content|
46
+ Then /^I should get (?:"(#{CAPTURE_A_NUMBER})" as response status and )?"([^"]*)" in response content$/ do |status, content|
47
+ last_response.status.should == status if status.present?
47
48
  last_response.body.should == content
48
49
  end
49
50
 
@@ -1,13 +1,18 @@
1
+ require 'net/http'
2
+
1
3
  class Double < ActiveRecord::Base
2
- attr_accessible :fullpath, :content, :description, :verb
4
+ attr_accessible :fullpath, :content, :description, :verb, :status
3
5
 
4
- METHODS = %w{GET POST PUT DELETE}
6
+ VERBS = %w{GET POST PUT DELETE}
7
+ STATUSES = Net::HTTPResponse::CODE_TO_OBJ.keys.map(&:to_i)
5
8
 
6
- validates_presence_of :fullpath, :content
7
- validates_inclusion_of :verb, :in => METHODS
9
+ validates_presence_of :fullpath
10
+ validates_inclusion_of :verb, :in => VERBS
11
+ validates_inclusion_of :status, :in => STATUSES
8
12
 
9
13
  before_save :toggle_active
10
14
  before_validation :set_verb
15
+ before_validation :set_status
11
16
  after_destroy :set_active
12
17
 
13
18
  has_many :requests
@@ -25,6 +30,10 @@ class Double < ActiveRecord::Base
25
30
  self.verb = 'GET' unless verb.present?
26
31
  end
27
32
 
33
+ def set_status
34
+ self.status = 200 unless status.present?
35
+ end
36
+
28
37
  def set_active
29
38
  if active && f = Double.where(:fullpath => fullpath).last
30
39
  f.active = true
@@ -27,7 +27,7 @@ module RestAssured
27
27
  end
28
28
 
29
29
  router.post /^\/doubles(\.json)?$/ do |passes_json|
30
- f = { :fullpath => params['fullpath'], :content => params['content'], :description => params['description'], :verb => params['verb'] }
30
+ f = { :fullpath => params['fullpath'], :content => params['content'], :description => params['description'], :verb => params['verb'], :status => params['status'] }
31
31
 
32
32
  @double = Double.create(passes_json ? JSON.parse(request.body.read)['double'] : ( params['double'] || f ))
33
33
 
@@ -10,6 +10,7 @@ class Response
10
10
  d.requests.create!(:rack_env => env.to_json, :body => body, :params => request.params.to_json)
11
11
 
12
12
  app.body d.content
13
+ app.status d.status
13
14
  elsif r = Redirect.ordered.find { |r| request.fullpath =~ /#{r.pattern}/ }
14
15
  app.redirect( "#{r.to}#{request.fullpath}" )
15
16
  else
@@ -1,3 +1,3 @@
1
1
  module RestAssured
2
- VERSION = '0.2.0.rc4'
2
+ VERSION = '0.2.0.rc5'
3
3
  end
@@ -0,0 +1,7 @@
1
+ RSpec::Matchers.define :validate_inclusion_of do |field, list|
2
+ match do |actual|
3
+ list.each do |i|
4
+ actual.should allow_value(i).for(field)
5
+ end
6
+ end
7
+ end
@@ -2,13 +2,13 @@ require File.expand_path('../../spec_helper', __FILE__)
2
2
 
3
3
  describe 'Double routes' do
4
4
  let :test_double do
5
- { :fullpath => '/api/google?a=5', :content => 'some awesome content', :verb => 'POST' }
5
+ { :fullpath => '/api/google?a=5', :content => 'some awesome content', :verb => 'POST', :status => '201' }
6
6
  end
7
7
  let :valid_params do
8
- { 'double[fullpath]' => test_double[:fullpath], 'double[content]' => test_double[:content], 'double[verb]' => test_double[:verb] }
8
+ { 'double[fullpath]' => test_double[:fullpath], 'double[content]' => test_double[:content], 'double[verb]' => test_double[:verb], 'double[status]' => test_double[:status] }
9
9
  end
10
10
  let :invalid_params do
11
- { 'double[fullpath]' => test_double[:fullpath] }
11
+ valid_params.except('double[fullpath]')
12
12
  end
13
13
 
14
14
  describe "through ui", :ui => true do
@@ -46,7 +46,7 @@ describe 'Double routes' do
46
46
  post '/doubles', invalid_params
47
47
 
48
48
  last_response.should be_ok
49
- last_response.body.should =~ /Crumps!.*Content can't be blank/
49
+ last_response.body.should =~ /Crumps!.*Fullpath can't be blank/
50
50
  end
51
51
 
52
52
  it "brings up double edit form" do
@@ -99,10 +99,10 @@ describe 'Double routes' do
99
99
  end
100
100
 
101
101
  it "reports failure when creating with invalid parameters" do
102
- post '/doubles', test_double.except(:content)
102
+ post '/doubles', test_double.except(:fullpath)
103
103
 
104
104
  last_response.should_not be_ok
105
- last_response.body.should =~ /\{"content":\["can't be blank"\]\}/
105
+ last_response.body.should =~ /\{"fullpath":\["can't be blank"\]\}/
106
106
  end
107
107
 
108
108
  it "deletes all doubles" do
@@ -125,10 +125,10 @@ describe 'Double routes' do
125
125
  end
126
126
 
127
127
  it "reports failure when creating with invalid parameters" do
128
- post '/doubles.json', { :double => test_double.except(:content) }.to_json, 'CONTENT_TYPE' => 'Application/json'
128
+ post '/doubles.json', { :double => test_double.except(:fullpath) }.to_json, 'CONTENT_TYPE' => 'Application/json'
129
129
 
130
130
  last_response.should_not be_ok
131
- last_response.body.should =~ /\{"content":\["can't be blank"\]\}/
131
+ last_response.body.should =~ /\{"fullpath":\["can't be blank"\]\}/
132
132
  end
133
133
 
134
134
  it 'loads double as AR resource' do
@@ -22,12 +22,32 @@ describe Response do
22
22
  }
23
23
  let(:rest_assured_app) { double('App', :request => request).as_null_object }
24
24
 
25
- it "returns double content if an active one found with the same fullpath and the same method as request" do
26
- d = Double.create :fullpath => '/some/path', :content => 'content'
27
- request.stub(:fullpath).and_return(d.fullpath)
25
+ context 'when double matches request' do
26
+ before do
27
+ @double = Double.create :fullpath => '/some/path', :content => 'content', :status => 201
28
+ request.stub(:fullpath).and_return(@double.fullpath)
29
+ end
28
30
 
29
- rest_assured_app.should_receive(:body).with(d.content)
30
- Response.perform(rest_assured_app)
31
+ it "returns double content" do
32
+ rest_assured_app.should_receive(:body).with(@double.content)
33
+
34
+ Response.perform(rest_assured_app)
35
+ end
36
+
37
+ it 'sets response status to the one from double' do
38
+ rest_assured_app.should_receive(:status).with(@double.status)
39
+
40
+ Response.perform(rest_assured_app)
41
+ end
42
+
43
+ it 'records request' do
44
+ requests = double
45
+ Double.stub_chain('where.first').and_return(double(:requests => requests).as_null_object)
46
+
47
+ requests.should_receive(:create!).with(:rack_env => 'env', :body => 'body', :params => 'params')
48
+
49
+ Response.perform(rest_assured_app)
50
+ end
31
51
  end
32
52
 
33
53
  it "redirects if double not hit but there is redirect that matches request" do
@@ -46,15 +66,6 @@ describe Response do
46
66
  Response.perform(rest_assured_app)
47
67
  end
48
68
 
49
- it 'records request if double matches' do
50
- requests = double
51
- Double.stub_chain('where.first').and_return(double(:requests => requests).as_null_object)
52
-
53
- requests.should_receive(:create!).with(:rack_env => 'env', :body => 'body', :params => 'params')
54
-
55
- Response.perform(rest_assured_app)
56
- end
57
-
58
69
  it 'excludes "rack.input" and "rack.errors" as they break with "IOError - not opened for reading:" on consequent #to_json (as they are IO and StringIO)' do
59
70
  requests = double.as_null_object
60
71
  Double.stub_chain('where.first').and_return(double(:requests => requests).as_null_object)
@@ -2,19 +2,34 @@ require File.expand_path('../../spec_helper', __FILE__)
2
2
 
3
3
  describe Double do
4
4
  let :valid_params do
5
- { :fullpath => '/some/api', :content => 'some content', :verb => 'GET' }
5
+ { :fullpath => '/some/api', :content => 'some content', :verb => 'GET', :status => '303' }
6
6
  end
7
7
 
8
8
  it { should validate_presence_of(:fullpath) }
9
- it { should validate_presence_of(:content) }
10
- #it { should ensure_inclusion_of(:verb).in(Double::METHODS) }
9
+ it { should validate_inclusion_of(:verb, Double::VERBS) }
10
+ it { should validate_inclusion_of(:status, Double::STATUSES) }
11
+ it { should allow_mass_assignment_of(:fullpath) }
12
+ it { should allow_mass_assignment_of(:content) }
13
+ it { should allow_mass_assignment_of(:verb) }
14
+ it { should allow_mass_assignment_of(:status) }
15
+
11
16
  it { should have_many(:requests) }
12
17
 
18
+ it 'creates double with valid params' do
19
+ d = Double.new valid_params
20
+ d.should be_valid
21
+ end
22
+
13
23
  it "defaults verb to GET" do
14
24
  f = Double.create valid_params.except(:verb)
15
25
  f.verb.should == 'GET'
16
26
  end
17
27
 
28
+ it "defaults status to 200" do
29
+ f = Double.create valid_params.except(:status)
30
+ f.status.should == 200
31
+ end
32
+
18
33
  it "makes double active by default" do
19
34
  f = Double.create valid_params.except(:active)
20
35
  f.active.should be true
@@ -8,6 +8,9 @@ describe Redirect do
8
8
 
9
9
  it { should validate_presence_of(:pattern) }
10
10
  it { should validate_presence_of(:to) }
11
+ it { should allow_mass_assignment_of(:pattern) }
12
+ it { should allow_mass_assignment_of(:to) }
13
+ it { should allow_mass_assignment_of(:position) }
11
14
  # commented out since it doesn't work with :allow_blank => true
12
15
  #it { should validate_uniqueness_of(:position) }
13
16
 
@@ -7,6 +7,7 @@ Spork.prefork do
7
7
  require 'capybara/rspec'
8
8
  require 'rack/test'
9
9
  require 'database_cleaner'
10
+ require File.expand_path('../custom_matchers', __FILE__)
10
11
 
11
12
  ENV['RACK_ENV'] = 'test'
12
13
 
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-assured
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15424061
4
+ hash: 15424063
5
5
  prerelease: 6
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
9
  - 0
10
10
  - rc
11
- - 4
12
- version: 0.2.0.rc4
11
+ - 5
12
+ version: 0.2.0.rc5
13
13
  platform: ruby
14
14
  authors:
15
15
  - Artem Avetisyan
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2011-10-20 00:00:00 Z
20
+ date: 2011-10-21 00:00:00 Z
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
23
23
  name: sinatra
@@ -158,6 +158,7 @@ files:
158
158
  - db/migrate/20110912163705_rename_fixtures_to_doubles.rb
159
159
  - db/migrate/20111013122857_create_requests.rb
160
160
  - db/migrate/20111016174101_rename_method_to_verb.rb
161
+ - db/migrate/20111021113953_add_status_to_doubles.rb
161
162
  - features/call_history.feature
162
163
  - features/doubles_via_api.feature
163
164
  - features/doubles_via_ui.feature
@@ -204,6 +205,7 @@ files:
204
205
  - public/javascript/jquery.jgrowl_minimized.js
205
206
  - rest-assured.gemspec
206
207
  - spec/client/resource_double_spec.rb
208
+ - spec/custom_matchers.rb
207
209
  - spec/functional/double_routes_spec.rb
208
210
  - spec/functional/redirect_routes_spec.rb
209
211
  - spec/functional/response_spec.rb
@@ -274,6 +276,7 @@ test_files:
274
276
  - features/support/selenium-fix.rb
275
277
  - features/support/test-server.rb
276
278
  - spec/client/resource_double_spec.rb
279
+ - spec/custom_matchers.rb
277
280
  - spec/functional/double_routes_spec.rb
278
281
  - spec/functional/redirect_routes_spec.rb
279
282
  - spec/functional/response_spec.rb