Class: Tap::Mechanize::Test::MockServer

Inherits:
Object
  • Object
show all
Defined in:
lib/tap/mechanize/test/mock_server.rb

Overview

MockServer allows easy creation of a lazy Rack application that calls the block for content. The status and headers of the response are setup during initialize.

env = Rack::MockRequest.env_for('http://localhost:2000/')

m = MockServer.new {|env| ['yo'] }
m.call(env)       # => [200, {'Content-Type' => 'text/html'}, ['yo']]

Instance Method Summary collapse

Constructor Details

#initialize(status = 200, headers = {'Content-Type' => 'text/html'}, &block) ⇒ MockServer

Returns a new instance of MockServer.



15
16
17
18
19
# File 'lib/tap/mechanize/test/mock_server.rb', line 15

def initialize(status=200, headers={'Content-Type' => 'text/html'}, &block)
  @status = status
  @headers = headers
  @block = block
end

Instance Method Details

#call(env) ⇒ Object

Calls the initialization block with env. The block must return the content of the response.

Returns: [status, headers, block-return]



25
26
27
# File 'lib/tap/mechanize/test/mock_server.rb', line 25

def call(env)
  [@status, @headers, @block.call(env)]
end