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.



758
759
760
761
762
763
764
765
766
767
768
# File 'lib/mockserver/models.rb', line 758

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.



755
756
757
# File 'lib/mockserver/models.rb', line 755

def body
  @body
end

#connection_optionsObject

Returns the value of attribute connection_options.



755
756
757
# File 'lib/mockserver/models.rb', line 755

def connection_options
  @connection_options
end

#cookiesObject

Returns the value of attribute cookies.



755
756
757
# File 'lib/mockserver/models.rb', line 755

def cookies
  @cookies
end

#delayObject

Returns the value of attribute delay.



755
756
757
# File 'lib/mockserver/models.rb', line 755

def delay
  @delay
end

#headersObject

Returns the value of attribute headers.



755
756
757
# File 'lib/mockserver/models.rb', line 755

def headers
  @headers
end

#primaryObject

Returns the value of attribute primary.



755
756
757
# File 'lib/mockserver/models.rb', line 755

def primary
  @primary
end

#reason_phraseObject

Returns the value of attribute reason_phrase.



755
756
757
# File 'lib/mockserver/models.rb', line 755

def reason_phrase
  @reason_phrase
end

#status_codeObject

Returns the value of attribute status_code.



755
756
757
# File 'lib/mockserver/models.rb', line 755

def status_code
  @status_code
end

Class Method Details

.from_hash(data) ⇒ Object



783
784
785
786
787
788
789
790
791
792
793
794
795
796
# File 'lib/mockserver/models.rb', line 783

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_cookies(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



814
815
816
# File 'lib/mockserver/models.rb', line 814

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

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



798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
# File 'lib/mockserver/models.rb', line 798

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



770
771
772
773
774
775
776
777
778
779
780
781
# File 'lib/mockserver/models.rb', line 770

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

#with_body(body) ⇒ Object



835
836
837
838
# File 'lib/mockserver/models.rb', line 835

def with_body(body)
  @body = body
  self
end


829
830
831
832
833
# File 'lib/mockserver/models.rb', line 829

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

#with_delay(delay) ⇒ Object



840
841
842
843
# File 'lib/mockserver/models.rb', line 840

def with_delay(delay)
  @delay = delay
  self
end

#with_header(name, *values) ⇒ Object



823
824
825
826
827
# File 'lib/mockserver/models.rb', line 823

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

#with_reason_phrase(reason_phrase) ⇒ Object



845
846
847
848
# File 'lib/mockserver/models.rb', line 845

def with_reason_phrase(reason_phrase)
  @reason_phrase = reason_phrase
  self
end

#with_status_code(status_code) ⇒ Object



818
819
820
821
# File 'lib/mockserver/models.rb', line 818

def with_status_code(status_code)
  @status_code = status_code
  self
end