Class: Faraday::Adapter::Test::Stub

Inherits:
Struct
  • Object
show all
Defined in:
lib/faraday/adapter/test.rb

Overview

Stub request

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#blockObject

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



187
188
189
# File 'lib/faraday/adapter/test.rb', line 187

def block
  @block
end

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



187
188
189
# File 'lib/faraday/adapter/test.rb', line 187

def body
  @body
end

#headersObject

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



187
188
189
# File 'lib/faraday/adapter/test.rb', line 187

def headers
  @headers
end

#hostObject

Returns the value of attribute host

Returns:

  • (Object)

    the current value of host



187
188
189
# File 'lib/faraday/adapter/test.rb', line 187

def host
  @host
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



187
188
189
# File 'lib/faraday/adapter/test.rb', line 187

def path
  @path
end

#queryObject

Returns the value of attribute query

Returns:

  • (Object)

    the current value of query



187
188
189
# File 'lib/faraday/adapter/test.rb', line 187

def query
  @query
end

#strict_modeObject

Returns the value of attribute strict_mode

Returns:

  • (Object)

    the current value of strict_mode



187
188
189
# File 'lib/faraday/adapter/test.rb', line 187

def strict_mode
  @strict_mode
end

Instance Method Details

#body_match?(request_body) ⇒ Boolean

Returns:

  • (Boolean)


242
243
244
245
246
247
248
249
250
251
# File 'lib/faraday/adapter/test.rb', line 242

def body_match?(request_body)
  return true if body.to_s.empty?

  case body
  when Proc
    body.call(request_body)
  else
    request_body == body
  end
end

#headers_match?(request_headers) ⇒ Boolean

Returns:

  • (Boolean)


227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/faraday/adapter/test.rb', line 227

def headers_match?(request_headers)
  if strict_mode
    headers_with_user_agent = headers.dup.tap do |hs|
      # NOTE: Set User-Agent in case it's not set when creating Stubs.
      #       Users would not want to set Faraday's User-Agent explicitly.
      hs[:user_agent] ||= Connection::USER_AGENT
    end
    return Set.new(headers_with_user_agent) == Set.new(request_headers)
  end

  headers.keys.all? do |key|
    request_headers[key] == headers[key]
  end
end

#matches?(env) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/faraday/adapter/test.rb', line 189

def matches?(env)
  request_host = env[:url].host
  request_path = Faraday::Utils.normalize_path(env[:url].path)
  request_headers = env.request_headers
  request_body = env[:body]

  # meta is a hash used as carrier
  # that will be yielded to consumer block
  meta = {}
  [(host.nil? || host == request_host) &&
    path_match?(request_path, meta) &&
    params_match?(env) &&
    body_match?(request_body) &&
    headers_match?(request_headers), meta]
end

#params_match?(env) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/faraday/adapter/test.rb', line 214

def params_match?(env)
  request_params = env[:params]
  params = env.params_encoder.decode(query) || {}

  if strict_mode
    return Set.new(params) == Set.new(request_params)
  end

  params.keys.all? do |key|
    request_params[key] == params[key]
  end
end

#path_match?(request_path, meta) ⇒ Boolean

Returns:

  • (Boolean)


205
206
207
208
209
210
211
# File 'lib/faraday/adapter/test.rb', line 205

def path_match?(request_path, meta)
  if path.is_a?(Regexp)
    !!(meta[:match_data] = path.match(request_path))
  else
    path == request_path
  end
end

#to_sObject



253
254
255
# File 'lib/faraday/adapter/test.rb', line 253

def to_s
  "#{path} #{body}"
end