Class: Greenlight::Request
- Inherits:
-
Object
- Object
- Greenlight::Request
- Defined in:
- lib/greenlight/request.rb
Instance Attribute Summary collapse
-
#assert_no ⇒ Object
count assertions to report the index of the failed one.
-
#expectations ⇒ Object
request assertions.
-
#options ⇒ Object
request description.
-
#req_response ⇒ Object
response structure with json parsed body.
-
#response ⇒ Object
request response.
-
#url ⇒ Object
request description.
Instance Method Summary collapse
- #_debug_info ⇒ Object
-
#assert(condition) ⇒ Object
define assertion.
- #body ⇒ Object
- #code ⇒ Object
- #error_msg ⇒ Object
-
#expect(&block) ⇒ Object
define list of expectations (assertions).
-
#header(name) ⇒ Object
assertion helpers.
- #headers ⇒ Object
-
#initialize(url, options) ⇒ Request
constructor
A new instance of Request.
- #raw_body ⇒ Object
-
#run ⇒ Object
run the request and evaluate expectations.
- #total_time ⇒ Object
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, ) self.url = url self. = self.expectations = [] end |
Instance Attribute Details
#assert_no ⇒ Object
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 |
#expectations ⇒ Object
request assertions
28 29 30 |
# File 'lib/greenlight/request.rb', line 28 def expectations @expectations end |
#options ⇒ Object
request description
19 20 21 |
# File 'lib/greenlight/request.rb', line 19 def @options end |
#req_response ⇒ Object
response structure with json parsed body
25 26 27 |
# File 'lib/greenlight/request.rb', line 25 def req_response @req_response end |
#response ⇒ Object
request response
22 23 24 |
# File 'lib/greenlight/request.rb', line 22 def response @response end |
#url ⇒ Object
request description
19 20 21 |
# File 'lib/greenlight/request.rb', line 19 def url @url end |
Instance Method Details
#_debug_info ⇒ Object
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: #{[:body]}" info "request headers: #{[: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 |
#body ⇒ Object
87 88 89 |
# File 'lib/greenlight/request.rb', line 87 def body req_response.body end |
#code ⇒ Object
79 80 81 |
# File 'lib/greenlight/request.rb', line 79 def code response.code end |
#error_msg ⇒ Object
164 165 166 |
# File 'lib/greenlight/request.rb', line 164 def error_msg "REQUEST '#{[: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 |
#headers ⇒ Object
75 76 77 |
# File 'lib/greenlight/request.rb', line 75 def headers response.headers end |
#raw_body ⇒ Object
83 84 85 |
# File 'lib/greenlight/request.rb', line 83 def raw_body response.body end |
#run ⇒ Object
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("#{[:method].upcase} #{url}") Console.instance.indent # run the request [:ssl_verifypeer] = false [:followlocation] = true Injector.decorate() # convert all headers keys to strings to avoid having symbols like :"header" when # declaring headers with colons instead of arrows if .key?(:headers) new_opts = {} [:headers].map do |k, v| new_opts[k.to_s] = v end [:headers] = new_opts end if .key?(:headers) and [:headers].key?('Content-Type') ctype = [:headers]['Content-Type'] if ctype.include?('application/json') # automatically encode json content [:body] = JSON.generate([:body], quirks_mode: true) end end self.response = Typhoeus::Request.new(url, ).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. info e.backtrace.inspect _debug_info error error_msg raise RequestException ensure Console.instance.unindent end req_response end |
#total_time ⇒ Object
91 92 93 |
# File 'lib/greenlight/request.rb', line 91 def total_time response.total_time end |