Class: Rectory::Expectation

Inherits:
Object
  • Object
show all
Defined in:
lib/rectory/expectation.rb

Overview

The expectations for an HTTP request.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, args = {}) ⇒ Expectation

Returns a new instance of Expectation.

Parameters:

  • url (String)

    The URL against which to test an HTTP reponse

  • args (Hash) (defaults to: {})

    A hash of HTTP response expectations (‘:code`, `:location`)

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rectory/expectation.rb', line 15

def initialize(url, args = {})
  raise ArgumentError, "You must provide a URL to test!" if url.nil?

  defaults = {
    :code   => 200,
    :result => {}
  }

  args          = defaults.merge args
  self.url      = url
  self.code     = args[:code].to_i
  self.result   = args[:result]
  self.location = args[:location]
end

Instance Attribute Details

#codeObject

The expected HTTP status code. (200, 302, 304, 404, 400, etc)

Returns:

  • (Object)

    the current value of code



9
10
11
# File 'lib/rectory/expectation.rb', line 9

def code
  @code
end

#locationObject

The expected HTTP ‘Location` header, or the end location of a redirect. Should be `nil` if the expectation is not a redirect (200, 304, 404, etc)

Returns:

  • (Object)

    the current value of location



9
10
11
# File 'lib/rectory/expectation.rb', line 9

def location
  @location
end

#resultObject

A Rectory::Result instance which contains HTTP repsonse details

Returns:

  • (Object)

    the current value of result



9
10
11
# File 'lib/rectory/expectation.rb', line 9

def result
  @result
end

#urlObject

The URL to test

Returns:

  • (Object)

    the current value of url



9
10
11
# File 'lib/rectory/expectation.rb', line 9

def url
  @url
end

Instance Method Details

#pass?Boolean

Determines whether the HTTP result satifies the expectations

Verifies that both status code and location match

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/rectory/expectation.rb', line 35

def pass?
  perform if result.empty?
  truths = []
  truths << (result[:location].to_s.gsub(/\/$/, "") == location.to_s.gsub(/\/$/, ""))
  truths << (result[:code] == code.to_i) unless code.nil?
  truths.all?
end