require_relative "./helpers" describe BigBench::Fragment do before(:each) do @benchmark = BigBench::Benchmark::Benchmark.new("a test benchmark", "http://localhost:3001", {}){} @benchmark.start = Time.now end it "should parse a single fragment" do fragments = BigBench::Fragment.parse(@benchmark){ get "/" } fragments.size.should == 1 fragments.first.uri.to_s.should == "http://localhost:3001/" end it "should parse multiple fragments" do fragments = BigBench::Fragment.parse(@benchmark) do get "/index" get "/page" end fragments.size.should == 2 fragments.first.uri.to_s.should == "http://localhost:3001/index" fragments.last.uri.to_s.should == "http://localhost:3001/page" end context "should support following fragment methods" do it "GET" do fragments = BigBench::Fragment.parse(@benchmark){ get "/" } fragments.size.should == 1 fragments.first.uri.to_s.should == "http://localhost:3001/" fragments.first.method.should == :get fragments.first.options.should == {} end it "POST" do fragments = BigBench::Fragment.parse(@benchmark){ post "/login", { :user => "tommy@test.com", :password => "secret" } } fragments.size.should == 1 fragments.first.uri.to_s.should == "http://localhost:3001/login" fragments.first.method.should == :post fragments.first.options.should == { :user => "tommy@test.com", :password => "secret" } end it "PUT" do fragments = BigBench::Fragment.parse(@benchmark){ put "/books", { :book => "A Book" } } fragments.size.should == 1 fragments.first.uri.to_s.should == "http://localhost:3001/books" fragments.first.method.should == :put fragments.first.options.should == { :book => "A Book" } end it "DELETE" do fragments = BigBench::Fragment.parse(@benchmark){ delete "/books/5", { :user => "tommy@test.com", :password => "secret" } } fragments.size.should == 1 fragments.first.uri.to_s.should == "http://localhost:3001/books/5" fragments.first.method.should == :delete fragments.first.options.should == { :user => "tommy@test.com", :password => "secret" } end end context "should run following fragment methods" do it "GET" do fragment = BigBench::Fragment.parse(@benchmark){ get "/" }.first result = nil EventMachine.run { |http| start = Time.now http = fragment.run! http.callback { fragment.track!(start, Time.now, http) http.response_header.status.should == 200 http.response.should match(/Test/) EventMachine.stop } http.errback { puts "Error"; EventMachine.stop } } @benchmark.tracker.trackings.size.should == 1 end it "POST" do fragment = BigBench::Fragment.parse(@benchmark){ post "/" }.first result = nil EventMachine.run { |http| start = Time.now http = fragment.run! http.callback { fragment.track!(start, Time.now, http) http.response_header.status.should == 200 http.response.should match(/Test/) EventMachine.stop } http.errback { puts "Error"; EventMachine.stop } } @benchmark.tracker.trackings.size.should == 1 end it "PUT" do fragment = BigBench::Fragment.parse(@benchmark){ put "/" }.first result = nil EventMachine.run { |http| start = Time.now http = fragment.run! http.callback { fragment.track!(start, Time.now, http) http.response_header.status.should == 200 http.response.should match(/Test/) EventMachine.stop } http.errback { puts "Error"; EventMachine.stop } } @benchmark.tracker.trackings.size.should == 1 end it "DELETE" do fragment = BigBench::Fragment.parse(@benchmark){ delete "/" }.first result = nil EventMachine.run { |http| start = Time.now http = fragment.run! http.callback { fragment.track!(start, Time.now, http) http.response_header.status.should == 200 http.response.should match(/Test/) EventMachine.stop } http.errback { puts "Error"; EventMachine.stop } } @benchmark.tracker.trackings.size.should == 1 end end context "should run basic auth fragments" do it "and track a 401 if no authorization was provided" do fragment = BigBench::Fragment.parse(@benchmark){ get "/basic/auth" }.first result = nil EventMachine.run { |http| start = Time.now http = fragment.run! http.callback { fragment.track!(start, Time.now, http) http.response_header.status.should == 401 EventMachine.stop } http.errback { puts "Error"; EventMachine.stop } } @benchmark.tracker.trackings.size.should == 1 @benchmark.tracker.trackings.first[:status] == 401 end it "and work with fragment authorization" do fragment = BigBench::Fragment.parse(@benchmark){ get "/basic/auth", :basic_auth => ['admin', 'secret'] }.first result = nil EventMachine.run { |http| start = Time.now http = fragment.run! http.callback { fragment.track!(start, Time.now, http) http.response_header.status.should == 200 http.response.should match(/Test/) EventMachine.stop } http.errback { puts "Error"; EventMachine.stop } } @benchmark.tracker.trackings.size.should == 1 @benchmark.tracker.trackings.first[:status] == 200 end it "and work with config authorization" do BigBench.config.basic_auth = ['admin', 'secret'] fragment = BigBench::Fragment.parse(@benchmark){ get "/basic/auth" }.first result = nil EventMachine.run { |http| start = Time.now http = fragment.run! http.callback { fragment.track!(start, Time.now, http) http.response_header.status.should == 200 http.response.should match(/Test/) EventMachine.stop } http.errback { puts "Error"; EventMachine.stop } } @benchmark.tracker.trackings.size.should == 1 @benchmark.tracker.trackings.first[:status] == 200 end end context "should send params in body", :params do it "and track a 406 if no params were provided" do fragment = BigBench::Fragment.parse(@benchmark){ post "/post/content" }.first result = nil EventMachine.run { |http| start = Time.now http = fragment.run! http.callback { fragment.track!(start, Time.now, http) http.response_header.status.should == 406 EventMachine.stop } http.errback { puts "Error"; EventMachine.stop } } @benchmark.tracker.trackings.size.should == 1 @benchmark.tracker.trackings.first[:status] == 406 end it "and work with params supplied" do fragment = BigBench::Fragment.parse(@benchmark){ post "/post/content", :params => { :name => "bigbench", :id => 1 } }.first result = nil EventMachine.run { |http| start = Time.now http = fragment.run! http.callback { fragment.track!(start, Time.now, http) http.response_header.status.should == 200 http.response.should match(/Test/) EventMachine.stop } http.errback { puts "Error"; EventMachine.stop } } @benchmark.tracker.trackings.size.should == 1 @benchmark.tracker.trackings.first[:status] == 200 end it "and sample a hash of params randomly if an array was specified" do fragment = BigBench::Fragment.parse(@benchmark){ post "/post/content", :params => [ { :name => "bigbench", :id => 1 }, { :name => "bigbench2", :id => 2 } ] }.first result = nil EventMachine.run { |http| start = Time.now http = fragment.run! http.callback { fragment.track!(start, Time.now, http) http.response_header.status.should == 200 http.response.should match(/Test/) EventMachine.stop } http.errback { puts "Error"; EventMachine.stop } } @benchmark.tracker.trackings.size.should == 1 @benchmark.tracker.trackings.first[:status] == 200 end end end