Class: HTTPStubServlet

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/httpstubservlet.rb

Constant Summary collapse

@@instances =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ HTTPStubServlet

Returns a new instance of HTTPStubServlet.



8
9
10
11
# File 'lib/httpstubservlet.rb', line 8

def initialize(server)
  super
  clear_responses
end

Class Method Details

.get_instance(server, port) ⇒ Object



4
5
6
# File 'lib/httpstubservlet.rb', line 4

def self.get_instance(server, port)
  @@instances[port] ||= self.new(server)
end

Instance Method Details

#clear_responsesObject



29
30
31
# File 'lib/httpstubservlet.rb', line 29

def clear_responses
  @responses = {}
end

#convert_header_name(key) ⇒ Object



53
54
55
56
57
# File 'lib/httpstubservlet.rb', line 53

def convert_header_name(key)
  {
    :content_type => "Content-Type"
  }[key] || key
end

#fill_response(response, canned_response) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/httpstubservlet.rb', line 37

def fill_response(response, canned_response)
  headers = canned_response.first || {}
  headers.each do |k, v|
    next if k == :status
    response[convert_header_name(k)] = v
  end

  response.status = headers[:status] || 200
  response.body = canned_response.last
end

#get_response(path, method) ⇒ Object



33
34
35
# File 'lib/httpstubservlet.rb', line 33

def get_response(path, method)
  (@responses[path] || {})[method]
end

#respond_with_404(response) ⇒ Object



48
49
50
51
# File 'lib/httpstubservlet.rb', line 48

def respond_with_404(response)
  response.status = 404
  response.body = "not found"
end