From db7f9cc2d4d08570e130639985e4dba0fcaa5caa Mon Sep 17 00:00:00 2001 From: Matt Kasa Date: Thu, 23 Apr 2020 15:54:33 -0700 Subject: [PATCH] Test terraform state API using Unicorn::TeeInput Relates to https://gitlab.com/gitlab-org/gitlab/-/issues/215391 --- spec/requests/api/terraform/state_spec.rb | 16 ++++++++++++++ spec/support/unicorn.rb | 27 +++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 spec/support/unicorn.rb diff --git a/spec/requests/api/terraform/state_spec.rb b/spec/requests/api/terraform/state_spec.rb index 88c277f4e08d..844cd9484119 100644 --- a/spec/requests/api/terraform/state_spec.rb +++ b/spec/requests/api/terraform/state_spec.rb @@ -78,6 +78,14 @@ expect(response).to have_gitlab_http_status(:ok) end + + context 'on Unicorn', :unicorn do + it 'updates the state' do + expect { request }.to change { Terraform::State.count }.by(0) + + expect(response).to have_gitlab_http_status(:ok) + end + end end context 'without body' do @@ -112,6 +120,14 @@ expect(response).to have_gitlab_http_status(:ok) end + + context 'on Unicorn', :unicorn do + it 'creates a new state' do + expect { request }.to change { Terraform::State.count }.by(1) + + expect(response).to have_gitlab_http_status(:ok) + end + end end context 'without body' do diff --git a/spec/support/unicorn.rb b/spec/support/unicorn.rb new file mode 100644 index 000000000000..0b01fc9e26ca --- /dev/null +++ b/spec/support/unicorn.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +REQUEST_CLASSES = [ + ::Grape::Request, + ::Rack::Request +].freeze + +def request_body_class + return ::Unicorn::TeeInput if defined?(::Unicorn) + + Class.new(StringIO) do + def string + raise NotImplementedError, '#string is only valid under Puma which uses StringIO, use #read instead' + end + end +end + +RSpec.configure do |config| + config.before(:each, :unicorn) do + REQUEST_CLASSES.each do |request_class| + allow_any_instance_of(request_class) + .to receive(:body).and_wrap_original do |m, *args| + request_body_class.new(m.call(*args).read) + end + end + end +end