Class: Maze::Servlets::LogServlet

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

Overview

Receives log requests sent from the test fixture

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ LogServlet

Constructor

Parameters:

  • server (HTTPServer)

    WEBrick HTTPServer



13
14
15
16
# File 'lib/maze/servlets/log_servlet.rb', line 13

def initialize(server)
  super server
  @requests = Server.logs
end

Instance Method Details

#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



63
64
65
66
67
68
# File 'lib/maze/servlets/log_servlet.rb', line 63

def do_OPTIONS(request, response)
  super

  response.header['Access-Control-Allow-Methods'] = 'POST, OPTIONS'
  response.status = Server.status_code('OPTIONS')
end

#do_POST(request, response) ⇒ Object

Logs and parses an incoming POST request. Parses ‘multipart/form-data` and `application/json` content-types. Parsed requests are added to the requests list.

Parameters:

  • request (HTTPRequest)

    The incoming GET request

  • response (HTTPResponse)

    The response to return



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/maze/servlets/log_servlet.rb', line 24

def do_POST(request, response)
  hash = {
    body: JSON.parse(request.body),
    request: request,
    response: response
  }
  @requests.add(hash)

  response.header['Access-Control-Allow-Origin'] = '*'
  response.status = Server.status_code('POST')
rescue JSON::ParserError => e
  Bugsnag.notify e
  msg = "Unable to parse request as JSON: #{e.message}"
  $logger.error msg
  Server.invalid_requests.add({
    reason: msg,
    request: request,
    response: response,
    body: request.body
  })
rescue StandardError => e
  Bugsnag.notify e
  $logger.error "Invalid log request: #{e.message}"
  Server.invalid_requests.add({
    invalid: true,
    reason: e.message,
    request: {
      request_uri: request.request_uri,
      header: request.header.to_h,
      body: request.inspect
    },
    response: response
  })
end