Class: Maze::Servlets::LogServlet
- Inherits:
-
BaseServlet
- Object
- WEBrick::HTTPServlet::AbstractServlet
- BaseServlet
- Maze::Servlets::LogServlet
- Defined in:
- lib/maze/servlets/log_servlet.rb
Overview
Receives log requests sent from the test fixture
Instance Method Summary collapse
-
#do_OPTIONS(request, response) ⇒ Object
Logs and returns a set of valid headers for this servlet.
-
#do_POST(request, response) ⇒ Object
Logs and parses an incoming POST request.
-
#initialize(server) ⇒ LogServlet
constructor
Constructor.
Constructor Details
#initialize(server) ⇒ LogServlet
Constructor
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.
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.
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.}" $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.}" Server.invalid_requests.add({ invalid: true, reason: e., request: { request_uri: request.request_uri, header: request.header.to_h, body: request.inspect }, response: response }) end |