Class: RSpec::Cramp::MockResponse
- Inherits:
-
Object
- Object
- RSpec::Cramp::MockResponse
- Defined in:
- lib/rspec/cramp/mock_response.rb
Overview
Response from get, post etc. methods called in rspecs.
Instance Method Summary collapse
- #[](i) ⇒ Object
- #body ⇒ Object
- #headers ⇒ Object
-
#initialize(response) ⇒ MockResponse
constructor
A new instance of MockResponse.
- #last_failure_message_for_should ⇒ Object
- #last_failure_message_for_should_not ⇒ Object
- #matching?(match_options) ⇒ Boolean
- #read_body(max_chunks = 1, &block) ⇒ Object
- #status ⇒ Object
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 |
#body ⇒ Object
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 |
#headers ⇒ Object
44 45 46 |
# File 'lib/rspec/cramp/mock_response.rb', line 44 def headers @headers end |
#last_failure_message_for_should ⇒ Object
62 63 64 65 |
# File 'lib/rspec/cramp/mock_response.rb', line 62 def # 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_not ⇒ Object
66 67 68 69 |
# File 'lib/rspec/cramp/mock_response.rb', line 66 def # 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
52 53 54 55 56 57 58 59 60 |
# File 'lib/rspec/cramp/mock_response.rb', line 52 def matching?() expected_status = .delete(:status) expected_header = .delete(:headers) expected_body = .delete(:body) expected_chunks = .delete(:chunks) raise "Unsupported match option" unless .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 |
#status ⇒ Object
48 49 50 |
# File 'lib/rspec/cramp/mock_response.rb', line 48 def status @status end |