Class: RSpec::Cramp::MockResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/cramp/mock_response.rb

Overview

Response from get, post etc. methods called in rspecs.

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ MockResponse

Returns a new instance of MockResponse.



6
7
8
9
10
# File 'lib/rspec/cramp/mock_response.rb', line 6

def initialize(response)
  @status = response[0]
  @headers = response[1]
  @body = response[2]
end

Instance Method Details

#[](i) ⇒ Object



33
34
35
# File 'lib/rspec/cramp/mock_response.rb', line 33

def [](i)
  [@status, @headers, @body][i]
end

#bodyObject



37
38
39
40
41
42
# File 'lib/rspec/cramp/mock_response.rb', line 37

def body
  if @body.is_a? ::Cramp::Body
    raise "Error: Something went wrong or body is not loaded yet (use response.read_body do { })."
  end
  @body
end

#headersObject



44
45
46
# File 'lib/rspec/cramp/mock_response.rb', line 44

def headers
  @headers
end

#last_failure_message_for_shouldObject



62
63
64
65
# File 'lib/rspec/cramp/mock_response.rb', line 62

def last_failure_message_for_should
  # TODO Better failure message showing the specific mismatches that made it fail.
  "expected #{@failure_info[:expected]} in #{@failure_info[:what].to_s} but got: #{@failure_info[:actual]}"
end

#last_failure_message_for_should_notObject



66
67
68
69
# File 'lib/rspec/cramp/mock_response.rb', line 66

def last_failure_message_for_should_not
  # TODO Better failure message showing the specific successful matches that made it fail.
  "expected response not to match the conditions but got: #{[@status, @headers, @body].inspect}"
end

#matching?(match_options) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
# File 'lib/rspec/cramp/mock_response.rb', line 52

def matching?(match_options)
  expected_status = match_options.delete(:status)
  expected_header = match_options.delete(:headers)
  expected_body = match_options.delete(:body)
  expected_chunks = match_options.delete(:chunks)
  raise "Unsupported match option" unless match_options.empty?
  matching_status?(expected_status) && matching_headers?(expected_header) && matching_body?(expected_body) &&
  matching_chunks?(expected_chunks)
end

#read_body(max_chunks = 1, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rspec/cramp/mock_response.rb', line 12

def read_body(max_chunks = 1, &block)
  if @body.is_a? ::Cramp::Body
    stopping = false
    deferred_body = @body
    chunks = []
    check = lambda do
      if chunks.count >= max_chunks
        @body = chunks
        stopping = true
        block.call if block
        EM.next_tick { EM.stop }
      end            
    end
    check.call
    deferred_body.each do |chunk|
      chunks << chunk unless stopping
      check.call
    end
  end
end

#statusObject



48
49
50
# File 'lib/rspec/cramp/mock_response.rb', line 48

def status
  @status
end