require File.dirname(__FILE__) + '/spec_helper.rb' # Time to add your specs! # http://rspec.rubyforge.org/ describe "Initial creation of ", WebSpeak::Request do before :each do @request = WebSpeak::Request.new end it "should not have any cookies" do @request.cookies.should be_empty end it "should not have any headers" do @request.headers.should be_empty end it "should not have any params" do @request.params.should be_empty end it "should not have any query_params" do @request.query_params.should be_empty end it "should have appropriate default connection values" do @request.host.should eql("localhost") @request.port.should eql(80) @request.path.should eql("/") end end describe "Posting a ", WebSpeak::Request do before :each do @request = WebSpeak::Request.new @mock_http = mock(:http) end it "should post properly" do @mock_http.should_receive(:post).with('/', '', {}).and_return(:response) Net::HTTP.should_receive(:start).with('localhost', 80).and_yield(@mock_http) @request.post @request.response.should eql(:response) end it "should post to different host information" do @request.host = 'foo.bar' @request.port = 1234 @request.path = '/other/path' @mock_http.should_receive(:post).with('/other/path', '', {}).and_return(:response) Net::HTTP.should_receive(:start).with('foo.bar', 1234).and_yield(@mock_http) @request.post end it "should contain headers when provided" do @request.headers['header_name'] = 'header_value' @mock_http.should_receive(:post).with('/', '', {'header_name' => 'header_value'}).and_return(:response) Net::HTTP.should_receive(:start).with('localhost', 80).and_yield(@mock_http) @request.post end it "should contain cookies when provided" do @request.cookies['cookie_name'] = 'cookie_value' @mock_http.should_receive(:post).with('/', '', {'Cookie' => 'cookie_name=cookie_value; '}).and_return(:response) Net::HTTP.should_receive(:start).with('localhost', 80).and_yield(@mock_http) @request.post end it "should contain query_params in the url when provided" do @request.query_params['param_name'] = 'param_value' @mock_http.should_receive(:post).with('/?param_name=param_value', '', {}).and_return(:response) Net::HTTP.should_receive(:start).with('localhost', 80).and_yield(@mock_http) @request.post end it "should contain params in the body when provided" do @request.params['param_name'] = 'param_value' @mock_http.should_receive(:post).with('/', 'param_name=param_value', {}).and_return(:response) Net::HTTP.should_receive(:start).with('localhost', 80).and_yield(@mock_http) @request.post end end describe "GETing a ", WebSpeak::Request do before :each do @request = WebSpeak::Request.new @mock_http = mock(:http) end it "should get a simple URL" do @request.host = 'foo.bar' @request.port = 1234 @request.path = '/get/path' @mock_http.should_receive(:get).with('/get/path', {}).and_return(:response) Net::HTTP.should_receive(:start).with('foo.bar', 1234).and_yield(@mock_http) @request.get end it "should add headers and cookies" do @request.headers['header_name'] = 'header_value' @request.cookies['cookie_name'] = 'cookie_value' @mock_http.should_receive(:get).with('/', {'header_name' => 'header_value', 'Cookie' => 'cookie_name=cookie_value; '}).and_return(:response) Net::HTTP.should_receive(:start).with('localhost', 80).and_yield(@mock_http) @request.get end it "should add params and query_params to the path" do @request.params['param_name'] = 'param_value' @request.query_params['query_param_name'] = 'query_param_value' @mock_http.should_receive(:get) do |path, hdrs| path.should match(/^\/\?/) path.should include("query_param_name=query_param_value") path.should include("param_name=param_value") hdrs.should be_empty :response end Net::HTTP.should_receive(:start).with('localhost', 80).and_yield(@mock_http) @request.get end end