shard-kemal/spec/context_spec.cr

72 lines
2.2 KiB
Crystal
Raw Normal View History

2015-10-28 18:52:34 +00:00
require "./spec_helper"
describe "Context" do
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|
t = TestContextStorageType.new
t.id = 32
a = AnotherContextStorageType.new
2017-03-25 17:20:10 +00:00
env.set "key", "value"
2016-07-14 18:56:01 +00:00
env.set "before_get", "Kemal"
env.set "before_get_int", 123
env.set "before_get_context_test", t
env.set "another_context_test", a
2016-07-14 18:56:01 +00:00
env.set "before_get_float", 3.5
end
get "/" do |env|
env.set "key", "value"
{
2017-05-08 14:33:19 +00:00
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"),
before_get_context_test: env.get("before_get_context_test"),
2016-07-14 18:56:01 +00:00
}
end
2016-07-14 18:56:01 +00:00
request = HTTP::Request.new("GET", "/")
io = IO::Memory.new
2016-07-14 18:56:01 +00:00
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
2016-12-03 22:43:30 +00:00
Kemal::FilterHandler::INSTANCE.call(context)
2016-07-14 18:56:01 +00:00
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
context.store["before_get_context_test"].as(TestContextStorageType).id.should eq 32
2016-07-14 18:56:01 +00:00
end
2016-07-17 11:31:45 +00:00
end