Module: Bacon::HTTPHelpers

Defined in:
lib/bacon/ext/http.rb

Instance Method Summary collapse

Instance Method Details

#check_http_response_status(req, expected, body_regexp = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bacon/ext/http.rb', line 38

def check_http_response_status(req, expected, body_regexp = nil)
  status = req.response_header.status
  unless status == expected
    raise Bacon::Error.new(:failed, "expected status was #{expected}, got #{status} : #{req.response}")
  end

  if body_regexp
    body_ok = false
    if body_regexp.is_a?(Regexp)
      body_ok = (req.response =~ body_regexp)
    else
      body_ok = (req.response == body_regexp)
    end
    
    unless body_ok
      raise Bacon::Error.new(:failed, "body does not match\n\texpected\t\"#{body_regexp}\"\n\tgot\t\t\"#{req.response}\"")
    end
  end

  # just to avoid empty specification error
  status.should == expected
end

#http_request(method, path, args = {}, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bacon/ext/http.rb', line 26

def http_request(method, path, args = {}, &block)
  headers = args.delete(:headers) || {}
  body = args.delete(:body)
  params = args.delete(:params) || {}
  
  request_data = {:path => path, :query => params, :head => headers, :body => body}
  
  req = EM::HttpRequest.new("http://127.0.0.1:#{$bacon_http_port}#{path}").send(method, request_data)
  req.callback(&block)
  req
end

#start_server(rack_app = nil, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bacon/ext/http.rb', line 12

def start_server(rack_app = nil, &block)
  $bacon_http_port ||= 11000
  $bacon_http_port+= 1
  
  if rack_app && rack_app.respond_to?(:call)
    block = proc{
      run rack_app
    }
  end
  
  Thin::Logging.silent = true
  Thin::Server.start('127.0.0.1', $bacon_http_port, &block)
end