kemal/spec/context_spec.cr

71 lines
2.1 KiB
Crystal
Raw Normal View History

2015-10-28 18:52:34 +00:00
require "./spec_helper"
describe "Context" do
it "has a default content type" do
2016-03-21 16:29:48 +00:00
get "/" do |env|
2016-01-24 15:44:37 +00:00
"Hello"
end
request = HTTP::Request.new("GET", "/")
2016-03-21 16:29:48 +00:00
client_response = call_request_on_app(request)
2016-01-24 15:44:37 +00:00
client_response.headers["Content-Type"].should eq("text/html")
end
2015-10-28 18:52:34 +00:00
it "sets content type" do
2016-03-21 16:29:48 +00:00
get "/" do |env|
2016-01-24 10:22:25 +00:00
env.response.content_type = "application/json"
"Hello"
2015-10-28 18:52:34 +00:00
end
request = HTTP::Request.new("GET", "/")
2016-03-21 16:29:48 +00:00
client_response = call_request_on_app(request)
2016-01-24 10:22:25 +00:00
client_response.headers["Content-Type"].should eq("application/json")
2015-10-28 18:52:34 +00:00
end
2015-10-30 15:06:25 +00:00
it "parses headers" do
2016-03-21 16:29:48 +00:00
get "/" do |env|
2016-01-24 10:22:25 +00:00
name = env.request.headers["name"]
2015-10-30 15:06:25 +00:00
"Hello #{name}"
end
headers = HTTP::Headers.new
2016-01-24 10:22:25 +00:00
headers["name"] = "kemal"
2015-10-30 15:06:25 +00:00
request = HTTP::Request.new("GET", "/", headers)
2016-03-21 16:29:48 +00:00
client_response = call_request_on_app(request)
2016-01-24 10:22:25 +00:00
client_response.body.should eq "Hello kemal"
2015-10-30 15:06:25 +00:00
end
2015-10-30 20:01:18 +00:00
it "sets response headers" do
2016-03-21 16:29:48 +00:00
get "/" do |env|
2016-01-24 10:22:25 +00:00
env.response.headers.add "Accept-Language", "tr"
2015-10-30 20:01:18 +00:00
end
request = HTTP::Request.new("GET", "/")
2016-03-21 16:29:48 +00:00
client_response = call_request_on_app(request)
2016-01-24 10:22:25 +00:00
client_response.headers["Accept-Language"].should eq "tr"
2015-10-30 20:01:18 +00:00
end
2016-07-14 18:56:01 +00:00
it "can store variables" do
before_get "/" do |env|
env.set "before_get", "Kemal"
env.set "before_get_int", 123
env.set "before_get_float", 3.5
end
get "/" do |env|
env.set "key", "value"
{
key: env.get("key"),
before_get: env.get("before_get"),
before_get_int: env.get("before_get_int"),
before_get_float: env.get("before_get_float")
}
end
request = HTTP::Request.new("GET", "/")
io = MemoryIO.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
Kemal::Middleware::Filter::INSTANCE.call(context)
Kemal::RouteHandler::INSTANCE.call(context)
context.store["key"].should eq "value"
context.store["before_get"].should eq "Kemal"
context.store["before_get_int"].should eq 123
context.store["before_get_float"].should eq 3.5
end
end