2015-10-28 18:52:34 +00:00
|
|
|
require "./spec_helper"
|
|
|
|
|
|
|
|
describe "Context" do
|
2016-01-24 16:13:04 +00:00
|
|
|
it "has a default content type" do
|
2016-01-24 15:44:37 +00:00
|
|
|
kemal = Kemal::Handler.new
|
|
|
|
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::Handler.new
|
|
|
|
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::Handler.new
|
|
|
|
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::Handler.new
|
|
|
|
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
|