harper 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1,2 +1,2 @@
1
1
  rvm_install_on_use_flag=1
2
- rvm --create use ruby-1.9.3-p0@harper
2
+ rvm --create use ruby-1.9.3@harper
data/README.md CHANGED
@@ -61,6 +61,32 @@ Harper will silently replace it with the new mock.
61
61
  they should not be so generic that they appear in all requests to
62
62
  that url.
63
63
 
64
+ * *request_cookies*: This is a hash of key value pairs of cookies being
65
+ expected in the actual request, which will be used by harper for matching
66
+ before sending back the mock response.
67
+
68
+ sample usage:
69
+
70
+ harperClient.mock({:method => "POST",
71
+ :url => "/url",
72
+ :'content-type' => "application/xml",
73
+ :request_body => request_json,
74
+ :body => "response body",
75
+ :request_cookies => {"UserID" => "JohnDoe"}
76
+ })
77
+
78
+ * *cookies*: This is a hash of key value pairs of cookies that harper will
79
+ send back in the mock response.
80
+
81
+ sample usage:
82
+
83
+ harperClient.mock({:method => "POST",
84
+ :url => "/url",
85
+ :'content-type' => "application/xml",
86
+ :body => "response body",
87
+ :cookies => {"UserID" => "JohnDoe","sampleCookie" => "cookieValue"}
88
+ })
89
+
64
90
  ## Typical Use
65
91
 
66
92
  1. Start Harper at the very start of your test run.
@@ -3,6 +3,8 @@ require 'sinatra/base'
3
3
  require 'logger'
4
4
  require 'json'
5
5
 
6
+ require 'harper/mock_filter'
7
+
6
8
  module Harper
7
9
  class App < Sinatra::Base
8
10
 
@@ -20,6 +22,10 @@ module Harper
20
22
  end
21
23
 
22
24
  helpers do
25
+ def cookies(mocked_cookies)
26
+ mocked_cookies.each_pair { |k, v| response.set_cookie(k, v) } if mocked_cookies
27
+ end
28
+
23
29
  def logger
24
30
  LOGGER
25
31
  end
@@ -28,14 +34,23 @@ module Harper
28
34
  [url].pack('m').tr("+/=", "-_.").gsub("\n", '')
29
35
  end
30
36
 
31
- def retrieve_mock(mock_id, http_method, request_body)
37
+ def retrieve_mock(mock_id, request)
32
38
  if @@mocks[mock_id]
39
+ # If there is only one mock with the id, then ignore all other filters
33
40
  return @@mocks[mock_id].first if @@mocks[mock_id].length == 1
