1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
require 'yaml'
require 'rack/mock'
require 'rack/request'
require 'rack/response'
app = 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
Rack::Response.new(env.to_yaml,
req.GET["status"] || 200,
"Content-Type" => "text/yaml").finish
}
context "Rack::MockRequest" do
specify "should return a MockResponse" do
res = Rack::MockRequest.new(app).get("")
res.should.be.kind_of Rack::MockResponse
end
specify "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
specify "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
specify "should allow GET/POST/PUT/DELETE" 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).delete("", :input => "foo")
env = YAML.load(res.body)
env["REQUEST_METHOD"].should.equal "DELETE"
Rack::MockRequest.env_for("/", :method => "OPTIONS")["REQUEST_METHOD"].
should.equal "OPTIONS"
end
specify "should set content length" do
env = Rack::MockRequest.env_for("/", :input => "foo")
env["CONTENT_LENGTH"].should.equal "3"
end
specify "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
specify "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
specify "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
specify "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
specify "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
specify "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.match "baz=2"
env["QUERY_STRING"].should.match "foo[bar]=1"
env["PATH_INFO"].should.equal "/foo"
env["mock.postdata"].should.equal ""
end
specify "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.match "baz=2"
env["QUERY_STRING"].should.match "foo[bar]=1"
env["PATH_INFO"].should.equal "/foo"
env["mock.postdata"].should.equal ""
end
specify "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
specify "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
specify "should accept params and build multipart encoded params for POST requests" do
files = Rack::Utils::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["mock.postdata"].length.should.equal 206
end
specify "should behave valid according to the Rack spec" do
lambda {
res = Rack::MockRequest.new(app).
get("https://bla.example.org:9292/meh/foo?bar", :lint => true)
}.should.not.raise(Rack::Lint::LintError)
end
end
context "Rack::MockResponse" do
specify "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
specify "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.be 414 # needs change often.
res.location.should.be.nil
end
specify "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
specify "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
specify "should optionally make Rack errors fatal" do
lambda {
Rack::MockRequest.new(app).get("/?error=foo", :fatal => true)
}.should.raise(Rack::MockRequest::FatalWarning)
end
end
|