DMA32
  F   ˆl–Ãa¾”*i?u8 …p/Üiå1hß�þ_e¹qÀ
YyÙ%lŸÏÂ„e×DßÁ_�uÐb&=LyZËcõÁÀ ?÷     # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'
require 'rack/handler/puma'

# this cop is disabled to speed up testing by avoiding the overhead of multiple requests

RSpec.describe UserAgent, :aggregate_failures do
  include ZammadSpecSupportRequest

  before :all do # rubocop:disable RSpec/BeforeAfterAll
    ENV['CI_BASIC_AUTH_USER']     = 'basic_auth_user'
    ENV['CI_BASIC_AUTH_PASSWORD'] = 'test123'
    ENV['CI_BEARER_TOKEN']        = 'test_bearer_123'
  end

  # using def instead of let to make it available in before(:all)
  def base_host
    'localhost'
  end

  def host
    "http://#{base_host}:3000"
  end

  def ssl_host
    "https://#{base_host}:3001"
  end

  def start_server(with_ssl: nil)
    if with_ssl.present?
      localhost_authority = Localhost::Authority.new(base_host, issuer: nil)
      localhost_authority.save # make sure the certificate is created

      puma_host = "ssl://0.0.0.0?key=#{localhost_authority.key_path}&cert=#{localhost_authority.certificate_path}"
    end

    port = with_ssl.present? ? 3001 : 3000

    @puma_thread = Thread.new do
      app = Rack::Builder.new do
        map '/' do
          run Rails.application
        end
      end.to_app

      Rack::Handler::Puma.run app, Port: port, Host: puma_host do |s|
        @puma_server = s
      end
    end

    check_host = with_ssl.present? ? ssl_host : host

    10.times do
      break if system("curl -sSfk #{check_host}/test/get_accepted/1 > /dev/null")

      sleep 0.2
    end
  end

  def stop_server
    @puma_server.stop # rubocop:disable RSpec/InstanceVariable
    @puma_thread.join # rubocop:disable RSpec/InstanceVariable
  end

  shared_context 'when doing user agent tests' do
    shared_examples 'successful request' do
      it 'returns a response' do
        expect(response).to be_success
        expect(response.code).to eq(code)
      end
    end

    shared_examples 'successful request with json body' do
      it 'returns a response' do
        expect(response).to be_success
        expect(response.code).to eq(code)
        expect(json_response).to include(expected_body)
      end
    end

    shared_examples 'successful get request' do
      it 'returns a response' do
        expect(response).to be_success
        expect(response.code).to eq(code)
        expect(response.header).to include('content-type' => content_type)
        expect(json_response).to include(expected_body)
      end
    end

    shared_examples 'successful post/put/patch request' do
      include_examples 'successful request with json body'
    end

    shared_examples 'successful delete request' do
      include_examples 'successful request with json body'
    end

    shared_examples 'successful redirect request' do
      include_examples 'successful request with json body'
    end

    shared_examples 'unsuccessful request with body' do
      it 'returns a response' do
        expect(response).not_to be_success
        expect(response.code).to eq(code)
        expect(response.body).to be_present
      end
    end

    shared_examples 'unsuccessful request without body' do
      it 'returns a response' do
        expect(response).not_to be_success
        expect(response.code).to eq(code)
        expect(response.body).to be_nil
      end
    end

    shared_examples 'unsuccessful get/post/put/delete request' do
      it 'returns a response' do
        expect(response).not_to be_success
        expect(response.code).to eq(code)
        expect(response.body).to eq(expected_body)
      end
    end

    context 'with an insecure connection' do
      before :all do # rubocop:disable RSpec/BeforeAfterAll
        start_server
      end

      after :all do # rubocop:disable RSpec/BeforeAfterAll
        stop_server
      end

      describe '#get' do
        context 'without http basic auth' do
          subject(:response) { described_class.get(request_url, {}, options) }

          let(:options) { {} }

          co