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
29
30
31
# 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
    Celluloid.logger.level = ::Logger::ERROR # see spec_helper.rb:9

    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

#get_sse(path, params = {}, headers = {}, &block) ⇒ Object



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

def get_sse path, params = {}, headers = {}, &block
  @last_response = hc.get url(path), params, headers, &block
end

#hcObject



33
34
35
# File 'lib/angelo/minitest/helpers.rb', line 33

def hc
  @hc ||= HTTPClient.new
end

#last_response_must_be_html(body = '') ⇒ Object



105
106
107
108
109
# File 'lib/angelo/minitest/helpers.rb', line 105

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



111
112
113
114
115
# File 'lib/angelo/minitest/helpers.rb', line 111

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

#url(path = nil) ⇒ Object



37
38
39
40
41
# File 'lib/angelo/minitest/helpers.rb', line 37

def url path = nil
  url = HTTP_URL % [DEFAULT_ADDR, DEFAULT_PORT]
  url += path if path
  url
end

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/angelo/minitest/helpers.rb', line 89

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