34
- mocks_for_requested_http_method = @@mocks[mock_id].select { |m| m['method'] == http_method}
35
- mock = mocks_for_requested_http_method.detect{|m| m["request_body"] && request_body =~ /#{m["request_body"]}/} if request_body
36
- mock ||= mocks_for_requested_http_method.detect{|m| m["request_body"].nil?}
41
+
42
+ # Mock parameters
43
+ method = request.request_method
44
+ body = request.body ? request.body.read : ''
45
+ cookies = request.cookies
46
+
47
+ mocks_for(mock_id).by_method(method).by_body(body).by_cookies(cookies).value
37
48
  end
38
49
  end
50
+
51
+ def mocks_for(mock_id)
52
+ MockFilter.new(@@mocks[mock_id])
53
+ end
39
54
  end
40
55
 
41
56
  post '/h/mocks' do
@@ -86,10 +101,10 @@ module Harper
86
101
 
87
102
  logger.debug("#{request.request_method} request for a mock: '#{request.path}'")
88
103
 
89
- request_body = request.body.read if request.body
90
- mock = retrieve_mock(mock_id, request.request_method, request_body)
104
+ mock = retrieve_mock(mock_id, request)
91
105
 
92
106
  if mock
107
+ cookies mock['cookies']
93
108
  content_type mock['content-type']
94
109
  status mock['status'] || "200"
95
110
  sleep mock['delay']
@@ -0,0 +1,33 @@
1
+ module Harper
2
+ class MockFilter
3
+
4
+ def initialize(mocks)
5
+ @mocks = mocks
6
+ end
7
+
8
+ def value
9
+ @mocks.first
10
+ end
11
+
12
+ def all
13
+ @mocks
14
+ end
15
+
16
+ def by_method(actual)
17
+ MockFilter.new(@mocks.select { |m| m['method'] == actual })
18
+ end
19
+
20
+ def by_body(actual)
21
+ MockFilter.new(@mocks.select { |m| !m['request_body'] || actual =~ /#{m["request_body"]}/ })
22
+ end
23
+
24
+ def by_cookies(actual)
25
+ MockFilter.new(@mocks.select { |m| !m['request_cookies'] || contains_all(m['request_cookies'], actual)})
26
+ end
27
+
28
+ def contains_all(sub, full)
29
+ (sub.to_a - full.to_a).empty?
30
+ end
31
+
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module Harper
2
- Version = "1.1.0"
2
+ Version = "1.2.0"
3
3
  end
@@ -15,7 +15,7 @@ describe Harper::App do
15
15
  let(:content_type) { "text/plain" }
16
16
  let(:body) { "fake body" }
17
17
  let(:delay) { 0 }
18
-
18
+
19
19
  let(:mock_def) do
20
20
  { :method => method,
21
21
  :url => url,
@@ -71,7 +71,7 @@ describe Harper::App do
71
71
  it "should respond with a 201 created" do
72
72
  last_response.status.should == 201
73
73
  end
74
-
74
+
75
75
  it "should point to a newly created mock resource" do
76
76
  last_response.headers['Location'].should match(%r{/h/mocks/})
77
77
  end
@@ -169,7 +169,7 @@ describe Harper::App do
169
169
  end
170
170
 
171
171
  context "controlling harper" do
172
-
172
+
173
173
  it "should exit, abruptly, on demand" do
174
174
  host = mock('hosting server')
175
175
  host.should_receive(:shutdown)
@@ -294,4 +294,114 @@ describe Harper::App do
294
294
  end
295
295
  end
296
296
 
297
+ context "support for cookies" do
298
+
299
+ after(:each) do
300
+ delete '/h/mocks'
301
+ end
302
+
303
+ it "should send back cookies registered in the mock" do
304
+ url = "/someUrl"
305
+ mock =
306
+ {:method => "POST",
307
+ :url => url,
308
+ :'content-type' => "application/xml",
309
+ :body => "body",
310
+ :cookies => {"UserID" => "JohnDoe","sampleCookie" => "cookieValue"}
311
+ }.to_json
312
+
313
+ post '/h/mocks', mock
314
+
315
+ post url
316
+
317
+ last_response.headers["Set-Cookie"].should == "UserID=JohnDoe\nsampleCookie=cookieValue"
318
+ end
319
+
320
+ it "should send back response based on match of cookies in the request with those registered in the mock" do
321
+ url = "/someUrl"
322
+ mock_with_no_cookie =
323
+ {:method => "POST",
324
+ :url => url,
325
+ :'content-type' => "application/xml",
326
+ :body => "body one"
327
+ }.to_json
328
+
329
+ mock_two =
330
+ {:method => "POST",
331
+ :url => url,
332
+ :'content-type' => "application/xml",
333
+ :body => "body two",
334
+ :request_cookies => {"UserID" => "JohnDoe"}
335
+ }.to_json
336
+
337
+ mock_three =
338
+ {:method => "POST",
339
+ :url => url,
340
+ :'content-type' => "application/xml",
341
+ :body => "body three",
342
+ :request_cookies => {"UserID" => "JohnDoe1"}
343
+ }.to_json
344
+
345
+
346
+ post '/h/mocks', mock_three
347
+ post '/h/mocks', mock_two
348
+ post '/h/mocks', mock_with_no_cookie
349
+
350
+ post url
351
+ last_response.body.should == "body one"
352
+
353
+ set_cookie "UserID=JohnDoe"
354
+ set_cookie "Key1=Value1"
355
+ post url
356
+ last_response.body.should == "body two"
357
+
358
+ set_cookie "UserID=JohnDoe1"
359
+ post url
360
+ last_response.body.should == "body three"
361
+ end
362
+
363
+ it "should send back response based on cookies in the request even if request body is the same" do
364
+ url = "/someUrl"
365
+
366
+ request_json = {:param => "request body 1"}.to_json
367
+ mock_one =
368
+ {:method => "POST",
369
+ :url => url,
370
+ :'content-type' => "application/xml",
371
+ :request_body => request_json,
372
+ :body => "body one",
373
+ :request_cookies => {"UserID" => "JohnDoe"}
374
+ }.to_json
375
+
376
+ mock_two =
377
+ {:method => "POST",
378
+ :url => url,
379
+ :'content-type' => "application/xml",
380
+ :body => "body two",
381
+ :request_body => request_json,
382
+ }.to_json
383
+
384
+ mock_three =
385
+ {:method => "POST",
386
+ :url => url,
387
+ :'content-type' => "application/xml",
388
+ :body => "body three",
389
+ }.to_json
390
+
391
+
392
+ post '/h/mocks', mock_one
393
+ post '/h/mocks', mock_two
394
+ post '/h/mocks', mock_three
395
+
396
+ post url
397
+ last_response.body.should == "body three"
398
+
399
+ post url, request_json
400
+ last_response.body.should == "body two"
401
+
402
+ set_cookie "UserID=JohnDoe"
403
+ post url, request_json
404
+ last_response.body.should == "body one"
405
+ end
406
+ end
297
407
  end
@@ -0,0 +1,75 @@
1
+ $: << File.dirname(__FILE__) + "/.."
2
+
3
+ require 'spec/spec_helper'
4
+ require 'harper/mock_filter'
5
+
6
+ describe Harper::MockFilter do
7
+
8
+ subject { Harper::MockFilter.new(starting_set) }
9
+
10
+ context "an empty set" do
11
+ let(:starting_set) { [] }
12
+
13
+ it('should be nil') { subject.value.should be_nil }
14
+ end
15
+
16
+ context "a set of one item" do
17
+ let(:starting_set) { [:a_mock] }
18
+
19
+ it('should be the single item') { subject.value.should be :a_mock }
20
+ end
21
+
22
+ context "a set of matching mocks" do
23
+ let(:starting_set) { [{'id' => 'first'}.merge(attrs), {'id' => 'second'}.merge(attrs)] }
24
+
25
+ context "by methods" do
26
+ let(:attrs) { {'method' => 'matching'} }
27
+
28
+ it 'should return the first when filtered by method' do
29
+ subject.by_method('matching').value['id'].should == 'first'
30
+ end
31
+ end
32
+
33
+ context "by bodies" do
34
+ let(:attrs) { {'request_body' => "match"} }
35
+
36
+ it 'should return the first when filtered by body' do
37
+ subject.by_body('matching').value['id'].should == 'first'
38
+ end
39
+ end
40
+
41
+ context "by cookies" do
42
+ let(:attrs) { {'request_cookies' => {'c1' => 'v1'} } }
43
+
44
+ it 'should return the first when filtered by cookies' do
45
+ subject.by_cookies('c1' => 'v1', 'c2' => 'v2').value['id'].should == 'first'
46
+ end
47
+ end
48
+ end
49
+
50
+ context "a set of five mocks" do
51
+ let(:put_mock) { {'method' => 'put'} }
52
+ let(:with_body) { {'method' => 'get', 'request_body' => 'body'} }
53
+ let(:nothing) { {'method' => 'get'} }
54
+ let(:with_cookies) { {'method' => 'get', 'request_cookies' => {'c' => 'v'}} }
55
+ let(:both) { {'method' => 'get', 'request_body' => 'body', 'request_cookies' => {'c' => 'v'}} }
56
+
57
+ let(:starting_set) { [put_mock, with_body, nothing, with_cookies, both] }
58
+
59
+ context 'filtered by method' do
60
+ it("should find one mock with method 'put'") { subject.by_method('put').all.should have(1).mocks }
61
+ it("should find four mocks with method 'get'") { subject.by_method('get').all.should have(4).mocks }
62
+ end
63
+
64
+ context 'filtered by body' do
65
+ it("should find five mocks with body 'body'") { subject.by_body('body').all.should have(5).mocks }
66
+ it("should find three mocks with body 'unspecified'") { subject.by_body('unspecified').all.should have(3).mocks }
67
+ end
68
+
69
+ context 'filtered by cookies' do
70
+ it("should find five mocks with cookie 'c'") { subject.by_cookies('c' => 'v').all.should have(5).mocks }
71
+ it("should find three mocks with cookie 'c' = 'other'") { subject.by_cookies('c' => 'other').all.should have(3).mocks }
72
+ end
73
+ end
74
+
75
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: harper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-10 00:00:00.000000000 Z
12
+ date: 2013-01-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
16
- requirement: &70183758661480 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: 1.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70183758661480
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.0
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: httparty
27
- requirement: &70183758661020 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *70183758661020
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: json
38
- requirement: &70183758676260 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: 1.4.6
44
54
  type: :runtime
45
55
  prerelease: false
46
- version_requirements: *70183758676260
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.4.6
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: trollop
49
- requirement: &70183758674400 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,10 +69,15 @@ dependencies:
54
69
  version: '0'
55
70
  type: :runtime
56
71
  prerelease: false
57
- version_requirements: *70183758674400
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  - !ruby/object:Gem::Dependency
59
79
  name: rack-test
60
- requirement: &70183758673380 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
61
81
  none: false
62
82
  requirements:
63
83
  - - ! '>='
@@ -65,10 +85,15 @@ dependencies:
65
85
  version: '0'
66
86
  type: :development
67
87
  prerelease: false
68
- version_requirements: *70183758673380
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
69
94
  - !ruby/object:Gem::Dependency
70
95
  name: sham_rack
71
- requirement: &70183758672900 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
72
97
  none: false
73
98
  requirements:
74
99
  - - ! '>='
@@ -76,10 +101,15 @@ dependencies:
76
101
  version: '0'
77
102
  type: :development
78
103
  prerelease: false
79
- version_requirements: *70183758672900
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
80
110
  - !ruby/object:Gem::Dependency
81
111
  name: rspec
82
- requirement: &70183758672340 !ruby/object:Gem::Requirement
112
+ requirement: !ruby/object:Gem::Requirement
83
113
  none: false
84
114
  requirements:
85
115
  - - ! '>='
@@ -87,10 +117,15 @@ dependencies:
87
117
  version: '0'
88
118
  type: :development
89
119
  prerelease: false
90
- version_requirements: *70183758672340
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
91
126
  - !ruby/object:Gem::Dependency
92
127
  name: cucumber
93
- requirement: &70183758671800 !ruby/object:Gem::Requirement
128
+ requirement: !ruby/object:Gem::Requirement
94
129
  none: false
95
130
  requirements:
96
131
  - - ! '>='
@@ -98,10 +133,15 @@ dependencies:
98
133
  version: '0.10'
99
134
  type: :development
100
135
  prerelease: false
101
- version_requirements: *70183758671800
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0.10'
102
142
  - !ruby/object:Gem::Dependency
103
143
  name: cucumber-sinatra
104
- requirement: &70183758670580 !ruby/object:Gem::Requirement
144
+ requirement: !ruby/object:Gem::Requirement
105
145
  none: false
106
146
  requirements:
107
147
  - - ! '>='
@@ -109,7 +149,12 @@ dependencies:
109
149
  version: '0'
110
150
  type: :development
111
151
  prerelease: false
112
- version_requirements: *70183758670580
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
113
158
  description: Dead simple mocking for HTTP services, a Sinatra app
114
159
  email:
115
160
  - giles.alexander@gmail.com
@@ -136,9 +181,11 @@ files:
136
181
  - lib/harper.rb
137
182
  - lib/harper/app.rb
138
183
  - lib/harper/client.rb
184
+ - lib/harper/mock_filter.rb
139
185
  - lib/harper/version.rb
140
186
  - spec/client_spec.rb
141
187
  - spec/harper_spec.rb
188
+ - spec/mock_filter_spec.rb
142
189
  - spec/spec_helper.rb
143
190
  homepage: http://github.com/gga/harper
144
191
  licenses: []
@@ -160,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
207
  version: 1.3.6
161
208
  requirements: []
162
209
  rubyforge_project: harper
163
- rubygems_version: 1.8.10
210
+ rubygems_version: 1.8.24
164
211
  signing_key:
165
212
  specification_version: 3
166
213
  summary: Dead simple mocking for HTTP services
@@ -174,4 +221,5 @@ test_files:
174
221
  - features/support/paths.rb
175
222
  - spec/client_spec.rb
176
223
  - spec/harper_spec.rb
224
+ - spec/mock_filter_spec.rb
177
225
  - spec/spec_helper.rb