pinch_hitter 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog CHANGED
@@ -1,2 +1,5 @@
1
1
  === Release 0.1 / 2013-1-20
2
2
  Initial release of the gem
3
+
4
+ === Release 0.2 / 2013-1-27
5
+ Switch json to do overrides by key rather than path (like xml)
data/README.md CHANGED
@@ -15,15 +15,20 @@ Or install it yourself as:
15
15
 
16
16
  $ gem install pinch_hitter
17
17
 
18
- ## Usage
18
+ ## Purpose
19
19
 
20
- Any xml posted to the /store endpoint will be returned (FIFO) when a post to the /respond endpoint is made.
20
+ Simulate those pesky external web services for testing and gain control over the data that's returned. Pinch Hitter can stand in for one or many services. The test controls the content that is served in an order it controls to the application under test.
21
21
 
22
- Start the service using rackup
23
- rackup
22
+ See the [wiki](https://github.com/stevenjackson/pinch_hitter/wiki) for more details
24
23
 
25
24
  See Rakefile for test options
26
25
 
26
+ ## Standalone
27
+
28
+ Start the service using rackup
29
+
30
+ $ rackup
31
+
27
32
  ## Contributing
28
33
 
29
34
  1. Fork it
@@ -33,3 +33,11 @@ end
33
33
  Then /^I see a definition$/ do
34
34
  @response.body.to_s.should == messages.load(:glossary).squish
35
35
  end
36
+
37
+ Given /^I want to lookup a definition with a "(.*?)" of "(.*?)"$/ do |key, value|
38
+ mock.prime '/glossary', :glossary, key => value
39
+ end
40
+
41
+ Then /^I see a definition with a "(.*?)" of "(.*?)"$/ do |key, value|
42
+ @response.body.to_s.should == messages.load(:glossary, { key => value }).squish
43
+ end
@@ -21,3 +21,7 @@ Feature: Test WS replay
21
21
  When I query the glossary
22
22
  Then I see a definition
23
23
 
24
+ Scenario: Replay with json substituion
25
+ Given I want to lookup a definition with a "ID" of "FOO"
26
+ When I query the glossary
27
+ Then I see a definition with a "ID" of "FOO"
data/lib/pinch_hitter.rb CHANGED
@@ -23,7 +23,11 @@ module PinchHitter
23
23
  end
24
24
 
25
25
  def prime(endpoint, message, overrides={})
26
- @session.post "/store?endpoint=#{endpoint}", message_store.load(message, overrides)
26
+ store endpoint, message_store.load(message, overrides)
27
+ end
28
+
29
+ def store(endpoint, content)
30
+ @session.post "/store?endpoint=#{endpoint}", content
27
31
  end
28
32
 
29
33
  end
@@ -22,17 +22,33 @@ module PinchHitter
22
22
 
23
23
  def replace_json(content, overrides={})
24
24
  return content if overrides.empty?
25
- modified = child = parent = JSON.parse(content).clone
26
- overrides.each do |key, value|
27
-
28
- key.each do |part|
29
- parent = child
30
- child = parent.send "fetch", part
25
+ doc = JSON.parse(content)
26
+ overrides.each do |key, value|
27
+ hash = find_nested_hash(doc, key)
28
+ if has_key(hash, key)
29
+ hash[key] = value
31
30
  end
32
- parent[key.last] = value
33
31
  end
34
- modified.to_s
32
+ doc.to_s
33
+ end
34
+
35
+ def find_nested_hash(parent, key)
36
+ return parent if has_key(parent, key)
37
+ return nil unless parent.respond_to? :each
38
+
39
+ found = nil
40
+ parent.find do |parent_key, child|
41
+ found = find_nested_hash(child, key)
42
+ end
43
+ found
35
44
  end
45
+
46
+ def has_key(hash, key)
47
+ hash.respond_to?(:key?) && hash.key?(key)
48
+ end
49
+
50
+
51
+
36
52
  end
37
53
  end
38
54
  end
@@ -20,17 +20,17 @@ module PinchHitter
20
20
 
21
21
  post '/reset' do
22
22
  @@responses.reset
23
- 200
23
+ status 200
24
24
  end
25
25
 
26
26
  post '/store/*' do
27
27
  store "/#{params[:splat].first}", request.body.read
28
- 200
28
+ status 200
29
29
  end
30
30
 
31
31
  post '/store' do
32
32
  store request["endpoint"], request.body.read
33
- 200
33
+ status 200
34
34
  end
35
35
 
36
36
  post '/respond' do
@@ -1,3 +1,3 @@
1
1
  module PinchHitter
2
- VERSION = "0.1"
2
+ VERSION = "0.2"
3
3
  end
@@ -25,6 +25,9 @@ class TestJsonMessage < MiniTest::Unit::TestCase
25
25
  "value": "File",
26
26
  "popup": {
27
27
  "menuitem": "OpenDoc()"
28
+ },
29
+ "trigger" : {
30
+ "action" : "confirm"
28
31
  }
29
32
  }}
30
33
  }
@@ -35,7 +38,7 @@ class TestJsonMessage < MiniTest::Unit::TestCase
35
38
  end
36
39
 
37
40
  def test_message_with_overrides
38
- json = @test.json_message(filename, {["menu", "popup", "menuitem"] => 'WhatsUpDoc?' })
41
+ json = @test.json_message(filename, {"menuitem" => 'WhatsUpDoc?' })
39
42
  assert json.include? "WhatsUpDoc?"
40
43
  end
41
44
  end
@@ -52,4 +52,10 @@ class TestPinchHitter < MiniTest::Unit::TestCase
52
52
  assert_equal message_content, session.last_response.body
53
53
  end
54
54
 
55
+ def test_store
56
+ @test.store '/foo', message_content
57
+ session.get '/foo'
58
+ assert_equal message_content, session.last_response.body
59
+ end
60
+
55
61
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pinch_hitter
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-21 00:00:00.000000000 Z
12
+ date: 2013-01-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
@@ -192,7 +192,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
192
192
  version: '0'
193
193
  segments:
194
194
  - 0
195
- hash: -4118053142123846714
195
+ hash: 2707663932851003695
196
196
  required_rubygems_version: !ruby/object:Gem::Requirement
197
197
  none: false
198
198
  requirements:
@@ -201,7 +201,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
201
201
  version: '0'
202
202
  segments:
203
203
  - 0
204
- hash: -4118053142123846714
204
+ hash: 2707663932851003695
205
205
  requirements: []
206
206
  rubyforge_project:
207
207
  rubygems_version: 1.8.24