require 'spec_helper' begin require 'rails' # Configure the Rails application ENV["RAILS_ENV"] = "test" require 'dummy/application' describe "Rails Integration" do let(:app) do Rails.application end let(:adapter) do MongoRequestLogger::Viewer.adapter end before :each do adapter.clear! end it "should initialize" do Rails.logger.should be_a MongoRequestLogger::Logger adapter.should be end it "should log basic info" do get '/' last_response.status.should == 200 last_response.body.should =~ /Home/ last_response.should be_ok results = adapter.query({}).to_a results.count.should == 1 result = results.first result['timestamp'].should be_within(1.minute).of(Time.now) result['path'].should == '/' result['host'].should == 'example.org' result['ip'].should == '127.0.0.1' result['request_method'].should == 'GET' result['response'].should == 200 result['user_agent'].should == '' end it "should log get parameters" do get '/?a=5&b=test' result = adapter.query({}).first result['path'].should == '/' result['params'].should == {'a' => '5', 'b' => 'test'} end it "should log post JSON paramters" do data = '{"test": "yes"}' post '/', {}, {input: data, "CONTENT_TYPE" => 'application/json'} result = adapter.query({}).first result['path'].should == '/' result['request_method'].should == 'POST' result['content_type'].should == 'application/json' result['content_length'].should == data.length result['response'].should == 200 # TODO: result['params'].should == {'test' => 'yes'} end it "should handle broken post JSON content" do data = "This is not JSON" post '/', {}, {input: data, "CONTENT_TYPE" => 'application/json'} last_response.status.should == 500 result = adapter.query({}).first result['path'].should == '/' result['response'].should == 500 # Why is it 500? result['request_method'].should == 'POST' result['content_type'].should == 'application/json' result['content_length'].should == data.length result['exception'].should be end it "should handle broken post XML content" do data = "This is not XML" post '/', {}, {input: data, "CONTENT_TYPE" => 'application/xml'} last_response.status.should == 500 result = adapter.query({}).first result['path'].should == '/' result['response'].should == 500 # Why is it 500? result['request_method'].should == 'POST' result['content_type'].should == 'application/xml' result['content_length'].should == data.length result['exception'].should be end end rescue LoadError # Cannot find Rails - skip these tests end