spec/ferver_spec.rb in ferver-1.1.0 vs spec/ferver_spec.rb in ferver-1.2.0

- old
+ new

@@ -1,231 +1,183 @@ -require "spec_helper" +require 'spec_helper' -describe "ferver" do - include Webrat::Matchers # allow cool html matching +describe 'ferver' do + let(:file_list) { double('file-list') } + context 'given a request to the server root' do + before do + get '/' + end - context "given a request to the server root" do + it 'should return redirect' do + expect(last_response).to be_redirect + end - before(:each) do - get "/" - end + it 'should redirect to file list' do + follow_redirect! - it "should return redirect" do - expect(last_response).to be_redirect - end + expect(last_response).to be_ok + expect(last_request.url).to match(/files/) + end + end - it "should redirect to file list" do - follow_redirect! + describe 'choosing directory to serve files from' do + before do + allow(file_list).to receive(:all).and_return(EMPTY_FILE_LIST) + allow(file_list).to receive(:size).and_return(0) + end - expect(last_response).to be_ok - expect(last_request.url).to match(/files/) - end + context 'when no directory is specified' do + it 'will use default directory' do + expect(Ferver::FileList).to receive(:new).with('./').and_return(file_list) + get '/files' + end end - describe "choosing directory to serve files from" do - - before { - @file_list = mock() - @file_list.stubs(:files).returns(EMPTY_FILE_LIST) - @file_list.stubs(:file_count).returns(0) - } - - context "when no directory is specified" do - - it "will use default directory" do - - Ferver::FileList.expects(:new).with("./").returns(@file_list) - - get "/files" - end - + context 'when the directory passed via configuration' do + before do + Ferver.configure do |conf| + conf.directory_path = '/foo' end + end - context "when the directory passed via configuration" do + it 'will use directory specified' do + expect(Ferver::FileList).to receive(:new).with('/foo').and_return(file_list) - before { Ferver::App.set :ferver_path, "/foo" } + get '/files' + end + end - it "will use directory specified" do - Ferver::FileList.expects(:new).with("/foo").returns(@file_list) + context 'when directory does not exist' do + before do + allow(Ferver::FileList).to receive(:new).and_raise(Ferver::DirectoryNotFoundError) + end - get "/files" - end + it 'will return server error status' do + get '/files' - end + expect(last_response.status).to eql 500 + end + end + end - context "when directory does not exist" do + context 'given an empty list of files' do + before do + allow(file_list).to receive(:all).and_return(EMPTY_FILE_LIST) + allow(file_list).to receive(:size).and_return(0) + allow(Ferver::FileList).to receive(:new).and_return(file_list) + end - before(:each) { Ferver::App.set :ferver_path, "/foo" } + context 'when no content-type is requested' do + before { get '/files' } - it "will attempt to create FileList with path" do - Ferver::FileList.expects(:new).with("/foo").raises(Ferver::DirectoryNotFoundError) + it 'should return valid response' do + expect(last_response).to be_ok + end - get "/files" - end + it 'should contain no file list in response content' do + expect(last_response.body).not_to have_tag('li') + expect(last_response.body).to have_tag('p', text: /0 files/) + end + end - it "will return server error status" do - Ferver::FileList.stubs(:new).with("/foo").raises(Ferver::DirectoryNotFoundError) + context 'when json content-type is requested' do + before do + get '/files', {}, 'HTTP_ACCEPT' => 'application/json' + end - get "/files" + it 'should return valid response' do + expect(last_response).to be_ok + expect(last_response.content_type).to include('application/json') + end - last_response.status.should eql 500 - end + it 'should contain no file list in response content' do + list = JSON.parse last_response.body + expect(list).to eq(EMPTY_FILE_LIST) + end + end + end - end - + context 'given a list of files' do + before do + allow(file_list).to receive(:all).and_return(%w(file1 file2)) + allow(file_list).to receive(:size).and_return(2) + allow(Ferver::FileList).to receive(:new).and_return(file_list) end - context "given an empty list of files" do + describe 'file list request' do - before { - file_list = mock() - file_list.stubs(:files).returns(EMPTY_FILE_LIST) - file_list.stubs(:file_count).returns(0) - Ferver::FileList.stubs(:new).returns(file_list) - } + context 'when no content-type is requested' do + before { get '/files' } - context "when no content-type is requested" do + it 'should return valid response' do + expect(last_response).to be_ok + end - before { get "/files" } + it 'should contain no file list in response content' do + expect(last_response.body).to have_tag('li', count: 2) + expect(last_response.body).to have_tag('p', text: /2 files/) + end - it "should return valid response" do - expect(last_response).to be_ok - #todo test html - end + it 'should list filenames' do + expect(last_response.body).to have_tag('li') do + with_tag('a', text: 'file1') + with_tag('a', text: 'file2') + end + end + end - it "should contain no file list in response content" do - expect(last_response.body).to have_selector("li", :count => 0) - expect(last_response.body).to contain(/0 files/) - end - + context 'when json content-type is requested' do + before do + get '/files', {}, 'HTTP_ACCEPT' => 'application/json' end - context "when json content-type is requested" do - - before { - get "/files", {}, {"HTTP_ACCEPT" => "application/json" } - } - - it "should return valid response" do - expect(last_response).to be_ok - expect(last_response.content_type).to include("application/json") - end - - it "should contain no file list in response content" do - list = JSON.parse last_response.body - expect(list).to eq(EMPTY_FILE_LIST) - end - + it 'should return valid response' do + expect(last_response).to be_ok + expect(last_response.content_type).to include('application/json') end + it 'should contain no file list in response content' do + list = JSON.parse last_response.body + expect(list.count).to eq(2) + expect(list).to match_array(%w(file1 file2)) + end + end end - context "given a list of files" do + describe 'downloading a file' do - before { - file_list = mock() - file_list.stubs(:files).returns(["file1", "file2"]) - file_list.stubs(:file_count).returns(2) - Ferver::FileList.stubs(:new).returns(file_list) - } - - context "when no content-type is requested" do - - before { get "/files" } - - it "should return valid response" do - expect(last_response).to be_ok - #todo test html - end - - it "should contain no file list in response content" do - expect(last_response.body).to have_selector("li", :count => 2) - expect(last_response.body).to contain(/2 files/) - end - - it "should list filenames" do - expect(last_response.body).to have_selector("li") do |node| - - expect(node.first).to have_selector("a", :content => "file1") - expect(node.last).to have_selector("a", :content => "file2") - - end - end - + context 'when requesting a file out of range' do + before do + allow(file_list).to receive(:file_id_is_valid?).with(3).and_return(false) + get '/files/3' end - context "when json content-type is requested" do - - before { - get "/files", {}, {"HTTP_ACCEPT" => "application/json" } - } - - it "should return valid response" do - expect(last_response).to be_ok - expect(last_response.content_type).to include("application/json") - end - - it "should contain no file list in response content" do - list = JSON.parse last_response.body - expect(list.count).to eq(2) - expect(list).to match_array(["file1", "file2"]) - end - + it 'should return not_found' do + expect(last_response).to be_not_found end + end - end + context 'when requesting invalid file id' do + before { get '/files/foo' } - - describe "downloading a file" do - - before { - @file_list = mock() - @file_list.stubs(:files).returns(["file1", "file2"]) - @file_list.stubs(:file_count).returns(2) - Ferver::FileList.stubs(:new).returns(@file_list) - } - - context "when requesting a file out of range" do - - before { - @file_list.expects(:file_id_is_valid?).with(3).returns(false) - get "/files/3" - } - - it "should return not_found" do - expect(last_response).to be_not_found - end - + it 'should return not_found' do + expect(last_response).to be_bad_request end + end - context "when requesting invalid file id" do + context 'when requesting a valid file id' do + before { get '/files/0' } - before { - @file_list.expects(:file_id_is_valid?).never - get "/files/foo" - } - - it "should return not_found" do - expect(last_response).to be_bad_request - end - + xit 'should return ok response' do + expect(last_response).to be_ok end - context "when requesting a valid file id" do - - before { get "/files/0" } - - xit "should return ok response" do - expect(last_response).to be_ok - end - - xit "should return octet-stream content-type" do - expect(last_response.headers['Content-Type']).to eq("application/octet-stream") - end - + xit 'should return octet-stream content-type' do + expect(last_response.headers['Content-Type']).to eq('application/octet-stream') end + end end - -end \ No newline at end of file + end +end