require File.dirname(__FILE__) + '/spec_helper' describe Baurets::Optionsful::Server do include Rack::Test::Methods context "as a Rack middleware" do it "is a Ruby object that responds to call;" do assert ::Baurets::Optionsful::Server.new(app).respond_to? :call end it "takes exactly one argument, (the environment) and returns an Array;" do response = ::Baurets::Optionsful::Server.new(app).call(mock_env({"REQUEST_METHOD" => "OPTIONS", "PATH_INFO" => "/posts"})) assert response.kind_of?(Array) end it "the returned Array must have exactly three values: the status, the headers and the body;" do response = ::Baurets::Optionsful::Server.new(app).call(mock_env({"REQUEST_METHOD" => "OPTIONS", "PATH_INFO" => "/posts"})) assert response.size.should == 3 assert response[0].kind_of? Fixnum assert response[1].kind_of? Hash assert response[2].kind_of? String end before do ActionController::Routing::Routes.draw do |map| map.resources :posts, :has_many => :comments end end it "must be nice, acting somewhere on a Rack middleware stack;" do response = fake_opts_app.call(mock_env({"REQUEST_METHOD" => "OPTIONS", "PATH_INFO" => "/posts"})) assert response.size.should == 3 assert response[0].kind_of? Fixnum assert response[0].should == 204 assert response[1].kind_of? Hash assert response[1]["Allow"] end it "must let the request go through the stack, if it has nothing to it!" do response = fake_opts_app.call(mock_env({"REQUEST_METHOD" => "GET", "PATH_INFO" => "/lobster"})) assert response.size.should == 3 assert response[0].kind_of? Fixnum assert response[0].should == 200 assert response[1].kind_of? Hash end end context "as an interpreter for HTTP OPTIONS requests, MUST recognize the Rails" do describe "default resource routing" do #Sample resource route (maps HTTP verbs to controller actions automatically) before(:all) do ActionController::Routing::Routes.draw do |map| map.resources :posts, :has_many => :comments end end it "the index action displays a list of all posts in response of a GET request" do response = http_options_request("/posts") assert response.kind_of?(Array) assert allows?(response[1], "GET") end it "the new action return from a GET request an HTML form for creating a new post" do response = http_options_request("/posts/new") assert response.kind_of?(Array) assert allows?(response[1], "GET") end it "the create action uses POST to create a new post instance" do response = http_options_request("/posts") assert response.kind_of?(Array) assert allows?(response[1], "POST") end it "the show action display a specific post in response of a GET request" do response = http_options_request("/posts/1") assert response.kind_of?(Array) assert allows?(response[1], "GET") end it "the edit action return an HTML form for editing a post in response of a GET request" do response = http_options_request("/posts/1/edit") assert response.kind_of?(Array) assert allows?(response[1], "GET") end it "the update action uses PUT to update a specific post" do response = http_options_request("/posts/1") assert response.kind_of?(Array) assert allows?(response[1], "PUT") end it "the destroy action uses DELETE to delete a specific post" do response = http_options_request("/posts/1") assert response.kind_of?(Array) assert allows?(response[1], "DELETE") end it "not finding a path, return 404 Not Found" do response = http_options_request("/sblingers/sblongers") assert response.kind_of?(Array) assert response[0].should be 404 end ## # Note that extension relation types are REQUIRED to be absolute URIs # in Link headers, and MUST be quoted if they contain a semicolon (";") # or comma (",") (as these characters are used as delimiters in the # header itself). it "the Link header MUST be quoted if it contains a semicolon or comma" do response = http_options_request("/posts") assert response.kind_of?(Array) link = response[1]["Link"] assert link.should =~ /\A"{1}.+"\z/ end end describe "named routes" do before(:each) do ActionController::Routing::Routes.draw do |map| map.login 'login', :controller => 'accounts', :action => 'login' end end it "should work" do response = http_options_request("/login") assert response.kind_of?(Array) assert response[0].should be 204 assert allows?(response[1], "GET") # WTF? return ANY?!? ;p end after(:all) do ActionController::Routing::Routes.reload! end end describe "route globbing" do before(:all) do ActionController::Routing::Routes.draw do |map| map.connect '*path' , :controller => 'blog' , :action => 'unrecognized?' end end it "should work" do pending "What the hell is that?" response = http_options_request("/post/path") assert response.kind_of?(Array) assert response[0].should_not be 404 end after(:all) do ActionController::Routing::Routes.reload! end end describe "route conditions" do before(:all) do ActionController::Routing::Routes.draw do |map| map.connect 'post/:id', :controller => 'posts', :action => 'show', :conditions => { :method => :get } map.connect 'post/:id', :controller => 'posts', :action => 'create_comment', :conditions => { :method => :post } end end it "should work" do response = http_options_request("/post/123") assert response.kind_of?(Array) assert allows?(response[1], "GET") assert allows?(response[1], "POST") end after(:all) do ActionController::Routing::Routes.reload! end end describe "pretty URLs" do before(:all) do ActionController::Routing::Routes.draw do |map| map.connect "articles/:year/:month/:day", :controller => 'articles', :action => 'find_by_date', :requirements => { :year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/ } end end it "should work" do response = http_options_request("/articles/2010/07/23") assert response.kind_of?(Array) assert response[0].should be 204 end after(:all) do ActionController::Routing::Routes.reload! end end describe "regular expressions and parameters" do before(:all) do ActionController::Routing::Routes.draw do |map| map.geocode 'geocode/:postalcode', :controller => 'geocode', :action => 'show', :requirements => { :postalcode => /\d{5}(-\d{4})?/ } end end it "should work" do response = http_options_request("/geocode/20100") assert response.kind_of?(Array) assert response[0].should be 204 assert allows?(response[1], "GET") end after(:all) do ActionController::Routing::Routes.reload! end end describe "resource route within a namespace" do before(:all) do ActionController::Routing::Routes.draw do |map| map.namespace :admin do |admin| # Directs /admin/posts/* to Admin::PostsController (app/controllers/admin/posts_controller.rb) admin.resources :posts end end end it "must understand an namespaced path" do response = http_options_request("/admin/posts") assert response.kind_of?(Array) assert response[0].should be 204 assert allows?(response[1], "GET") assert allows?(response[1], "POST") end after(:all) do ActionController::Routing::Routes.reload! end end describe "deal the catch all" do before(:all) do ActionController::Routing::Routes.draw do |map| map.resources :posts map.connect ':controller/:action/:id' end end it "must cheat :P" do response = http_options_request("/posts") assert response.kind_of?(Array) assert response[0].should be 204 assert allows?(response[1], "GET") assert allows?(response[1], "POST") end after(:all) do ActionController::Routing::Routes.reload! end end describe "another sample of named route" do before(:all) do ActionController::Routing::Routes.draw do |map| map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase' end end it "should work" do response = http_options_request("/products/123/purchase") assert response.kind_of?(Array) assert response[0].should be 204 assert allows?(response[1], "ANY") end after(:all) do ActionController::Routing::Routes.reload! end end describe "Sample resource route with options" do before(:all) do ActionController::Routing::Routes.draw do |map| map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get } end end it "should work" do response = http_options_request("/products/123/toggle") assert response.kind_of?(Array) assert response[0].should be 204 assert allows?(response[1], "POST") end it "should work" do response = http_options_request("/products/123/short") assert response.kind_of?(Array) assert response[0].should be 204 assert allows?(response[1], "GET") end it "should work" do response = http_options_request("/products/sold") assert response.kind_of?(Array) assert response[0].should be 204 assert allows?(response[1], "GET") end after(:all) do ActionController::Routing::Routes.reload! end end end end