class Cramp::TestCase

Public Instance Methods

app() click to toggle source
# File lib/cramp/test_case.rb, line 76
def app
  raise "Please define a method called 'app' returning an async Rack Application"
end
create_request() click to toggle source
# File lib/cramp/test_case.rb, line 9
def create_request
  @request = Rack::MockRequest.new(app)
end
delete(path, opts={}, headers={}, &block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
stop() click to toggle source
# File lib/cramp/test_case.rb, line 80
def stop
  EM.stop
end