Class: MockServer::HttpResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/mockserver/models.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status_code: nil, reason_phrase: nil, headers: nil, cookies: nil, body: nil, delay: nil, connection_options: nil, primary: nil) ⇒ HttpResponse

Returns a new instance of HttpResponse.



660
661
662
663
664
665
666
667
668
669
670
# File 'lib/mockserver/models.rb', line 660

def initialize(status_code: nil, reason_phrase: nil, headers: nil, cookies: nil,
               body: nil, delay: nil, connection_options: nil, primary: nil)
  @status_code = status_code
  @reason_phrase = reason_phrase
  @headers = headers
  @cookies = cookies
  @body = body
  @delay = delay
  @connection_options = connection_options
  @primary = primary
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



657
658
659
# File 'lib/mockserver/models.rb', line 657

def body
  @body
end

#connection_optionsObject

Returns the value of attribute connection_options.



657
658
659
# File 'lib/mockserver/models.rb', line 657

def connection_options
  @connection_options
end

#cookiesObject

Returns the value of attribute cookies.



657
658
659
# File 'lib/mockserver/models.rb', line 657

def cookies
  @cookies
end

#delayObject

Returns the value of attribute delay.



657
658
659
# File 'lib/mockserver/models.rb', line 657

def delay
  @delay
end

#headersObject

Returns the value of attribute headers.



657
658
659
# File 'lib/mockserver/models.rb', line 657

def headers
  @headers
end

#primaryObject

Returns the value of attribute primary.



657
658
659
# File 'lib/mockserver/models.rb', line 657

def primary
  @primary
end

#reason_phraseObject

Returns the value of attribute reason_phrase.



657
658
659
# File 'lib/mockserver/models.rb', line 657

def reason_phrase
  @reason_phrase
end

#status_codeObject

Returns the value of attribute status_code.



657
658
659
# File 'lib/mockserver/models.rb', line 657

def status_code
  @status_code
end

Class Method Details

.from_hash(data) ⇒ Object



685
686
687
688
689
690
691
692
693
694
695
696
697
698
# File 'lib/mockserver/models.rb', line 685

def self.from_hash(data)
  return nil if data.nil?

  new(
    status_code:       data['statusCode'],
    reason_phrase:     data['reasonPhrase'],
    headers:           MockServer.deserialize_key_multi_values(data['headers']),
    cookies:           MockServer.deserialize_key_multi_values(data['cookies']),
    body:              MockServer.deserialize_body(data['body']),
    delay:             Delay.from_hash(data['delay']),
    connection_options: ConnectionOptions.from_hash(data['connectionOptions']),
    primary:           data['primary']
  )
end

.not_found_responseObject



716
717
718
# File 'lib/mockserver/models.rb', line 716

def self.not_found_response
  new(status_code: 404, reason_phrase: 'Not Found')
end

.response(body: nil, status_code: nil) ⇒ Object



700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'lib/mockserver/models.rb', line 700

def self.response(body: nil, status_code: nil)
  resp = new
  if body
    resp.body = body
    if status_code.nil?
      resp.status_code = 200
      resp.reason_phrase = 'OK'
    else
      resp.status_code = status_code
    end
  elsif status_code
    resp.status_code = status_code
  end
  resp
end

Instance Method Details

#to_hObject



672
673
674
675
676
677
678
679
680
681
682
683
# File 'lib/mockserver/models.rb', line 672

def to_h
  MockServer.strip_none({
    'statusCode'       => @status_code,
    'reasonPhrase'     => @reason_phrase,
    'headers'          => MockServer.serialize_key_multi_values(@headers),
    'cookies'          => MockServer.serialize_key_multi_values(@cookies),
    'body'             => MockServer.serialize_body(@body),
    'delay'            => @delay&.to_h,
    'connectionOptions' => @connection_options&.to_h,
    'primary'          => @primary
  })
end

#with_body(body) ⇒ Object



737
738
739
740
# File 'lib/mockserver/models.rb', line 737

def with_body(body)
  @body = body
  self
end


731
732
733
734
735
# File 'lib/mockserver/models.rb', line 731

def with_cookie(name, value)
  @cookies ||= []
  @cookies << KeyToMultiValue.new(name: name, values: [value])
  self
end

#with_delay(delay) ⇒ Object



742
743
744
745
# File 'lib/mockserver/models.rb', line 742

def with_delay(delay)
  @delay = delay
  self
end

#with_header(name, *values) ⇒ Object



725
726
727
728
729
# File 'lib/mockserver/models.rb', line 725

def with_header(name, *values)
  @headers ||= []
  @headers << KeyToMultiValue.new(name: name, values: values.flatten)
  self
end

#with_reason_phrase(reason_phrase) ⇒ Object



747
748
749
750
# File 'lib/mockserver/models.rb', line 747

def with_reason_phrase(reason_phrase)
  @reason_phrase = reason_phrase
  self
end

#with_status_code(status_code) ⇒ Object



720
721
722
723
# File 'lib/mockserver/models.rb', line 720

def with_status_code(status_code)
  @status_code = status_code
  self
end