Class: ApiTester::Endpoint
- Inherits:
-
Object
- Object
- ApiTester::Endpoint
- Defined in:
- lib/api-tester/definition/endpoint.rb
Overview
Class for defining and interacting with endpoints in a contract
Instance Attribute Summary collapse
-
#bad_request_response ⇒ Object
Returns the value of attribute bad_request_response.
-
#longest_time ⇒ Object
Returns the value of attribute longest_time.
-
#methods ⇒ Object
Returns the value of attribute methods.
-
#name ⇒ Object
Returns the value of attribute name.
-
#not_allowed_response ⇒ Object
Returns the value of attribute not_allowed_response.
-
#not_found_response ⇒ Object
Returns the value of attribute not_found_response.
-
#path_params ⇒ Object
Returns the value of attribute path_params.
-
#relative_url ⇒ Object
Returns the value of attribute relative_url.
-
#required_headers ⇒ Object
Returns the value of attribute required_headers.
-
#test_helper ⇒ Object
Returns the value of attribute test_helper.
Instance Method Summary collapse
- #add_method(verb:, response:, request: Request.new) ⇒ Object
- #add_path_param(param) ⇒ Object
- #call(base_url:, method:, query: '', payload: {}, headers: {}) ⇒ Object
- #default_call(base_url) ⇒ Object
- #display_url ⇒ Object
-
#initialize(name:, relative_url:) ⇒ Endpoint
constructor
A new instance of Endpoint.
- #url ⇒ Object
- #verbs ⇒ Object
Constructor Details
#initialize(name:, relative_url:) ⇒ Endpoint
Returns a new instance of Endpoint.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/api-tester/definition/endpoint.rb', line 16 def initialize(name:, relative_url:) self.relative_url = relative_url self.name = name self.methods = [] self.path_params = [] self.longest_time = { time: 0 } self.test_helper = ApiTester::TestHelper.new '' self.bad_request_response = ApiTester::Response.new status_code: 400 self.not_allowed_response = ApiTester::Response.new status_code: 415 self.not_found_response = ApiTester::Response.new status_code: 404 self.required_headers = {} end |
Instance Attribute Details
#bad_request_response ⇒ Object
Returns the value of attribute bad_request_response.
14 15 16 |
# File 'lib/api-tester/definition/endpoint.rb', line 14 def bad_request_response @bad_request_response end |
#longest_time ⇒ Object
Returns the value of attribute longest_time.
14 15 16 |
# File 'lib/api-tester/definition/endpoint.rb', line 14 def longest_time @longest_time end |
#methods ⇒ Object
Returns the value of attribute methods.
14 15 16 |
# File 'lib/api-tester/definition/endpoint.rb', line 14 def methods @methods end |
#name ⇒ Object
Returns the value of attribute name.
14 15 16 |
# File 'lib/api-tester/definition/endpoint.rb', line 14 def name @name end |
#not_allowed_response ⇒ Object
Returns the value of attribute not_allowed_response.
14 15 16 |
# File 'lib/api-tester/definition/endpoint.rb', line 14 def not_allowed_response @not_allowed_response end |
#not_found_response ⇒ Object
Returns the value of attribute not_found_response.
14 15 16 |
# File 'lib/api-tester/definition/endpoint.rb', line 14 def not_found_response @not_found_response end |
#path_params ⇒ Object
Returns the value of attribute path_params.
14 15 16 |
# File 'lib/api-tester/definition/endpoint.rb', line 14 def path_params @path_params end |
#relative_url ⇒ Object
Returns the value of attribute relative_url.
14 15 16 |
# File 'lib/api-tester/definition/endpoint.rb', line 14 def relative_url @relative_url end |
#required_headers ⇒ Object
Returns the value of attribute required_headers.
14 15 16 |
# File 'lib/api-tester/definition/endpoint.rb', line 14 def required_headers @required_headers end |
#test_helper ⇒ Object
Returns the value of attribute test_helper.
14 15 16 |
# File 'lib/api-tester/definition/endpoint.rb', line 14 def test_helper @test_helper end |
Instance Method Details
#add_method(verb:, response:, request: Request.new) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/api-tester/definition/endpoint.rb', line 86 def add_method(verb:, response:, request: Request.new) methods << ApiTester::Method.new(verb: verb, response: response, request: request) self end |
#add_path_param(param) ⇒ Object
93 94 95 96 |
# File 'lib/api-tester/definition/endpoint.rb', line 93 def add_path_param(param) path_params << param self end |
#call(base_url:, method:, query: '', payload: {}, headers: {}) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/api-tester/definition/endpoint.rb', line 63 def call(base_url:, method:, query: '', payload: {}, headers: {}) test_helper.before call_url = query ? "#{base_url}#{url}?#{query}" : "#{base_url}#{url}" begin response = nil time = Benchmark.measure { response = RestClient::Request.execute(method: method.verb, url: call_url, payload: payload.to_json, headers: headers) } if time.real > longest_time[:time] longest_time[:time] = time.real longest_time[:payload] = payload.to_json longest_time[:verb] = method.verb end rescue RestClient::ExceptionWithResponse => e response = e.response end test_helper.after response end |
#default_call(base_url) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/api-tester/definition/endpoint.rb', line 42 def default_call(base_url) test_helper.before method_defaults = methods[0].default_request method_defaults[:url] = "#{base_url}#{url}" begin response = nil time = Benchmark.measure { response = RestClient::Request.execute(method_defaults) } if time.real > longest_time[:time] && longest_time[:time] > 0 longest_time[:time] = time.real longest_time[:payload] = payload.to_json longest_time[:verb] = method.verb end rescue RestClient::ExceptionWithResponse => e response = e.response end test_helper.after response end |
#display_url ⇒ Object
29 30 31 |
# File 'lib/api-tester/definition/endpoint.rb', line 29 def display_url relative_url end |
#url ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/api-tester/definition/endpoint.rb', line 33 def url temp_url = relative_url.clone path_params.each do |param| value = test_helper.retrieve_param(param).to_s temp_url = relative_url.sub "{#{param}}", value end temp_url end |
#verbs ⇒ Object
98 99 100 |
# File 'lib/api-tester/definition/endpoint.rb', line 98 def verbs methods.map(&:verb) end |