require 'spec_helper' require 'eeny-meeny/models/encryptor' require 'eeny-meeny/middleware' def initialize_app(secure: true, secret: 'test', path: '/', same_site: :strict) EenyMeeny.reset! EenyMeeny.configure do |config| config.cookies = { path: path, same_site: same_site } config.experiments = YAML.load_file(File.join('spec','fixtures','experiments.yml')) config.secret = secret config.secure = secure end described_class.new(app) end describe EenyMeeny::Middleware do let(:app) { MockRackApp.new } describe 'when initialized' do subject { initialize_app } it 'sets the experiments' do expect(subject.instance_variable_get(:@experiments)).to be end it 'sets the cookie config' do expect(subject.instance_variable_get(:@cookie_config)).to be end end describe 'when called with a GET request' do subject { initialize_app } context "and the request doesn't contain the experiment cookie" do let(:request) { Rack::MockRequest.new(subject) } before(:example) do @response = request.get('/test', 'CONTENT_TYPE' => 'text/plain') end it "sets the 'HTTP_COOKIE' header on the request" do expect(app['HTTP_COOKIE']).to be expect(app['HTTP_COOKIE'].length).to be > 0 end it "sets the 'Set-Cookie' header on the response" do expect(@response['Set-Cookie']).to be expect(@response['Set-Cookie'].length).to be > 0 end end context 'and the request already contains the experiment cookie' do let(:request) { Rack::MockRequest.new(subject) } before(:example) do @original_request_cookies = 'test=abc;eeny_meeny_my_page_v1=on1tOQ5hiKdA0biVZVwvTUQcmkODacwdpi%2FedQJIYQz9KdWYAXqzCafF5Dqqa6xtHFBdXYVmz%2Bp4%2FigmKz4hBVYZbJU%2FMwBbvYG%2BIoBelk10PxwtyxbA%2BiDzFT4jZeiTkNOmZ3rp1Gzz74JjT4aocqB187p7SrpeM2jfyZ8ZKPOiZs6tXf0QoXkV%2BZbtxJLRPr5lgmGxslfM8vCIm1%2F0HQ%3D%3D;' @response = request.get('/test', 'CONTENT_TYPE' => 'text/plain', 'HTTP_COOKIE' => @original_request_cookies) end it "does not change the 'HTTP_COOKIE' header on the request" do expect(app['HTTP_COOKIE']).to eq(@original_request_cookies) end it "does not set the 'Set-Cookie' header on the response" do expect(@response['Set-Cookie']).to be nil end end end end