Class: RightDevelop::Testing::Client::Rest::Request::Base
- Inherits:
-
RestClient::Request
- Object
- RestClient::Request
- RightDevelop::Testing::Client::Rest::Request::Base
- Defined in:
- lib/right_develop/testing/clients/rest/requests/base.rb
Overview
Base class for record/playback request implementations.
Constant Summary collapse
- METADATA_CLASS =
metadata.
::RightDevelop::Testing::Recording::Metadata
- MUTEX =
semaphore
::Mutex.new
Instance Attribute Summary collapse
-
#fixtures_dir ⇒ Object
readonly
Returns the value of attribute fixtures_dir.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#request_metadata ⇒ Object
readonly
Returns the value of attribute request_metadata.
-
#request_timestamp ⇒ Object
readonly
Returns the value of attribute request_timestamp.
-
#response_metadata ⇒ Object
readonly
Returns the value of attribute response_metadata.
-
#response_timestamp ⇒ Object
readonly
Returns the value of attribute response_timestamp.
-
#route_data ⇒ Object
readonly
Returns the value of attribute route_data.
-
#route_path ⇒ Object
readonly
Returns the value of attribute route_path.
-
#state_file_path ⇒ Object
readonly
Returns the value of attribute state_file_path.
Instance Method Summary collapse
-
#forget_outstanding_request ⇒ Object
Removes the current request from the FIFO queue of outstanding requests in case of error, redirect, etc.
-
#handle_timeout ⇒ Net::HTTPResponse
Handles a timeout raised by a Net::HTTP call.
-
#initialize(args) ⇒ Base
constructor
A new instance of Base.
-
#log_request ⇒ Object
Overrides log_request to capture start-time for network request.
-
#log_response(response) ⇒ Object
Overrides log_response to capture end-time for network request.
Constructor Details
#initialize(args) ⇒ Base
Returns a new instance of Base.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/right_develop/testing/clients/rest/requests/base.rb', line 49 def initialize(args) args = args.dup unless @fixtures_dir = args.delete(:fixtures_dir) raise ::ArgumentError, 'fixtures_dir is required' end unless @logger = args.delete(:logger) raise ::ArgumentError, 'logger is required' end unless @route_path = args.delete(:route_path) raise ::ArgumentError, 'route_path is required' end unless @route_data = args.delete(:route_data) raise ::ArgumentError, 'route_data is required' end unless @route_data[:subdir] raise ::ArgumentError, "route_data is invalid: #{route_data.inspect}" end unless @state_file_path = args.delete(:state_file_path) raise ::ArgumentError, 'state_file_path is required' end # resolve request metadata before initializing base class in order to set # any timeout values. request_verb = args[:method] or raise ::ArgumentError, "must pass :method" request_verb = request_verb.to_s.upcase request_headers = (args[:headers] || {}).dup request_url = args[:url] or raise ::ArgumentError, "must pass :url" request_url = process_url_params(request_url, request_headers) if request_body = args[:payload] # currently only supporting string payload or nil. unless request_body.kind_of?(::String) raise ::ArgumentError, 'args[:payload] must be a string' end end rm = nil with_state_lock do |state| rm = METADATA_CLASS.new( mode: recording_mode, kind: :request, logger: @logger, route_data: @route_data, uri: METADATA_CLASS.normalize_uri(request_url), verb: request_verb, headers: request_headers, body: request_body, variables: state[:variables]) end @request_metadata = rm unless rm.timeouts.empty? args = args.dup if rm.timeouts[:open_timeout] args[:open_timeout] = Integer(rm.timeouts[:open_timeout]) end if rm.timeouts[:read_timeout] args[:timeout] = Integer(rm.timeouts[:read_timeout]) end end @response_metadata = nil super(args) if @block_response raise ::NotImplementedError, 'block_response not supported for record/playback' end if @raw_response raise ::ArgumentError, 'raw_response not supported for record/playback' end end |
Instance Attribute Details
#fixtures_dir ⇒ Object (readonly)
Returns the value of attribute fixtures_dir.
45 46 47 |
# File 'lib/right_develop/testing/clients/rest/requests/base.rb', line 45 def fixtures_dir @fixtures_dir end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
45 46 47 |
# File 'lib/right_develop/testing/clients/rest/requests/base.rb', line 45 def logger @logger end |
#request_metadata ⇒ Object (readonly)
Returns the value of attribute request_metadata.
47 48 49 |
# File 'lib/right_develop/testing/clients/rest/requests/base.rb', line 47 def @request_metadata end |
#request_timestamp ⇒ Object (readonly)
Returns the value of attribute request_timestamp.
46 47 48 |
# File 'lib/right_develop/testing/clients/rest/requests/base.rb', line 46 def @request_timestamp end |
#response_metadata ⇒ Object (readonly)
Returns the value of attribute response_metadata.
47 48 49 |
# File 'lib/right_develop/testing/clients/rest/requests/base.rb', line 47 def @response_metadata end |
#response_timestamp ⇒ Object (readonly)
Returns the value of attribute response_timestamp.
46 47 48 |
# File 'lib/right_develop/testing/clients/rest/requests/base.rb', line 46 def @response_timestamp end |
#route_data ⇒ Object (readonly)
Returns the value of attribute route_data.
45 46 47 |
# File 'lib/right_develop/testing/clients/rest/requests/base.rb', line 45 def route_data @route_data end |
#route_path ⇒ Object (readonly)
Returns the value of attribute route_path.
45 46 47 |
# File 'lib/right_develop/testing/clients/rest/requests/base.rb', line 45 def route_path @route_path end |
#state_file_path ⇒ Object (readonly)
Returns the value of attribute state_file_path.
46 47 48 |
# File 'lib/right_develop/testing/clients/rest/requests/base.rb', line 46 def state_file_path @state_file_path end |
Instance Method Details
#forget_outstanding_request ⇒ Object
Removes the current request from the FIFO queue of outstanding requests in case of error, redirect, etc.
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/right_develop/testing/clients/rest/requests/base.rb', line 149 def forget_outstanding_request ruid = request_uid ork = outstanding_request_key with_state_lock do |state| outstanding = state[:outstanding] if outstanding_requests = outstanding[ork] if outstanding_requests.delete(ruid) logger.debug("Forgot outstanding request uid=#{ruid.inspect} at #{ork.inspect}") end outstanding.delete(ork) if outstanding_requests.empty? end end end |
#handle_timeout ⇒ Net::HTTPResponse
Handles a timeout raised by a Net::HTTP call.
142 143 144 145 |
# File 'lib/right_develop/testing/clients/rest/requests/base.rb', line 142 def handle_timeout pop_outstanding_request nil end |
#log_request ⇒ Object
Overrides log_request to capture start-time for network request.
123 124 125 126 127 |
# File 'lib/right_develop/testing/clients/rest/requests/base.rb', line 123 def log_request result = super push_outstanding_request result end |
#log_response(response) ⇒ Object
Overrides log_response to capture end-time for network request.
134 135 136 137 |
# File 'lib/right_develop/testing/clients/rest/requests/base.rb', line 134 def log_response(response) pop_outstanding_request super end |