Class: CocaineRuntimeMock
Defined Under Namespace
Modules: Server
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ CocaineRuntimeMock
constructor
A new instance of CocaineRuntimeMock.
- #on(name, request, response) ⇒ Object
- #register(name, session, options = {}) ⇒ Object
- #run ⇒ Object
- #stop ⇒ Object
- #when(name) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ CocaineRuntimeMock
Returns a new instance of CocaineRuntimeMock.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/cocaine/testing/mock_server.rb', line 73 def initialize( = {}) = {:host => 'localhost', :port => 10053}.merge() @host = [:host] @port = [:port] @services = {} @responses = {} @hooks = {} @servers = [] register 'locator', 0, endpoint: [@host, @port], version: 1, api: {} end |
Instance Method Details
#on(name, request, response) ⇒ Object
101 102 103 104 |
# File 'lib/cocaine/testing/mock_server.rb', line 101 def on(name, request, response) @responses[name] ||= {} @responses[name][request] = response end |
#register(name, session, options = {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/cocaine/testing/mock_server.rb', line 88 def register(name, session, ={}) raise "service '#{name}' already registered" if @services.has_key? name $log.debug "registering '#{name}' service" = {endpoint: ['localhost', 0], version: 1, api: {}}.merge @services[name] = on 'locator', [0, session, [name]], [[[:endpoint], [:version], [:api]]] end |
#run ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/cocaine/testing/mock_server.rb', line 110 def run @services.each do |name, | $log.debug "starting '#{name}' service at #{options[:endpoint]}" sig = EM::start_server *[:endpoint], Server, name, @responses[name], @hooks[name], @servers.push sig end end |
#stop ⇒ Object
118 119 120 121 122 |
# File 'lib/cocaine/testing/mock_server.rb', line 118 def stop @servers.each do |sig| EM.stop_server sig end end |
#when(name) ⇒ Object
106 107 108 |
# File 'lib/cocaine/testing/mock_server.rb', line 106 def when(name) @hooks[name] ||= Hook.new end |