2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
# rubocop:disable Style/ClassVars
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Testing
|
|
|
|
class RequestInspectorMiddleware
|
|
|
|
@@log_requests = Concurrent::AtomicBoolean.new(false)
|
|
|
|
@@logged_requests = Concurrent::Array.new
|
|
|
|
@@inject_headers = Concurrent::Hash.new
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
Request = Struct.new(:url, :status_code, :request_headers, :response_headers, :body, keyword_init: true)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
# Resets the current request log and starts logging requests
|
|
|
|
def self.log_requests!(headers = {})
|
|
|
|
@@inject_headers.replace(headers)
|
|
|
|
@@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']
|
|
|
|
env.merge! http_headers_env(@@inject_headers) if @@inject_headers.any?
|
|
|
|
request_headers = env_http_headers(env)
|
|
|
|
status, headers, body = @app.call(env)
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
full_body = +''
|
2018-12-05 23:21:45 +05:30
|
|
|
body.each { |b| full_body << b }
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
request = Request.new(
|
2018-03-17 18:26:18 +05:30
|
|
|
url: url,
|
|
|
|
status_code: status,
|
|
|
|
request_headers: request_headers,
|
2018-12-05 23:21:45 +05:30
|
|
|
response_headers: headers,
|
|
|
|
body: full_body
|
2018-03-17 18:26:18 +05:30
|
|
|
)
|
|
|
|
log_request request
|
|
|
|
|
|
|
|
[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 http_headers_env(headers)
|
|
|
|
Hash[*headers
|
|
|
|
.collect { |k, v| [k.split('-').collect(&:upcase).join('_'), v] }
|
|
|
|
.collect { |k, v| [k.prepend('HTTP_'), v] }
|
|
|
|
.flatten]
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_request(response)
|
|
|
|
@@logged_requests.push(response)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|