Class: Shokkenki::Consumer::Stubber::LocalServerStubber

Inherits:
Object
  • Object
show all
Defined in:
lib/shokkenki/consumer/stubber/local_server_stubber.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#hostObject (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_pathObject (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

#portObject (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

#schemeObject (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

#serverObject (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_stubsObject



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_uriObject



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_propertiesObject



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_closedObject



60
61
62
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 60

def session_closed
  @server.shutdown
end

#session_startedObject



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

Returns:

  • (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

#typeObject



23
24
25
# File 'lib/shokkenki/consumer/stubber/local_server_stubber.rb', line 23

def type
  :local_server
end

#unmatched_requestsObject



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_uriObject



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_interactionsObject



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_uriObject



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