shard-kemal/spec/context_spec.cr

52 lines
1.8 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
kemal = Kemal::RouteHandler::INSTANCE
2016-01-24 15:44:37 +00:00
kemal.add_route "GET", "/" do |env|
"Hello"
end
request = HTTP::Request.new("GET", "/")
io_with_context = create_request_and_return_io(kemal, request)
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.headers["Content-Type"].should eq("text/html")
end
2015-10-28 18:52:34 +00:00
it "sets content type" do
kemal = Kemal::RouteHandler::INSTANCE
2015-10-28 18:52:34 +00:00
kemal.add_route "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-01-24 10:22:25 +00:00
io_with_context = create_request_and_return_io(kemal, request)
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
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
kemal = Kemal::RouteHandler::INSTANCE
2015-10-30 15:06:25 +00:00
kemal.add_route "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-01-24 10:22:25 +00:00
io_with_context = create_request_and_return_io(kemal, request)
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
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
kemal = Kemal::RouteHandler::INSTANCE
2015-10-30 20:01:18 +00:00
kemal.add_route "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-01-24 10:22:25 +00:00
io_with_context = create_request_and_return_io(kemal, request)
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.headers["Accept-Language"].should eq "tr"
2015-10-30 20:01:18 +00:00
end
2015-10-28 18:52:34 +00:00
end