Class: Shokkenki::Consumer::Stubber::LocalServerStubber
- Inherits:
-
Object
- Object
- Shokkenki::Consumer::Stubber::LocalServerStubber
- Defined in:
- lib/shokkenki/consumer/stubber/local_server_stubber.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#interactions_path ⇒ Object
readonly
Returns the value of attribute interactions_path.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#scheme ⇒ Object
readonly
Returns the value of attribute scheme.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
- #clear_interaction_stubs ⇒ Object
-
#initialize(attributes) ⇒ LocalServerStubber
constructor
A new instance of LocalServerStubber.
- #interactions_uri ⇒ Object
- #server_properties ⇒ Object
- #session_closed ⇒ Object
- #session_started ⇒ Object
- #stub_interaction(interaction) ⇒ Object
- #successful?(response_code) ⇒ Boolean
- #type ⇒ Object
- #unmatched_requests ⇒ Object
- #unmatched_requests_uri ⇒ Object
- #unused_interactions ⇒ Object
- #unused_interactions_uri ⇒ Object
Constructor Details
#initialize(attributes) ⇒ LocalServerStubber
Returns a new instance of LocalServerStubber.
14 15 16 17 18 19 20 21 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 14 def initialize attributes @port = attributes[:port] @scheme = attributes[:scheme] || :http @host = attributes[:host] || 'localhost' @interactions_path = attributes[:interactions_path] || '/shokkenki/interactions' @unmatched_requests_path = '/shokkenki/requests/unmatched' @unused_interactions_path = '/shokkenki/interactions/unused' end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
12 13 14 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 12 def host @host end |
#interactions_path ⇒ Object (readonly)
Returns the value of attribute interactions_path.
12 13 14 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 12 def interactions_path @interactions_path end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
12 13 14 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 12 def port @port end |
#scheme ⇒ Object (readonly)
Returns the value of attribute scheme.
12 13 14 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 12 def scheme @scheme end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
12 13 14 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 12 def server @server end |
Instance Method Details
#clear_interaction_stubs ⇒ Object
36 37 38 39 40 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 36 def clear_interaction_stubs response = HTTParty.delete interactions_uri server.assert_ok! raise "Failed to clear interaction stubs: #{response.inspect}" unless successful?(response.code) end |
#interactions_uri ⇒ Object
86 87 88 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 86 def interactions_uri URI::Generic.build(server_properties.merge(:path => @interactions_path.to_s)).to_s end |
#server_properties ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 78 def server_properties { :scheme => @scheme.to_s, :host => @host.to_s, :port => @port } end |
#session_closed ⇒ Object
60 61 62 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 60 def session_closed @server.shutdown end |
#session_started ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 46 def session_started # Find a port as late as possible to minimise the # chance that it starts being used in between finding it # and using it. @port = FindAPort.available_port unless @port @server = Server.new( :app => StubServerMiddleware.new, :host => @host, :port => @port ) @server.start end |
#stub_interaction(interaction) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 27 def stub_interaction interaction response = HTTParty.post(interactions_uri, :body => interaction.to_hash.to_json, :headers => { 'Content-Type' => 'application/json' } ) server.assert_ok! raise "Failed to stub interaction: #{response.inspect}" unless successful?(response.code) end |
#successful?(response_code) ⇒ Boolean
42 43 44 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 42 def successful? response_code (200 <= response_code) && (response_code < 300) end |
#type ⇒ Object
23 24 25 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 23 def type :local_server end |
#unmatched_requests ⇒ Object
64 65 66 67 68 69 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 64 def unmatched_requests response = HTTParty.get unmatched_requests_uri server.assert_ok! raise "Failed to find unmatched requests: #{response.inspect}" unless successful?(response.code) JSON.parse response.body end |
#unmatched_requests_uri ⇒ Object
90 91 92 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 90 def unmatched_requests_uri URI::Generic.build(server_properties.merge(:path => @unmatched_requests_path.to_s)).to_s end |
#unused_interactions ⇒ Object
71 72 73 74 75 76 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 71 def unused_interactions response = HTTParty.get unused_interactions_uri server.assert_ok! raise "Failed to find unused interactions: #{response.inspect}" unless successful?(response.code) JSON.parse response.body end |
#unused_interactions_uri ⇒ Object
94 95 96 |
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 94 def unused_interactions_uri URI::Generic.build(server_properties.merge(:path => @unused_interactions_path.to_s)).to_s end |