implement basic request inspector for use in Capybara tests

This commit is contained in:
Mike Greiling 2017-10-23 12:19:56 +03:00
parent 04149dccab
commit b612bcfc64
No known key found for this signature in database
GPG Key ID: 0303DF507FA67596
2 changed files with 60 additions and 0 deletions

View File

@ -1,6 +1,7 @@
Rails.application.configure do
# Make sure the middleware is inserted first in middleware chain
config.middleware.insert_before('ActionDispatch::Static', 'Gitlab::Testing::RequestBlockerMiddleware')
config.middleware.insert_before('ActionDispatch::Static', 'Gitlab::Testing::RequestInspectorMiddleware')
# Settings specified here will take precedence over those in config/application.rb

View File

@ -0,0 +1,59 @@
module Gitlab
module Testing
class RequestInspectorMiddleware
@@log_requests = Concurrent::AtomicBoolean.new(false)
@@logged_requests = Concurrent::Array.new
# Resets the current request log and starts logging requests
def self.log_requests!
@@logged_requests.replace([])
@@log_requests.value = true
end
# Stops logging requests
def self.stop_logging!
@@log_requests.value = false
end
def self.requests
@@logged_requests
end
def initialize(app)
@app = app
end
def call(env)
return @app.call(env) unless @@log_requests.true?
url = env['REQUEST_URI']
request_headers = env_http_headers(env)
status, headers, body = @app.call(env)
log_response({
url: url,
status_code: status,
request_headers: request_headers,
response_headers: headers
})
[status, headers, body]
end
private
def env_http_headers(env)
Hash[*env.select {|k,v| k.start_with? 'HTTP_'}
.collect {|k,v| [k.sub(/^HTTP_/, ''), v]}
.collect {|k,v| [k.split('_').collect(&:capitalize).join('-'), v]}
.sort
.flatten]
end
def log_response(response)
@@logged_requests.push(response)
STDOUT.puts response.to_json
end
end
end
end