Class: Cramp::TestCase

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

Instance Method Summary collapse

Instance Method Details

#appObject



76
77
78
# File 'lib/cramp/test_case.rb', line 76

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

#delete(path, opts = {}, headers = {}, &block) ⇒ Object



16
# File 'lib/cramp/test_case.rb', line 16

def delete(path, opts={}, headers={}, &block)  request(:delete, path, opts, headers, &block)  end

#delete_body(path, opts = {}, headers = {}, &block) ⇒ Object



31
# File 'lib/cramp/test_case.rb', line 31

def delete_body(path, opts={}, headers={}, &block)  request_body(:delete, path, opts, headers, &block)  end

#delete_body_chunks(path, opts = {}, headers = {}, &block) ⇒ Object



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

def delete_body_chunks(path, opts={}, headers={}, &block)  request_body_chunks(:delete, path, opts, headers, &block)  end

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



13
# File 'lib/cramp/test_case.rb', line 13

def get(path, opts={}, headers={}, &block)     request(:get, path, opts, headers, &block)     end

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



28
# File 'lib/cramp/test_case.rb', line 28

def get_body(path, opts={}, headers={}, &block)     request_body(:get, path, opts, headers, &block)     end

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



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

def get_body_chunks(path, opts={}, headers={}, &block)     request_body_chunks(:get, path, opts, headers, &block)     end

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



17
# File 'lib/cramp/test_case.rb', line 17

def options(path, opts={}, headers={}, &block) request(:options, path, opts, headers, &block) end

#options_body(path, opts = {}, headers = {}, &block) ⇒ Object



32
# File 'lib/cramp/test_case.rb', line 32

def options_body(path, opts={}, headers={}, &block) request_body(:options, path, opts, headers, &block) end

#options_body_chunks(path, opts = {}, headers = {}, &block) ⇒ Object



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

def options_body_chunks(path, opts={}, headers={}, &block) request_body_chunks(:options, path, opts, headers, &block) end

#post(path, opts = {}, headers = {}, &block) ⇒ Object



14
# File 'lib/cramp/test_case.rb', line 14

def post(path, opts={}, headers={}, &block)    request(:post, path, opts, headers, &block)    end

#post_body(path, opts = {}, headers = {}, &block) ⇒ Object



29
# File 'lib/cramp/test_case.rb', line 29

def post_body(path, opts={}, headers={}, &block)    request_body(:post, path, opts, headers, &block)    end

#post_body_chunks(path, opts = {}, headers = {}, &block) ⇒ Object



53
# File 'lib/cramp/test_case.rb', line 53

def post_body_chunks(path, opts={}, headers={}, &block)    request_body_chunks(:post, path, opts, headers, &block)    end

#put(path, opts = {}, headers = {}, &block) ⇒ Object



15
# File 'lib/cramp/test_case.rb', line 15

def put(path, opts={}, headers={}, &block)     request(:put, path, opts, headers, &block)     end

#put_body(path, opts = {}, headers = {}, &block) ⇒ Object



30
# File 'lib/cramp/test_case.rb', line 30

def put_body(path, opts={}, headers={}, &block)     request_body(:put, path, opts, headers, &block)     end

#put_body_chunks(path, opts = {}, headers = {}, &block) ⇒ Object



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

def put_body_chunks(path, opts={}, headers={}, &block)     request_body_chunks(:put, path, opts, headers, &block)     end

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



19
20
21
22
23
24
25
26
# File 'lib/cramp/test_case.rb', line 19

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

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

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



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

def request_body(method, path, options = {}, headers = {}, &block)
  callback = options.delete(:callback) || block
  response_callback = proc do |response|
    # 'halt' returns a String, not an async Body object
    if response.last.is_a? String
      callback.call(response.last)
    else
      response.last.each {|chunk| callback.call(chunk) }
    end
  end
  headers = headers.merge('async.callback' => response_callback)

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

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cramp/test_case.rb', line 58

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

  stopping = false
  chunks = []

  request_body(method, 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



80
81
82
# File 'lib/cramp/test_case.rb', line 80

def stop
  EM.stop
end