Permalink
Browse files

Merge branch 'middleware-specs'

  • Loading branch information...
2 parents 320a2e9 + 3b1ef34 commit 86bc2a793280dc5138885f99f0eea37cdeb93557 @ajsharp ajsharp committed Jun 11, 2011
Showing with 80 additions and 24 deletions.
  1. +2 −1 Gemfile
  2. +6 −0 Gemfile.lock
  3. +22 −21 lib/aarrr/middleware.rb
  4. +2 −2 lib/aarrr/session.rb
  5. +45 −0 spec/functional/middleware_spec.rb
  6. +3 −0 spec/spec_helper.rb
View
3 Gemfile
@@ -13,4 +13,5 @@ gem "growl"
gem "guard"
gem "guard-rspec"
-gem "rake", "= 0.8.7"
+gem "rake", "= 0.8.7"
+gem "rack-test"
View
6 Gemfile.lock
@@ -26,6 +26,8 @@ GEM
mongo (1.3.1)
bson (>= 1.3.1)
rack (1.3.0)
+ rack-test (0.5.7)
+ rack (>= 1.0)
rake (0.8.7)
rb-fsevent (0.4.0)
rspec (2.6.0)
@@ -58,10 +60,14 @@ PLATFORMS
DEPENDENCIES
aarrr!
+ activesupport (>= 3.0)
bson_ext
growl
guard
guard-rspec
+ mongo (>= 1.3.1)
+ rack (>= 1.2.2)
+ rack-test
rake (= 0.8.7)
rb-fsevent
rspec (>= 2.0)
View
43 lib/aarrr/middleware.rb
@@ -1,22 +1,23 @@
# DONT USE THIS, ITS FUCKED
-# module AARRR
-# class Middleware
-#
-# def initialize(app)
-# @app = app
-# end
-#
-# def call(env)
-# aarrr_session = AARRR(env)
-#
-# status, headers, body = @app.call(env)
-# # require 'ruby-debug';debugger
-# # sets a tracking cookie on the response
-# response = Rack::Response.new body, status, headers
-# # aarrr_session.set_cookie(response)
-#
-# response.finish
-# end
-#
-# end
-# end
+
+module AARRR
+ class Middleware
+
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ aarrr_session = AARRR(env)
+
+ status, headers, body = @app.call(env)
+ # require 'ruby-debug';debugger
+ # sets a tracking cookie on the response
+ response = Rack::Response.new body, status, headers
+ aarrr_session.set_cookie(response)
+
+ response.finish
+ end
+
+ end
+end
View
4 lib/aarrr/session.rb
@@ -129,7 +129,7 @@ def parse_id(env_or_object)
# if it's a hash, then process like a request and pull out the cookie
if env_or_object.is_a?(Hash)
- if env_or_object["rack.session"].is_a?(Hash) and env_or_object["rack.session"]["user_id"].present?
+ if env_or_object["rack.session"].is_a?(Hash) and env_or_object["rack.session"]["user_id"]
# lookup user_id
user = AARRR.users.find_one({"user_id" => env_or_object["rack.session"]["user_id"].to_s})
if user.present?
@@ -166,7 +166,7 @@ def build_attributes(env_or_object)
user_attributes["ip_address"] = ip_address if ip_address
# set user_id if its in the session
- if env_or_object["rack.session"].is_a?(Hash) and env_or_object["rack.session"]["user_id"].present?
+ if env_or_object["rack.session"].is_a?(Hash) and env_or_object["rack.session"]["user_id"]
user_attributes["user_id"] = env_or_object["rack.session"]["user_id"].to_s
end
View
45 spec/functional/middleware_spec.rb
@@ -0,0 +1,45 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe 'an app that includes the aarrr middleware' do
+ def app
+ app = Rack::Builder.app do
+
+ use AARRR::Middleware
+ run lambda { |env|
+ [200, {'Content-Type' => 'text/plain'}, ["Hello"]]
+ }
+ end
+ end
+
+ it "includes the id in a cookie in the response" do
+ response = Rack::MockRequest.new(app).get('/')
+ user = AARRR.users.find_one
+ response.headers['Set-Cookie'].should =~ /_utmarr=#{user["_id"]}; path=\//
+ end
+
+ context "when including with another middleware" do
+ def app
+ Rack::Builder.app do
+
+ use Rack::Session::Cookie, :key => 'rack.session',
+ :secret => 'secret',
+ :expire_after => 10000
+ use AARRR::Middleware
+ run lambda { |env|
+ env['rack.session'] = {"user_id" => 'myid'}
+ [200, {'Content-Type' => 'text/plain'}, []]
+ }
+ end
+ end
+
+ it "includes the session cookie in the response" do
+ get '/'
+ last_response.headers["Set-Cookie"].should =~ /rack\.session/
+ end
+
+ it "includes the aarrr cookie in the response" do
+ get '/'
+ last_response.headers['Set-Cookie'].should =~ /_utmarr/
+ end
+ end
+end
View
3 spec/spec_helper.rb
@@ -1,5 +1,6 @@
require 'bundler/setup'
require 'aarrr'
+require 'rack/test'
RSpec.configure do |config|
@@ -15,6 +16,8 @@
AARRR.database.collections.select {|c| c.name !~ /system/ }.each(&:drop)
end
+ config.include(Rack::Test::Methods)
+
# add helper methods here
end

0 comments on commit 86bc2a7

Please sign in to comment.