Commit 43095a61 authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Add tests and refactor middleware

parent c6d13e36
......@@ -2,4 +2,3 @@
# in the middleware stack, because it tracks events with
# GitLab Performance Monitoring
Rails.application.config.middleware.use(Gitlab::EtagCaching::Middleware)
Rails.application.config.middleware.use(Gitlab::Jira::Middleware)
# Treats JIRA DVCS user agent requests in order to be successfully handled
# by our API.
Rails.application.config.middleware.use(Gitlab::Jira::Middleware)
......@@ -6,12 +6,16 @@ module Gitlab
end
def call(env)
return @app.call(env) unless /JIRA DVCS Connector/.match(env['HTTP_USER_AGENT'])
env['HTTP_AUTHORIZATION'] = env['HTTP_AUTHORIZATION'].sub('token', 'Bearer')
env['HTTP_AUTHORIZATION'].sub!('token', 'Bearer') if jira_dvcs_connector?(env)
@app.call(env)
end
private
def jira_dvcs_connector?(env)
/JIRA DVCS Connector/.match(env['HTTP_USER_AGENT'])
end
end
end
end
require 'spec_helper'
describe Gitlab::Jira::Middleware do
let(:app) { double(:app) }
let(:middleware) { described_class.new(app) }
describe '#call' do
it 'adjusts HTTP_AUTHORIZATION env when request from JIRA DVCS user agent' do
user_agent = 'JIRA DVCS Connector Vertigo/5.0.0-D20170810T012915'
expect(app).to receive(:call).with('HTTP_USER_AGENT' => user_agent,
'HTTP_AUTHORIZATION' => 'Bearer hash-123')
middleware.call('HTTP_USER_AGENT' => user_agent, 'HTTP_AUTHORIZATION' => 'token hash-123')
end
it 'does not change HTTP_AUTHORIZATION env when request is not from JIRA DVCS user agent' do
env = { 'HTTP_USER_AGENT' => 'Mozilla/5.0', 'HTTP_AUTHORIZATION' => 'token hash-123' }
expect(app).to receive(:call).with(env)
middleware.call(env)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment