test/spec_mock.rb in rack-1.6.13 vs test/spec_mock.rb in rack-2.0.0.alpha

- old
+ new

@@ -1,5 +1,6 @@ +require 'minitest/autorun' require 'yaml' require 'rack/lint' require 'rack/mock' require 'stringio' @@ -17,281 +18,325 @@ req.GET["status"] || 200, "Content-Type" => "text/yaml").finish }) describe Rack::MockRequest do - should "return a MockResponse" do + it "return a MockResponse" do res = Rack::MockRequest.new(app).get("") - res.should.be.kind_of Rack::MockResponse + res.must_be_kind_of Rack::MockResponse end - should "be able to only return the environment" do + it "be able to only return the environment" do env = Rack::MockRequest.env_for("") - env.should.be.kind_of Hash - env.should.include "rack.version" + env.must_be_kind_of Hash + env.must_include "rack.version" end - should "return an environment with a path" do + it "return an environment with a path" do env = Rack::MockRequest.env_for("http://www.example.com/parse?location[]=1&location[]=2&age_group[]=2") - env["QUERY_STRING"].should.equal "location[]=1&location[]=2&age_group[]=2" - env["PATH_INFO"].should.equal "/parse" - env.should.be.kind_of Hash - env.should.include "rack.version" + env["QUERY_STRING"].must_equal "location[]=1&location[]=2&age_group[]=2" + env["PATH_INFO"].must_equal "/parse" + env.must_be_kind_of Hash + env.must_include "rack.version" end - should "provide sensible defaults" do + it "provide sensible defaults" do res = Rack::MockRequest.new(app).request env = YAML.load(res.body) - env["REQUEST_METHOD"].should.equal "GET" - env["SERVER_NAME"].should.equal "example.org" - env["SERVER_PORT"].should.equal "80" - env["QUERY_STRING"].should.equal "" - env["PATH_INFO"].should.equal "/" - env["SCRIPT_NAME"].should.equal "" - env["rack.url_scheme"].should.equal "http" - env["mock.postdata"].should.be.empty + env["REQUEST_METHOD"].must_equal "GET" + env["SERVER_NAME"].must_equal "example.org" + env["SERVER_PORT"].must_equal "80" + env["QUERY_STRING"].must_equal "" + env["PATH_INFO"].must_equal "/" + env["SCRIPT_NAME"].must_equal "" + env["rack.url_scheme"].must_equal "http" + env["mock.postdata"].must_be :empty? end - should "allow GET/POST/PUT/DELETE/HEAD" do + it "allow GET/POST/PUT/DELETE/HEAD" do res = Rack::MockRequest.new(app).get("", :input => "foo") env = YAML.load(res.body) - env["REQUEST_METHOD"].should.equal "GET" + env["REQUEST_METHOD"].must_equal "GET" res = Rack::MockRequest.new(app).post("", :input => "foo") env = YAML.load(res.body) - env["REQUEST_METHOD"].should.equal "POST" + env["REQUEST_METHOD"].must_equal "POST" res = Rack::MockRequest.new(app).put("", :input => "foo") env = YAML.load(res.body) - env["REQUEST_METHOD"].should.equal "PUT" + env["REQUEST_METHOD"].must_equal "PUT" res = Rack::MockRequest.new(app).patch("", :input => "foo") env = YAML.load(res.body) - env["REQUEST_METHOD"].should.equal "PATCH" + env["REQUEST_METHOD"].must_equal "PATCH" res = Rack::MockRequest.new(app).delete("", :input => "foo") env = YAML.load(res.body) - env["REQUEST_METHOD"].should.equal "DELETE" - - Rack::MockRequest.env_for("/", :method => "HEAD")["REQUEST_METHOD"]. - should.equal "HEAD" + env["REQUEST_METHOD"].must_equal "DELETE" - Rack::MockRequest.env_for("/", :method => "OPTIONS")["REQUEST_METHOD"]. - should.equal "OPTIONS" + Rack::MockRequest.env_for("/", :method => "HEAD")["REQUEST_METHOD"] + .must_equal "HEAD" + + Rack::MockRequest.env_for("/", :method => "OPTIONS")["REQUEST_METHOD"] + .must_equal "OPTIONS" end - should "set content length" do + it "set content length" do env = Rack::MockRequest.env_for("/", :input => "foo") - env["CONTENT_LENGTH"].should.equal "3" + env["CONTENT_LENGTH"].must_equal "3" end - should "allow posting" do + it "allow posting" do res = Rack::MockRequest.new(app).get("", :input => "foo") env = YAML.load(res.body) - env["mock.postdata"].should.equal "foo" + env["mock.postdata"].must_equal "foo" res = Rack::MockRequest.new(app).post("", :input => StringIO.new("foo")) env = YAML.load(res.body) - env["mock.postdata"].should.equal "foo" + env["mock.postdata"].must_equal "foo" end - should "use all parts of an URL" do + it "use all parts of an URL" do res = Rack::MockRequest.new(app). get("https://bla.example.org:9292/meh/foo?bar") - res.should.be.kind_of Rack::MockResponse + res.must_be_kind_of Rack::MockResponse env = YAML.load(res.body) - env["REQUEST_METHOD"].should.equal "GET" - env["SERVER_NAME"].should.equal "bla.example.org" - env["SERVER_PORT"].should.equal "9292" - env["QUERY_STRING"].should.equal "bar" - env["PATH_INFO"].should.equal "/meh/foo" - env["rack.url_scheme"].should.equal "https" + env["REQUEST_METHOD"].must_equal "GET" + env["SERVER_NAME"].must_equal "bla.example.org" + env["SERVER_PORT"].must_equal "9292" + env["QUERY_STRING"].must_equal "bar" + env["PATH_INFO"].must_equal "/meh/foo" + env["rack.url_scheme"].must_equal "https" end - should "set SSL port and HTTP flag on when using https" do + it "set SSL port and HTTP flag on when using https" do res = Rack::MockRequest.new(app). get("https://example.org/foo") - res.should.be.kind_of Rack::MockResponse + res.must_be_kind_of Rack::MockResponse env = YAML.load(res.body) - env["REQUEST_METHOD"].should.equal "GET" - env["SERVER_NAME"].should.equal "example.org" - env["SERVER_PORT"].should.equal "443" - env["QUERY_STRING"].should.equal "" - env["PATH_INFO"].should.equal "/foo" - env["rack.url_scheme"].should.equal "https" - env["HTTPS"].should.equal "on" + env["REQUEST_METHOD"].must_equal "GET" + env["SERVER_NAME"].must_equal "example.org" + env["SERVER_PORT"].must_equal "443" + env["QUERY_STRING"].must_equal "" + env["PATH_INFO"].must_equal "/foo" + env["rack.url_scheme"].must_equal "https" + env["HTTPS"].must_equal "on" end - should "prepend slash to uri path" do + it "prepend slash to uri path" do res = Rack::MockRequest.new(app). get("foo") - res.should.be.kind_of Rack::MockResponse + res.must_be_kind_of Rack::MockResponse env = YAML.load(res.body) - env["REQUEST_METHOD"].should.equal "GET" - env["SERVER_NAME"].should.equal "example.org" - env["SERVER_PORT"].should.equal "80" - env["QUERY_STRING"].should.equal "" - env["PATH_INFO"].should.equal "/foo" - env["rack.url_scheme"].should.equal "http" + env["REQUEST_METHOD"].must_equal "GET" + env["SERVER_NAME"].must_equal "example.org" + env["SERVER_PORT"].must_equal "80" + env["QUERY_STRING"].must_equal "" + env["PATH_INFO"].must_equal "/foo" + env["rack.url_scheme"].must_equal "http" end - should "properly convert method name to an uppercase string" do + it "properly convert method name to an uppercase string" do res = Rack::MockRequest.new(app).request(:get) env = YAML.load(res.body) - env["REQUEST_METHOD"].should.equal "GET" + env["REQUEST_METHOD"].must_equal "GET" end - should "accept params and build query string for GET requests" do + it "accept params and build query string for GET requests" do res = Rack::MockRequest.new(app).get("/foo?baz=2", :params => {:foo => {:bar => "1"}}) env = YAML.load(res.body) - env["REQUEST_METHOD"].should.equal "GET" - env["QUERY_STRING"].should.include "baz=2" - env["QUERY_STRING"].should.include "foo[bar]=1" - env["PATH_INFO"].should.equal "/foo" - env["mock.postdata"].should.equal "" + env["REQUEST_METHOD"].must_equal "GET" + env["QUERY_STRING"].must_include "baz=2" + env["QUERY_STRING"].must_include "foo[bar]=1" + env["PATH_INFO"].must_equal "/foo" + env["mock.postdata"].must_equal "" end - should "accept raw input in params for GET requests" do + it "accept raw input in params for GET requests" do res = Rack::MockRequest.new(app).get("/foo?baz=2", :params => "foo[bar]=1") env = YAML.load(res.body) - env["REQUEST_METHOD"].should.equal "GET" - env["QUERY_STRING"].should.include "baz=2" - env["QUERY_STRING"].should.include "foo[bar]=1" - env["PATH_INFO"].should.equal "/foo" - env["mock.postdata"].should.equal "" + env["REQUEST_METHOD"].must_equal "GET" + env["QUERY_STRING"].must_include "baz=2" + env["QUERY_STRING"].must_include "foo[bar]=1" + env["PATH_INFO"].must_equal "/foo" + env["mock.postdata"].must_equal "" end - should "accept params and build url encoded params for POST requests" do + it "accept params and build url encoded params for POST requests" do res = Rack::MockRequest.new(app).post("/foo", :params => {:foo => {:bar => "1"}}) env = YAML.load(res.body) - env["REQUEST_METHOD"].should.equal "POST" - env["QUERY_STRING"].should.equal "" - env["PATH_INFO"].should.equal "/foo" - env["CONTENT_TYPE"].should.equal "application/x-www-form-urlencoded" - env["mock.postdata"].should.equal "foo[bar]=1" + env["REQUEST_METHOD"].must_equal "POST" + env["QUERY_STRING"].must_equal "" + env["PATH_INFO"].must_equal "/foo" + env["CONTENT_TYPE"].must_equal "application/x-www-form-urlencoded" + env["mock.postdata"].must_equal "foo[bar]=1" end - should "accept raw input in params for POST requests" do + it "accept raw input in params for POST requests" do res = Rack::MockRequest.new(app).post("/foo", :params => "foo[bar]=1") env = YAML.load(res.body) - env["REQUEST_METHOD"].should.equal "POST" - env["QUERY_STRING"].should.equal "" - env["PATH_INFO"].should.equal "/foo" - env["CONTENT_TYPE"].should.equal "application/x-www-form-urlencoded" - env["mock.postdata"].should.equal "foo[bar]=1" + env["REQUEST_METHOD"].must_equal "POST" + env["QUERY_STRING"].must_equal "" + env["PATH_INFO"].must_equal "/foo" + env["CONTENT_TYPE"].must_equal "application/x-www-form-urlencoded" + env["mock.postdata"].must_equal "foo[bar]=1" end - should "accept params and build multipart encoded params for POST requests" do + it "accept params and build multipart encoded params for POST requests" do files = Rack::Multipart::UploadedFile.new(File.join(File.dirname(__FILE__), "multipart", "file1.txt")) res = Rack::MockRequest.new(app).post("/foo", :params => { "submit-name" => "Larry", "files" => files }) env = YAML.load(res.body) - env["REQUEST_METHOD"].should.equal "POST" - env["QUERY_STRING"].should.equal "" - env["PATH_INFO"].should.equal "/foo" - env["CONTENT_TYPE"].should.equal "multipart/form-data; boundary=AaB03x" + env["REQUEST_METHOD"].must_equal "POST" + env["QUERY_STRING"].must_equal "" + env["PATH_INFO"].must_equal "/foo" + env["CONTENT_TYPE"].must_equal "multipart/form-data; boundary=AaB03x" # The gsub accounts for differences in YAMLs affect on the data. - env["mock.postdata"].gsub("\r", "").length.should.equal 206 + env["mock.postdata"].gsub("\r", "").length.must_equal 206 end - should "behave valid according to the Rack spec" do - lambda { - Rack::MockRequest.new(app). - get("https://bla.example.org:9292/meh/foo?bar", :lint => true) - }.should.not.raise(Rack::Lint::LintError) + it "behave valid according to the Rack spec" do + url = "https://bla.example.org:9292/meh/foo?bar" + Rack::MockRequest.new(app).get(url, :lint => true). + must_be_kind_of Rack::MockResponse end - should "call close on the original body object" do + it "call close on the original body object" do called = false body = Rack::BodyProxy.new(['hi']) { called = true } capp = proc { |e| [200, {'Content-Type' => 'text/plain'}, body] } - called.should.equal false + called.must_equal false Rack::MockRequest.new(capp).get('/', :lint => true) - called.should.equal true + called.must_equal true end - - if "<3".respond_to? :b - should "defaults encoding to ASCII 8BIT" do - req = Rack::MockRequest.env_for("/foo") - - keys = [ - Rack::REQUEST_METHOD, - "SERVER_NAME", - "SERVER_PORT", - Rack::QUERY_STRING, - Rack::PATH_INFO, - "rack.url_scheme", - "HTTPS" - ] - - keys.each do |k| - req[k].encoding.should.equal Encoding::ASCII_8BIT - end - end - end end describe Rack::MockResponse do - should "provide access to the HTTP status" do + it "provide access to the HTTP status" do res = Rack::MockRequest.new(app).get("") - res.should.be.successful - res.should.be.ok + res.must_be :successful? + res.must_be :ok? res = Rack::MockRequest.new(app).get("/?status=404") - res.should.not.be.successful - res.should.be.client_error - res.should.be.not_found + res.wont_be :successful? + res.must_be :client_error? + res.must_be :not_found? res = Rack::MockRequest.new(app).get("/?status=501") - res.should.not.be.successful - res.should.be.server_error + res.wont_be :successful? + res.must_be :server_error? res = Rack::MockRequest.new(app).get("/?status=307") - res.should.be.redirect + res.must_be :redirect? res = Rack::MockRequest.new(app).get("/?status=201", :lint => true) - res.should.be.empty + res.must_be :empty? end - should "provide access to the HTTP headers" do + it "provide access to the HTTP headers" do res = Rack::MockRequest.new(app).get("") - res.should.include "Content-Type" - res.headers["Content-Type"].should.equal "text/yaml" - res.original_headers["Content-Type"].should.equal "text/yaml" - res["Content-Type"].should.equal "text/yaml" - res.content_type.should.equal "text/yaml" - res.content_length.should.not.equal 0 - res.location.should.be.nil + res.must_include "Content-Type" + res.headers["Content-Type"].must_equal "text/yaml" + res.original_headers["Content-Type"].must_equal "text/yaml" + res["Content-Type"].must_equal "text/yaml" + res.content_type.must_equal "text/yaml" + res.content_length.wont_equal 0 + res.location.must_be_nil end - should "provide access to the HTTP body" do + it "provide access to the HTTP body" do res = Rack::MockRequest.new(app).get("") - res.body.should =~ /rack/ - res.should =~ /rack/ - res.should.match(/rack/) - res.should.satisfy { |r| r.match(/rack/) } + res.body.must_match(/rack/) + assert_match(res, /rack/) end - should "provide access to the Rack errors" do + it "provide access to the Rack errors" do res = Rack::MockRequest.new(app).get("/?error=foo", :lint => true) - res.should.be.ok - res.errors.should.not.be.empty - res.errors.should.include "foo" + res.must_be :ok? + res.errors.wont_be :empty? + res.errors.must_include "foo" end - should "allow calling body.close afterwards" do + it "allow calling body.close afterwards" do # this is exactly what rack-test does body = StringIO.new("hi") res = Rack::MockResponse.new(200, {}, body) body.close if body.respond_to?(:close) - res.body.should == 'hi' + res.body.must_equal 'hi' end - should "optionally make Rack errors fatal" do + it "optionally make Rack errors fatal" do lambda { Rack::MockRequest.new(app).get("/?error=foo", :fatal => true) - }.should.raise(Rack::MockRequest::FatalWarning) + }.must_raise Rack::MockRequest::FatalWarning + end +end + +describe Rack::MockResponse, 'headers' do + before do + @res = Rack::MockRequest.new(app).get('') + @res.set_header 'FOO', '1' + end + + it 'has_header?' do + lambda { @res.has_header? nil }.must_raise NoMethodError + + @res.has_header?('FOO').must_equal true + @res.has_header?('Foo').must_equal true + end + + it 'get_header' do + lambda { @res.get_header nil }.must_raise NoMethodError + + @res.get_header('FOO').must_equal '1' + @res.get_header('Foo').must_equal '1' + end + + it 'set_header' do + lambda { @res.set_header nil, '1' }.must_raise NoMethodError + + @res.set_header('FOO', '2').must_equal '2' + @res.get_header('FOO').must_equal '2' + + @res.set_header('Foo', '3').must_equal '3' + @res.get_header('Foo').must_equal '3' + @res.get_header('FOO').must_equal '3' + + @res.set_header('FOO', nil).must_be_nil + @res.get_header('FOO').must_be_nil + @res.has_header?('FOO').must_equal true + end + + it 'add_header' do + lambda { @res.add_header nil, '1' }.must_raise NoMethodError + + # Sets header on first addition + @res.add_header('FOO', '1').must_equal '1,1' + @res.get_header('FOO').must_equal '1,1' + + # Ignores nil additions + @res.add_header('FOO', nil).must_equal '1,1' + @res.get_header('FOO').must_equal '1,1' + + # Converts additions to strings + @res.add_header('FOO', 2).must_equal '1,1,2' + @res.get_header('FOO').must_equal '1,1,2' + + # Respects underlying case-sensitivity + @res.add_header('Foo', 'yep').must_equal '1,1,2,yep' + @res.get_header('Foo').must_equal '1,1,2,yep' + @res.get_header('FOO').must_equal '1,1,2,yep' + end + + it 'delete_header' do + lambda { @res.delete_header nil }.must_raise NoMethodError + + @res.delete_header('FOO').must_equal '1' + @res.has_header?('FOO').must_equal false + + @res.has_header?('Foo').must_equal false + @res.delete_header('Foo').must_be_nil end end