require File.dirname(__FILE__) + "/../spec_helper" describe Compp::Default do describe "send_401" do before(:each) do @c = Compp::Default.new({}) @mock_response = mock(EventMachine::DelegatedHttpResponse, :status= => true, :content= => true, :send_response => true) EventMachine::DelegatedHttpResponse.stub!(:new).and_return(@mock_response) end it "should create a new response" do EventMachine::DelegatedHttpResponse.should_receive(:new).with(@c).and_return(@mock_response) @c.send_401 end describe "response" do it "should have a 401 status" do @mock_response.should_receive(:status=).with(401) @c.send_401 end it "should have the right error message" do @mock_response.should_receive(:content=).with("Please, authenticate with with jid:password\n\n") @c.send_401 end end it "should send this response" do @mock_response.should_receive(:send_response) @c.send_401 end end describe "send_403" do before(:each) do @c = Compp::Default.new({}) @mock_response = mock(EventMachine::DelegatedHttpResponse, :status= => true, :content= => true, :send_response => true) EventMachine::DelegatedHttpResponse.stub!(:new).and_return(@mock_response) end it "should create a new response" do EventMachine::DelegatedHttpResponse.should_receive(:new).with(@c).and_return(@mock_response) @c.send_403 end describe "response" do it "should have a 401 status" do @mock_response.should_receive(:status=).with(403) @c.send_403 end it "should have the right error message" do @mock_response.should_receive(:content=).with("Couldn't authenticate XMPP user\n\n") @c.send_403 end end it "should send this response" do @mock_response.should_receive(:send_response) @c.send_403 end end describe "register_xmpp_handlers" do before(:each) do @c = Compp::Default.new({}) @client = mock(Blather::Client) @data = mock(Nokogiri::XML::Node, :to_xml => "cool") @client.stub!(:register_handler).with(:message).and_yield(@data) @c.stub(:orbitize) end it 'should resgister a handler on message' do @client.should_receive(:register_handler).with(:message).and_yield(@data) @c.register_xmpp_handlers(@client) end it "should call to_xml on the data" do @data.should_receive(:to_xml) @c.register_xmpp_handlers(@client) end it "should orbitize any message received" do @c.should_receive(:orbitize).with(@data.to_xml) @c.register_xmpp_handlers(@client) end end describe "process_http_request" do before(:each) do @c = Compp::Default.new({}) @c.stub!(:extract_jid_and_password_from_headers) @c.stub!(:send_401) end it "should extract the jid and password" do @c.should_receive(:extract_jid_and_password_from_headers) @c.process_http_request end it "should call send_401 if no jid and password were extracted" do @c.stub!(:extract_jid_and_password_from_headers) @c.should_receive(:send_401) @c.process_http_request end context 'when a jid password was exatcrted' do before(:each) do @jid = "julien@superfeedr.com" @password = "password" @mock_xmpp_client = mock(Blather::Client, :register_handler => true, :connect => true) @c.stub!(:extract_jid_and_password_from_headers).and_return([@jid, @password]) Blather::Client.stub!(:setup).and_return(@mock_xmpp_client) @mock_xmpp_client.stub!(:register_handler).with(:ready).and_yield() @c.stub!(:orbitize) end it "should create a new xmpp client" do Blather::Client.should_receive(:setup).with(@jid, @password).and_return(@mock_xmpp_client) @c.process_http_request end it "should register a handler on ready" do @mock_xmpp_client.should_receive(:register_handler).with(:ready).and_yield() @c.process_http_request end it "should orbitize the http headers when ready" do @c.should_receive(:orbitize).with("HTTP/1.0 200 OK") @c.should_receive(:orbitize).with("Content-Type: text/xml") @c.should_receive(:orbitize).with("Connection: close\r\n") @c.should_receive(:orbitize).with("") @c.process_http_request end it "should call register_xmpp_handlers" do @c.should_receive(:register_xmpp_handlers).with(@mock_xmpp_client) @c.process_http_request end it "should connect the client" do @mock_xmpp_client.should_receive(:connect) @c.process_http_request end end end describe "extract_jid_and_password_from_headers" do before(:each) do @c = Compp::Default.new({}) end it "should extract the Auth headers" do @c.http_headers = "Authorization: Basic anVsaWVuOnBhc3N3b3Jk\000User-Agent: curl/7.19.6 (i386-apple-darwin10.0.0) libcurl/7.19.6 zlib/1.2.3\000Host: 0.0.0.0:8000\000Accept: */*\000\000" @c.extract_jid_and_password_from_headers.should == ["julien", "password"] end it "should return nil, nil if no Auth header was found" do @c.http_headers = "User-Agent: curl/7.19.6 (i386-apple-darwin10.0.0) libcurl/7.19.6 zlib/1.2.3\000Host: 0.0.0.0:8000\000Accept: */*\000\000" @c.extract_jid_and_password_from_headers.should == [nil, nil] end end end