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.



680
681
682
683
684
685
686
687
688
689
690
# File 'lib/mockserver/models.rb', line 680

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.



677
678
679
# File 'lib/mockserver/models.rb', line 677

def body
  @body
end

#connection_optionsObject

Returns the value of attribute connection_options.



677
678
679
# File 'lib/mockserver/models.rb', line 677

def connection_options
  @connection_options
end

#cookiesObject

Returns the value of attribute cookies.



677
678
679
# File 'lib/mockserver/models.rb', line 677

def cookies
  @cookies
end

#delayObject

Returns the value of attribute delay.



677
678
679
# File 'lib/mockserver/models.rb', line 677

def delay
  @delay
end

#headersObject

Returns the value of attribute headers.



677
678
679
# File 'lib/mockserver/models.rb', line 677

def headers
  @headers
end

#primaryObject

Returns the value of attribute primary.



677
678
679
# File 'lib/mockserver/models.rb', line 677

def primary
  @primary
end

#reason_phraseObject

Returns the value of attribute reason_phrase.



677
678
679
# File 'lib/mockserver/models.rb', line 677

def reason_phrase
  @reason_phrase
end

#status_codeObject

Returns the value of attribute status_code.



677
678
679
# File 'lib/mockserver/models.rb', line 677

def status_code
  @status_code
end

Class Method Details

.from_hash(data) ⇒ Object



705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'lib/mockserver/models.rb', line 705

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



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

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

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



720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'lib/mockserver/models.rb', line 720

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



692
693
694
695
696
697
698
699
700
701
702
703
# File 'lib/mockserver/models.rb', line 692

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



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

def with_body(body)
  @body = body
  self
end


751
752
753
754
755
# File 'lib/mockserver/models.rb', line 751

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

#with_delay(delay) ⇒ Object



762
763
764
765
# File 'lib/mockserver/models.rb', line 762

def with_delay(delay)
  @delay = delay
  self
end

#with_header(name, *values) ⇒ Object



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

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

#with_reason_phrase(reason_phrase) ⇒ Object



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

def with_reason_phrase(reason_phrase)
  @reason_phrase = reason_phrase
  self
end

#with_status_code(status_code) ⇒ Object



740
741
742
743
# File 'lib/mockserver/models.rb', line 740

def with_status_code(status_code)
  @status_code = status_code
  self
end