Class: Maze::Servlets::ReflectiveServlet

Inherits:
BaseServlet
  • Object
show all
Defined in:
lib/maze/servlets/reflective_servlet.rb

Overview

Receives HTTP requests and responds according to the parameters given, which are:

  • delay_ms - milliseconds to wait before responding

  • status - HTTP response code

For GET requests these are expected to passed as GET parameters, for POST requests they are expected to be given as JSON fields.

Constant Summary collapse

REFLECTION_REQUEST_TYPE =
'reflection'

Instance Method Summary collapse

Instance Method Details

#do_GET(request, response) ⇒ Object

Accepts a GET request to provide a reflective response to.

Parameters:

  • request (HTTPRequest)

    The incoming GET request

  • response (HTTPResponse)

    The response to return



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/maze/servlets/reflective_servlet.rb', line 20

def do_GET(request, response)
  delay_ms = request.query['delay_ms']
  status = request.query['status']

  logged_request = {
    body: {},
    query: Rack::Utils.parse_nested_query(request.query_string),
    request: request,
    response: response,
    method: 'GET'
  }
  Server.list_for(REFLECTION_REQUEST_TYPE).add(logged_request)

  reflect response, delay_ms, status
end

#do_OPTIONS(request, response) ⇒ Object

Logs and returns a set of valid headers for this servlet.

Parameters:

  • request (HTTPRequest)

    The incoming GET request

  • response (HTTPResponse)

    The response to return



88
89
90
91
92
# File 'lib/maze/servlets/reflective_servlet.rb', line 88

def do_OPTIONS(request, response)
  super

  response.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
end

#do_POST(request, response) ⇒ Object

Accepts a POST request to provide a reflective response to.

Parameters:

  • request (HTTPRequest)

    The incoming GET request

  • response (HTTPResponse)

    The response to return



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/maze/servlets/reflective_servlet.rb', line 40

def do_POST(request, response)

  content_type = request['Content-Type']

  # For JSON, pull the instructions from the body.  Otherwise, take them from the query string.
  if content_type == 'application/json'
    body = JSON.parse(request.body)
    delay_ms = body['delay_ms']
    status = body['status']
  else
    query = Rack::Utils.parse_nested_query(request.query_string)
    body = {}
    delay_ms = query['delay_ms']
    status = query['status']
  end

  logged_request = {
    body: body,
    request: request,
    response: response,
    method: 'POST'
  }
  logged_request[:query] = query if query
  Server.list_for(REFLECTION_REQUEST_TYPE).add(logged_request)

  reflect response, delay_ms, status
rescue JSON::ParserError => e
  Bugsnag.notify e
  msg = "Unable to parse request as JSON: #{e.message}"
  $logger.error msg
  response.status = 418
rescue StandardError => e
  Bugsnag.notify e
  $logger.error "Invalid request to reflective servlet: #{e.message}"
  response.status = 500
end

#reflect(response, delay_ms, status) ⇒ Object



77
78
79
80
81
82
# File 'lib/maze/servlets/reflective_servlet.rb', line 77

def reflect(response, delay_ms, status)
  sleep delay_ms.to_i / 1000 unless delay_ms.nil?
  response.status = status || 200
  response.header['Access-Control-Allow-Origin'] = '*'
  response.body = "Returned status #{status} after waiting #{delay_ms} ms"
end