require File.dirname(__FILE__) + '/spec_helper' describe Baurets::Optionsful::Documentator do include Rack::Test::Methods Baurets::Optionsful::Documentator.send(:remove_const, :RAILS_ROOT) if Baurets::Optionsful::Documentator.const_defined?(:RAILS_ROOT) Baurets::Optionsful::Documentator.const_set(:RAILS_ROOT, File.join(File.dirname(__FILE__), 'samples' ) ) base_path = ::Baurets::Optionsful::Config.new.base_path describe "as a Rack middleware" do it "is a Ruby object that responds to call;" do assert ::Baurets::Optionsful::Documentator.new(app).respond_to? :call end before do ActionController::Routing::Routes.draw do |map| map.resources :posts, :has_many => :comments end end it "takes exactly one argument, (the environment) and returns an Array;" do response = ::Baurets::Optionsful::Documentator.new(app).call(mock_env({"REQUEST_METHOD" => "GET", "PATH_INFO" => (base_path + "/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::Documentator.new(app).call(mock_env({"REQUEST_METHOD" => "GET", "PATH_INFO" => (base_path + "/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 it "must be nice, acting somewhere on a Rack middleware stack." do response = fake_docs_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 describe "extracts metadata information about a resource" do describe "MUST override Rails routing recognition successfully" do before(:all) do ActionController::Routing::Routes.draw do |map| map.resources :posts, :has_many => :comments map.resources :notes map.resources :stuff map.resources :trips map.resources :teams end end it "may let the request go through if base path doesn't match" do pending # req_path = "/xyz" # response = http_get_request(req_path) # assert response[0].kind_of? Fixnum # assert response[0].should_not == 500 end it "MAY find proper structured information and build an html response" do Baurets::Optionsful::Documentator.send(:remove_const, :RAILS_ROOT) if Baurets::Optionsful::Documentator.const_defined?(:RAILS_ROOT) Baurets::Optionsful::Documentator.const_set(:RAILS_ROOT, File.join(File.dirname(__FILE__), 'samples' ) ) req_path = base_path + "/posts" response = http_get_request(req_path) assert response[0].kind_of? Fixnum assert response[0].should == 200 end it "MAY NOT find proper structured information and build an html response" do req_path = base_path + "/notes" response = http_get_request(req_path) assert response.kind_of?(Array) assert response[0].kind_of? Fixnum end it "MAY NOT find controller file and build an html response" do req_path = base_path + "/stuff" response = http_get_request(req_path) assert response.kind_of?(Array) assert response[0].kind_of? Fixnum end it "MAY NOT understand anything" do req_path = base_path + "/trips" response = http_get_request(req_path) assert response.kind_of?(Array) assert response[0].kind_of? Fixnum end it "MAY NOT understand anything" do req_path = base_path + "/teams" response = http_get_request(req_path) assert response.kind_of?(Array) assert response[0].kind_of? Fixnum end after(:all) do ActionController::Routing::Routes.reload! end end end end