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.



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

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.



697
698
699
# File 'lib/mockserver/models.rb', line 697

def body
  @body
end

#connection_optionsObject

Returns the value of attribute connection_options.



697
698
699
# File 'lib/mockserver/models.rb', line 697

def connection_options
  @connection_options
end

#cookiesObject

Returns the value of attribute cookies.



697
698
699
# File 'lib/mockserver/models.rb', line 697

def cookies
  @cookies
end

#delayObject

Returns the value of attribute delay.



697
698
699
# File 'lib/mockserver/models.rb', line 697

def delay
  @delay
end

#headersObject

Returns the value of attribute headers.



697
698
699
# File 'lib/mockserver/models.rb', line 697

def headers
  @headers
end

#primaryObject

Returns the value of attribute primary.



697
698
699
# File 'lib/mockserver/models.rb', line 697

def primary
  @primary
end

#reason_phraseObject

Returns the value of attribute reason_phrase.



697
698
699
# File 'lib/mockserver/models.rb', line 697

def reason_phrase
  @reason_phrase
end

#status_codeObject

Returns the value of attribute status_code.



697
698
699
# File 'lib/mockserver/models.rb', line 697

def status_code
  @status_code
end

Class Method Details

.from_hash(data) ⇒ Object



725
726
727
728
729
730
731
732
733
734
735
736
737
738
# File 'lib/mockserver/models.rb', line 725

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



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

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

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



740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
# File 'lib/mockserver/models.rb', line 740

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



712
713
714
715
716
717
718
719
720
721
722
723
# File 'lib/mockserver/models.rb', line 712

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



777
778
779
780
# File 'lib/mockserver/models.rb', line 777

def with_body(body)
  @body = body
  self
end


771
772
773
774
775
# File 'lib/mockserver/models.rb', line 771

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

#with_delay(delay) ⇒ Object



782
783
784
785
# File 'lib/mockserver/models.rb', line 782

def with_delay(delay)
  @delay = delay
  self
end

#with_header(name, *values) ⇒ Object



765
766
767
768
769
# File 'lib/mockserver/models.rb', line 765

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

#with_reason_phrase(reason_phrase) ⇒ Object



787
788
789
790
# File 'lib/mockserver/models.rb', line 787

def with_reason_phrase(reason_phrase)
  @reason_phrase = reason_phrase
  self
end

#with_status_code(status_code) ⇒ Object



760
761
762
763
# File 'lib/mockserver/models.rb', line 760

def with_status_code(status_code)
  @status_code = status_code
  self
end