Class: VCR::Response

Inherits:
Struct
  • Object
show all
Includes:
Normalizers::Body, Normalizers::Header
Defined in:
lib/vcr/structs.rb

Overview

The response of an HTTPInteraction.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Normalizers::Body

included

Constructor Details

#initialize(*args) ⇒ Response

Returns a new instance of Response.



322
323
324
325
# File 'lib/vcr/structs.rb', line 322

def initialize(*args)
  super(*args)
  self. ||= {}
end

Instance Attribute Details

#adapter_metadataHash

Additional metadata used by a specific VCR adapter.

Returns:

  • the current value of adapter_metadata



318
319
320
# File 'lib/vcr/structs.rb', line 318

def 
  
end

#bodyString

the response body

Returns:

  • the current value of body



318
319
320
# File 'lib/vcr/structs.rb', line 318

def body
  @body
end

#headersHash{String => Array<String>}

the response headers

Returns:

  • the current value of headers



318
319
320
# File 'lib/vcr/structs.rb', line 318

def headers
  @headers
end

#http_versionnil, String

the HTTP version

Returns:

  • the current value of http_version



318
319
320
# File 'lib/vcr/structs.rb', line 318

def http_version
  @http_version
end

#statusResponseStatus

the status of the response

Returns:

  • the current value of status



318
319
320
# File 'lib/vcr/structs.rb', line 318

def status
  @status
end

Class Method Details

.decompress(body, type) ⇒ Object

Decode string compressed with gzip or deflate

Raises:

  • if the content encoding is not a known encoding.



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/vcr/structs.rb', line 434

def self.decompress(body, type)
  unless HAVE_ZLIB
    warn "VCR: cannot decompress response; Zlib not available"
    return
  end

  case type
  when 'gzip'
    yield Zlib::GzipReader.new(StringIO.new(body), encoding: 'ASCII-8BIT').read
  when 'deflate'
    yield Zlib::Inflate.inflate(body)
  when 'identity', NilClass
    return
  else
    raise Errors::UnknownContentEncodingError, "unknown content encoding: #{type}"
  end
end

.from_hash(hash) ⇒ Response

Constructs a new instance from a hash.

Parameters:

  • the hash to use to construct the instance.

Returns:

  • the response



347
348
349
350
351
352
353
# File 'lib/vcr/structs.rb', line 347

def self.from_hash(hash)
  new ResponseStatus.from_hash(hash.fetch('status', {})),
      hash['headers'],
      body_from(hash['body']),
      hash['http_version'],
      hash['adapter_metadata']
end

Instance Method Details

#compressed?Boolean

Checks if the type of encoding is one of “gzip” or “deflate”.

Returns:



369
370
371
# File 'lib/vcr/structs.rb', line 369

def compressed?
  %w[ gzip deflate ].include? content_encoding
end

#content_encodingString

The type of encoding.

Returns:

  • encoding type



364
365
366
# File 'lib/vcr/structs.rb', line 364

def content_encoding
  enc = get_header('Content-Encoding') and enc.first
end

#decompressObject

Decodes the compressed body and deletes evidence that it was ever compressed.

Returns:

  • self

Raises:

  • if the content encoding is not a known encoding.



383
384
385
386
387
388
389
390
391
# File 'lib/vcr/structs.rb', line 383

def decompress
  self.class.decompress(body, content_encoding) { |new_body|
    self.body = new_body
    update_content_length_header
    ['vcr_decompressed'] = content_encoding
    delete_header('Content-Encoding')
  }
  return self
end

#recompressObject

Recompresses the decompressed body according to adapter metadata.

Raises:

  • if the content encoding stored in the adapter metadata is unknown



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/vcr/structs.rb', line 397

def recompress
  type = ['vcr_decompressed']
  new_body = begin
    case type
    when 'gzip'
      body_str = ''
      writer = Zlib::GzipWriter.new(StringIO.new(body_str), encoding: 'ASCII-8BIT')
      writer.write(body)
      writer.close
      body_str
    when 'deflate'
      Zlib::Deflate.inflate(body)
    when 'identity', NilClass
      nil
    else
      raise Errors::UnknownContentEncodingError, "unknown content encoding: #{type}"
    end
  end
  if new_body
    self.body = new_body
    update_content_length_header
    headers['Content-Encoding'] = type
  end
end

#to_hashHash

Builds a serializable hash from the response data.

Returns:

  • hash that represents this response and can be easily serialized.

See Also:



332
333
334
335
336
337
338
339
340
341
# File 'lib/vcr/structs.rb', line 332

def to_hash
  {
    'status'       => status.to_hash,
    'headers'      => headers,
    'body'         => serializable_body
  }.tap do |hash|
    hash['http_version']     = http_version if http_version
    hash['adapter_metadata'] =  unless .empty?
  end
end

#update_content_length_headerObject

Updates the Content-Length response header so that it is accurate for the response body.



357
358
359
# File 'lib/vcr/structs.rb', line 357

def update_content_length_header
  edit_header('Content-Length') { body ? body.bytesize.to_s : '0' }
end

#vcr_decompressed?Boolean

Checks if VCR decompressed the response body

Returns:



374
375
376
# File 'lib/vcr/structs.rb', line 374

def vcr_decompressed?
  ['vcr_decompressed']
end