2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-15 10:21:04 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::QueryLimiting::Middleware do
|
2018-01-15 10:21:04 -05:00
|
|
|
describe '#call' do
|
|
|
|
it 'runs the application with query limiting in place' do
|
|
|
|
middleware = described_class.new(-> (env) { env })
|
|
|
|
|
2019-12-16 07:07:43 -05:00
|
|
|
expect_next_instance_of(Gitlab::QueryLimiting::Transaction) do |instance|
|
|
|
|
expect(instance).to receive(:act_upon_results)
|
|
|
|
end
|
2018-01-15 10:21:04 -05:00
|
|
|
|
|
|
|
expect(middleware.call({ number: 10 }))
|
|
|
|
.to eq({ number: 10 })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#action_name' do
|
|
|
|
let(:middleware) { described_class.new(-> (env) { env }) }
|
|
|
|
|
|
|
|
context 'using a Rails request' do
|
|
|
|
it 'returns the name of the controller and action' do
|
|
|
|
env = {
|
|
|
|
described_class::CONTROLLER_KEY => double(
|
|
|
|
:controller,
|
|
|
|
action_name: 'show',
|
|
|
|
class: double(:class, name: 'UsersController'),
|
2020-01-29 13:08:47 -05:00
|
|
|
media_type: 'text/html'
|
2018-01-15 10:21:04 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(middleware.action_name(env)).to eq('UsersController#show')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the content type if this is not text/html' do
|
|
|
|
env = {
|
|
|
|
described_class::CONTROLLER_KEY => double(
|
|
|
|
:controller,
|
|
|
|
action_name: 'show',
|
|
|
|
class: double(:class, name: 'UsersController'),
|
2020-01-29 13:08:47 -05:00
|
|
|
media_type: 'application/json'
|
2018-01-15 10:21:04 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(middleware.action_name(env))
|
|
|
|
.to eq('UsersController#show (application/json)')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'using a Grape API request' do
|
|
|
|
it 'returns the name of the request method and endpoint path' do
|
|
|
|
env = {
|
|
|
|
described_class::ENDPOINT_KEY => double(
|
|
|
|
:endpoint,
|
|
|
|
route: double(:route, request_method: 'GET', path: '/foo')
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(middleware.action_name(env)).to eq('GET /foo')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil if the route can not be retrieved' do
|
|
|
|
endpoint = double(:endpoint)
|
|
|
|
env = { described_class::ENDPOINT_KEY => endpoint }
|
|
|
|
|
|
|
|
allow(endpoint)
|
|
|
|
.to receive(:route)
|
|
|
|
.and_raise(RuntimeError)
|
|
|
|
|
|
|
|
expect(middleware.action_name(env)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|