Class: Cramp::TestCase

Inherits:
ActiveSupport::TestCase
  • Object
show all
Defined in:
lib/cramp/test_case.rb

Instance Method Summary collapse

Instance Method Details

#appObject



50
51
52
# File 'lib/cramp/test_case.rb', line 50

def app
  raise "Please define a method called 'app' returning an async Rack Application"
end

#create_requestObject



9
10
11
# File 'lib/cramp/test_case.rb', line 9

def create_request
  @request = Rack::MockRequest.new(app)
end

#get(path, options = {}, headers = {}, &block) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/cramp/test_case.rb', line 13

def get(path, options = {}, headers = {}, &block)
  callback = options.delete(:callback) || block
  headers = headers.merge('async.callback' => callback)

  EM.run do
    catch(:async) { @request.get(path, headers) }
  end
end

#get_body(path, options = {}, headers = {}, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/cramp/test_case.rb', line 22

def get_body(path, options = {}, headers = {}, &block)
  callback = options.delete(:callback) || block
  response_callback = proc {|response| response[-1].each {|chunk| callback.call(chunk) } }
  headers = headers.merge('async.callback' => response_callback)

  EM.run do
    catch(:async) { @request.get(path, headers) }
  end
end

#get_body_chunks(path, options = {}, headers = {}, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cramp/test_case.rb', line 32

def get_body_chunks(path, options = {}, headers = {}, &block)
  callback = options.delete(:callback) || block
  count = options.delete(:count) || 1

  stopping = false
  chunks = []

  get_body(path, options, headers) do |body_chunk|
    chunks << body_chunk unless stopping

    if chunks.count >= count
      stopping = true
      callback.call(chunks) if callback
      EM.next_tick { EM.stop }
    end
  end
end

#stopObject



54
55
56
# File 'lib/cramp/test_case.rb', line 54

def stop
  EM.stop
end