晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。 林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。 见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝) 既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。 南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。
| DIR:/opt/alt/ruby20/lib64/ruby/gems/2.0.0/gems/rack-1.6.4/test/ |
| Current File : //opt/alt/ruby20/lib64/ruby/gems/2.0.0/gems/rack-1.6.4/test/spec_mock.rb |
require 'yaml'
require 'rack/lint'
require 'rack/mock'
require 'stringio'
app = Rack::Lint.new(lambda { |env|
req = Rack::Request.new(env)
env["mock.postdata"] = env["rack.input"].read
if req.GET["error"]
env["rack.errors"].puts req.GET["error"]
env["rack.errors"].flush
end
body = req.head? ? "" : env.to_yaml
Rack::Response.new(body,
req.GET["status"] || 200,
"Content-Type" => "text/yaml").finish
})
describe Rack::MockRequest do
should "return a MockResponse" do
res = Rack::MockRequest.new(app).get("")
res.should.be.kind_of Rack::MockResponse
end
should "be able to only return the environment" do
env = Rack::MockRequest.env_for("")
env.should.be.kind_of Hash
env.should.include "rack.version"
end
should "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"
end
should "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
end
should "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"
res = Rack::MockRequest.new(app).post("", :input => "foo")
env = YAML.load(res.body)
env["REQUEST_METHOD"].should.equal "POST"
res = Rack::MockRequest.new(app).put("", :input => "foo")
env = YAML.load(res.body)
env["REQUEST_METHOD"].should.equal "PUT"
res = Rack::MockRequest.new(app).patch("", :input => "foo")
env = YAML.load(res.body)
env["REQUEST_METHOD"].should.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"
Rack::MockRequest.env_for("/", :method => "OPTIONS")["REQUEST_METHOD"].
should.equal "OPTIONS"
end
should "set content length" do
env = Rack::MockRequest.env_for("/", :input => "foo")
env["CONTENT_LENGTH"].should.equal "3"
end
should "allow posting" do
res = Rack::MockRequest.new(app).get("", :input => "foo")
env = YAML.load(res.body)
env["mock.postdata"].should.equal "foo"
res = Rack::MockRequest.new(app).post("", :input => StringIO.new("foo"))
env = YAML.load(res.body)
env["mock.postdata"].should.equal "foo"
end
should "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
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"
end
should "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
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"
end
should "prepend slash to uri path" do
res = Rack::MockRequest.new(app).
get("foo")
res.should.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"
end
should "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"
end
should "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 ""
end
should "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 ""
end
should "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"
end
should "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"
end
should "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"
# The gsub accounts for differences in YAMLs affect on the data.
env["mock.postdata"].gsub("\r", "").length.should.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)
end
should "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
Rack::MockRequest.new(capp).get('/', :lint => true)
called.should.equal true
end
end
describe Rack::MockResponse do
should "provide access to the HTTP status" do
res = Rack::MockRequest.new(app).get("")
res.should.be.successful
res.should.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 = Rack::MockRequest.new(app).get("/?status=501")
res.should.not.be.successful
res.should.be.server_error
res = Rack::MockRequest.new(app).get("/?status=307")
res.should.be.redirect
res = Rack::MockRequest.new(app).get("/?status=201", :lint => true)
res.should.be.empty
end
should "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
end
should "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/) }
end
should "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"
end
should "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'
end
should "optionally make Rack errors fatal" do
lambda {
Rack::MockRequest.new(app).get("/?error=foo", :fatal => true)
}.should.raise(Rack::MockRequest::FatalWarning)
end
end
|