class Hyperion::FakeServer
Constants
- Request
- Rule
Attributes
port[RW]
Public Class Methods
new(port)
click to toggle source
# File lib/hyperion_test/fake_server.rb, line 20 def initialize(port) @port = port @kim = Kim.new(port: port) @kim.start end
Public Instance Methods
clear_routes()
click to toggle source
# File lib/hyperion_test/fake_server.rb, line 38 def clear_routes @kim.clear_handlers end
configure(&configure_routes)
click to toggle source
# File lib/hyperion_test/fake_server.rb, line 26 def configure(&configure_routes) config = Config.new configure_routes.call(config) config.rules.each do |rule| matcher = Kim::Matcher.and(verb(rule.verb), res(rule.path), req_headers(rule.headers)) handler = wrap(rule.handler, rule.rest_route) @kim.add_handler(matcher, &handler) end end
teardown()
click to toggle source
# File lib/hyperion_test/fake_server.rb, line 42 def teardown @kim.stop end
Private Instance Methods
massage_request!(req)
click to toggle source
# File lib/hyperion_test/fake_server.rb, line 57 def massage_request!(req) if req.body && !req.body.empty? req.body = read(req.body, :json) end end
massage_response(resp, rest_route)
click to toggle source
# File lib/hyperion_test/fake_server.rb, line 63 def massage_response(resp, rest_route) if rack_response?(resp) code, headers, body = resp unless body.is_a?(String) body = write(body, :json) end [code, headers, body] else if rest_route rd = rest_route.response_descriptor content_type = content_type_for(rd) format = rd else content_type = 'application/json' format = :json end ['200', {'Content-Type' => content_type}, write(resp, format)] end end
rack_response?(resp)
click to toggle source
# File lib/hyperion_test/fake_server.rb, line 83 def rack_response?(resp) resp.is_a?(Array) && resp.size == 3 end
wrap(handler, rest_route)
click to toggle source
Make it easier to write handlers by massaging input and output
# File lib/hyperion_test/fake_server.rb, line 49 def wrap(handler, rest_route) proc do |req| massage_request!(req) resp = handler.call(req) massage_response(resp, rest_route) end end