Module: Angelo::Minitest::Helpers

Defined in:
lib/angelo/minitest/helpers.rb

Constant Summary collapse

HTTP_URL =
'http://%s:%d'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_responseObject (readonly)

Returns the value of attribute last_response.



10
11
12
# File 'lib/angelo/minitest/helpers.rb', line 10

def last_response
  @last_response
end

Instance Method Details

#define_app(&block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/angelo/minitest/helpers.rb', line 12

def define_app &block

  before do
    app = Class.new Angelo::Base
    app.class_eval { content_type :html } # reset
    app.class_eval &block
    @server = Angelo::Server.new app
    app.server = @server
    $reactor = Reactor.new unless $reactor.alive?
  end

  after do
    sleep 0.1
    @server.terminate if @server and @server.alive?
  end

end

#hcObject



30
31
32
# File 'lib/angelo/minitest/helpers.rb', line 30

def hc
  @hc ||= HTTPClient.new
end

#last_response_must_be_html(body = '') ⇒ Object



84
85
86
87
88
# File 'lib/angelo/minitest/helpers.rb', line 84

def last_response_must_be_html body = ''
  last_response.status.must_equal 200
  last_response.body.to_s.must_equal body
  last_response.headers['Content-Type'].split(';').must_include HTML_TYPE
end

#last_response_must_be_json(obj = {}) ⇒ Object



90
91
92
93
94
# File 'lib/angelo/minitest/helpers.rb', line 90

def last_response_must_be_json obj = {}
  last_response.status.must_equal 200
  JSON.parse(last_response.body.to_s).must_equal obj
  last_response.headers['Content-Type'].split(';').must_include JSON_TYPE
end

#websocket_helper(path, params = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/angelo/minitest/helpers.rb', line 68

def websocket_helper path, params = {}
  params = params.keys.reduce([]) {|a,k|
    a << CGI.escape(k) + '=' + CGI.escape(params[k])
    a
  }.join('&')

  path += "?#{params}" unless params.empty?
  wsh = WebsocketHelper.new DEFAULT_ADDR, DEFAULT_PORT, path
  if block_given?
    yield wsh
    wsh.close
  else
    return wsh
  end
end