diff options
author | Mike Greiling <mike@pixelcog.com> | 2017-10-23 12:19:56 +0300 |
---|---|---|
committer | Mike Greiling <mike@pixelcog.com> | 2017-10-23 17:00:41 +0300 |
commit | b612bcfc64618906b5b11a245fb0aa2452d6c5dd (patch) | |
tree | 2b591e6c43a60f8f36d680458addecbad3f2db6c | |
parent | 04149dccab97aad4350cae9a6c6054b7de5c9850 (diff) | |
download | gitlab-ce-b612bcfc64618906b5b11a245fb0aa2452d6c5dd.tar.gz |
implement basic request inspector for use in Capybara tests
-rw-r--r-- | config/environments/test.rb | 1 | ||||
-rw-r--r-- | lib/gitlab/testing/request_inspector_middleware.rb | 59 |
2 files changed, 60 insertions, 0 deletions
diff --git a/config/environments/test.rb b/config/environments/test.rb index 1edb6fd39b8..d09e51e766a 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -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 diff --git a/lib/gitlab/testing/request_inspector_middleware.rb b/lib/gitlab/testing/request_inspector_middleware.rb new file mode 100644 index 00000000000..0ca21bd6a37 --- /dev/null +++ b/lib/gitlab/testing/request_inspector_middleware.rb @@ -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 |