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



195
196
197
# File 'lib/faraday/adapter/test.rb', line 195

def block
  @block
end

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



195
196
197
# File 'lib/faraday/adapter/test.rb', line 195

def body
  @body
end

#headersObject

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



195
196
197
# File 'lib/faraday/adapter/test.rb', line 195

def headers
  @headers
end

#hostObject

Returns the value of attribute host

Returns:

  • (Object)

    the current value of host



195
196
197
# File 'lib/faraday/adapter/test.rb', line 195

def host
  @host
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



195
196
197
# File 'lib/faraday/adapter/test.rb', line 195

def path
  @path
end

#queryObject

Returns the value of attribute query

Returns:

  • (Object)

    the current value of query



195
196
197
# File 'lib/faraday/adapter/test.rb', line 195

def query
  @query
end

#strict_modeObject

Returns the value of attribute strict_mode

Returns:

  • (Object)

    the current value of strict_mode



195
196
197
# File 'lib/faraday/adapter/test.rb', line 195

def strict_mode
  @strict_mode
end

Instance Method Details

#body_match?(request_body) ⇒ Boolean

Returns:

  • (Boolean)


250
251
252
253
254
255
256
257
258
259
# File 'lib/faraday/adapter/test.rb', line 250

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)


235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/faraday/adapter/test.rb', line 235

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)


197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/faraday/adapter/test.rb', line 197

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)


222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/faraday/adapter/test.rb', line 222

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)


213
214
215
216
217
218
219
# File 'lib/faraday/adapter/test.rb', line 213

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



261
262
263
# File 'lib/faraday/adapter/test.rb', line 261

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