Class: Greenlight::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/greenlight/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options) ⇒ Request

Returns a new instance of Request.



34
35
36
37
38
# File 'lib/greenlight/request.rb', line 34

def initialize(url, options)
  self.url = url
  self.options = options
  self.expectations = []
end

Instance Attribute Details

#assert_noObject

count assertions to report the index of the failed one



31
32
33
# File 'lib/greenlight/request.rb', line 31

def assert_no
  @assert_no
end

#expectationsObject

request assertions



28
29
30
# File 'lib/greenlight/request.rb', line 28

def expectations
  @expectations
end

#optionsObject

request description



19
20
21
# File 'lib/greenlight/request.rb', line 19

def options
  @options
end

#req_responseObject

response structure with json parsed body



25
26
27
# File 'lib/greenlight/request.rb', line 25

def req_response
  @req_response
end

#responseObject

request response



22
23
24
# File 'lib/greenlight/request.rb', line 22

def response
  @response
end

#urlObject

request description



19
20
21
# File 'lib/greenlight/request.rb', line 19

def url
  @url
end

Instance Method Details

#_debug_infoObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/greenlight/request.rb', line 46

def _debug_info
  if code != 0
    info "response status code: #{code}"
  else
    info "library returned: #{response.return_code}"
  end
  info "request body: #{options[:body]}"
  info "request headers: #{options[:headers]}"
  info "response body: #{body}"
  info "response headers: #{response.headers}"

end

#assert(condition) ⇒ Object

define assertion



60
61
62
63
64
65
66
67
68
# File 'lib/greenlight/request.rb', line 60

def assert(condition)
  unless condition
    error "assertion no. #{assert_no} failed"
    _debug_info
    raise AssertionException
  end

  self.assert_no = assert_no + 1
end

#bodyObject



87
88
89
# File 'lib/greenlight/request.rb', line 87

def body
  req_response.body
end

#codeObject



79
80
81
# File 'lib/greenlight/request.rb', line 79

def code
  response.code
end

#error_msgObject



164
165
166
# File 'lib/greenlight/request.rb', line 164

def error_msg
  "REQUEST '#{options[:method].upcase} #{url}' failed"
end

#expect(&block) ⇒ Object

define list of expectations (assertions)



41
42
43
44
# File 'lib/greenlight/request.rb', line 41

def expect(&block)
  self.expectations = block
  run
end

#header(name) ⇒ Object

assertion helpers



71
72
73
# File 'lib/greenlight/request.rb', line 71

def header(name)
  response.headers[name]
end

#headersObject



75
76
77
# File 'lib/greenlight/request.rb', line 75

def headers
  response.headers
end

#raw_bodyObject



83
84
85
# File 'lib/greenlight/request.rb', line 83

def raw_body
  response.body
end

#runObject

run the request and evaluate expectations



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/greenlight/request.rb', line 96

def run

  action Colors.grey("REQUEST ") + Colors.light_blue("#{options[:method].upcase} #{url}")
  Console.instance.indent
  # run the request
  options[:ssl_verifypeer] = false
  options[:followlocation] = true

  Injector.decorate(options)

  # convert all headers keys to strings to avoid having symbols like :"header" when
  # declaring headers with colons instead of arrows
  if options.key?(:headers)
    new_opts = {}
    options[:headers].map do |k, v|
      new_opts[k.to_s] = v
    end
    options[:headers] = new_opts
  end

  if options.key?(:headers) and options[:headers].key?('Content-Type')
    ctype = options[:headers]['Content-Type']
    if ctype.include?('application/json')
      # automatically encode json content
      options[:body] = JSON.generate(options[:body], quirks_mode: true)
    end
  end



  self.response = Typhoeus::Request.new(url, options).run

  self.req_response = RequestResponse.new.tap { |r|
    r.raw_body = response.body
    r.headers = response.headers
    r.code = response.code
    r.total_time = response.total_time

    if !r.headers.nil? && r.headers.key?('Content-Type') && r.headers['Content-Type'].include?('application/json')
      r.body = JSON.parse(response.body)
    else
      r.body = response.body
    end
  }

  # reset assertion counter
  self.assert_no = 1

  # evaluate response against expectations
  begin
    instance_eval(&expectations)
  rescue AssertionException
    error error_msg + " at #{expectations.source_location}"
    raise RequestException
  rescue StandardError => e
    error 'Exception ' + e.message
    info e.backtrace.inspect
    _debug_info
    error error_msg
    raise RequestException
  ensure
    Console.instance.unindent
  end

  req_response

end

#total_timeObject



91
92
93
# File 'lib/greenlight/request.rb', line 91

def total_time
  response.total_time
end