require 'spec_helper' require 'rack' require 'hawkei/plugins/rack/middleware' describe Hawkei::Plugins::Rack::Middleware do let(:app) { lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['All responses are OK']] } } let(:middleware) { Hawkei::Plugins::Rack::Middleware.new(app) } context 'reset the store' do before do 2.times { middleware.call({}) } end it { expect(Hawkei::Store.store).to eq({}) } end it 'reset the store with error' do allow(middleware).to receive(:call).and_raise(RuntimeError) expect { middleware.call(error: true) }.to raise_error(RuntimeError) expect(Hawkei::Store.store).to eq({}) end it 'set correct value for cookies' do _status, headers, _body = middleware.call({}) expect(headers['Set-Cookie']).to match(%r{path=/}) expect(headers['Set-Cookie']).to match(%r{max-age=315360000}) expect(headers['Set-Cookie']).to_not match(%r{domain}) end it 'set correct value for cookies with domain' do Hawkei.configurations.domain = "hawkei.io" _status, headers, _body = middleware.call({}) expect(headers['Set-Cookie']).to match(%r{path=/}) expect(headers['Set-Cookie']).to match(%r{max-age=315360000}) expect(headers['Set-Cookie']).to match(%r{domain=hawkei.io}) end it 'store the request' do _status, headers, _body = middleware.call({ 'hawkei_test' => true, 'HTTP_HOST' => '127.0.0.1:3000', 'HTTP_REFERER' => 'http://127.0.0.1:3000/?hello=test&password=1234', 'HTTP_COOKIE' => 'other', 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'HTTP_VERSION' => 'HTTP/1.1', 'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36', 'HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded', 'HTTP_CACHE_CONTROL' => 'max-age=0', 'HTTP_CONTENT_LENGTH' => '190', 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate, br', 'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.9,fr;q=0.8,ru;q=0.7', }) expected = { :url => "", :ssl => false, :host => "127.0.0.1", :port => 3000, :path => "", :referrer => "http://127.0.0.1:3000/?hello=test&password=HIDDEN", :method => nil, :xhr => false, :user_agent => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36", :ip => nil, :get_params => {}, :post_params => {}, :headers => { "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding" => "gzip, deflate, br", "Accept-Language" => "en-US,en;q=0.9,fr;q=0.8,ru;q=0.7", "Cache-Control" => "max-age=0", "Content-Length" => "190", "Content-Type" => "application/x-www-form-urlencoded", "Host" => "127.0.0.1:3000", "Referer" => "http://127.0.0.1:3000/?hello=test&password=1234", "User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36", "Version" => "HTTP/1.1" } } expect(Hawkei::Store.store[:request]).to include(expected) end end