Crystal 0.25.0 (#452)

This commit is contained in:
Serdar Dogruyol 2018-06-16 18:03:00 +03:00 committed by GitHub
parent c2236acf3a
commit a5870e7d24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 23 additions and 22 deletions

View file

@ -174,7 +174,7 @@ describe "ParamParser" do
body_params.to_s.should eq("") body_params.to_s.should eq("")
json_params = Kemal::ParamParser.new(request).json json_params = Kemal::ParamParser.new(request).json
json_params.should eq({} of String => Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type)) json_params.should eq({} of String => Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Any) | Array(JSON::Any))
end end
end end

View file

@ -85,7 +85,6 @@ describe "Kemal::RouteHandler" do
post "/" do |env| post "/" do |env|
skills = env.params.json["skills"].as(Array) skills = env.params.json["skills"].as(Array)
skills_from_languages = skills.map do |skill| skills_from_languages = skills.map do |skill|
skill = skill.as(Hash)
skill["language"] skill["language"]
end end
"Skills #{skills_from_languages.each.join(',')}" "Skills #{skills_from_languages.each.join(',')}"

View file

@ -131,7 +131,7 @@ describe Kemal::StaticFileHandler do
end end
it "should handle setting custom headers" do it "should handle setting custom headers" do
headers = Proc(HTTP::Server::Response, String, File::Stat, Void).new do |response, path, stat| headers = Proc(HTTP::Server::Response, String, File::Info, Void).new do |response, path, stat|
if path =~ /\.html$/ if path =~ /\.html$/
response.headers.add("Access-Control-Allow-Origin", "*") response.headers.add("Access-Control-Allow-Origin", "*")
end end
@ -143,7 +143,7 @@ describe Kemal::StaticFileHandler do
response = handle HTTP::Request.new("GET", "/dir/test.txt") response = handle HTTP::Request.new("GET", "/dir/test.txt")
response.headers.has_key?("Access-Control-Allow-Origin").should be_false response.headers.has_key?("Access-Control-Allow-Origin").should be_false
response.headers["Content-Size"].should eq( response.headers["Content-Size"].should eq(
File.stat("#{__DIR__}/static/dir/test.txt").size.to_s File.info("#{__DIR__}/static/dir/test.txt").size.to_s
) )
response = handle HTTP::Request.new("GET", "/dir/index.html") response = handle HTTP::Request.new("GET", "/dir/index.html")

View file

@ -28,11 +28,12 @@ describe "Kemal::WebSocketHandler" do
"Upgrade" => "websocket", "Upgrade" => "websocket",
"Connection" => "Upgrade", "Connection" => "Upgrade",
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==", "Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
"Sec-WebSocket-Version" => "13",
} }
request = HTTP::Request.new("GET", "/", headers) request = HTTP::Request.new("GET", "/", headers)
io_with_context = create_ws_request_and_return_io(handler, request) io_with_context = create_ws_request_and_return_io(handler, request)
io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-Websocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n\x81\u0005Match") io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n\x81\u0005Match")
end end
it "fetches named url parameters" do it "fetches named url parameters" do
@ -42,10 +43,11 @@ describe "Kemal::WebSocketHandler" do
"Upgrade" => "websocket", "Upgrade" => "websocket",
"Connection" => "Upgrade", "Connection" => "Upgrade",
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==", "Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
"Sec-WebSocket-Version" => "13",
} }
request = HTTP::Request.new("GET", "/1234", headers) request = HTTP::Request.new("GET", "/1234", headers)
io_with_context = create_ws_request_and_return_io(handler, request) io_with_context = create_ws_request_and_return_io(handler, request)
io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-Websocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n") io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n")
end end
it "matches correct verb" do it "matches correct verb" do

View file

@ -59,7 +59,7 @@ module Kemal
end end
end end
config.server ||= HTTP::Server.new(config.host_binding, config.port, config.handlers) server = config.server ||= HTTP::Server.new(config.handlers)
{% if !flag?(:without_openssl) %} {% if !flag?(:without_openssl) %}
config.server.not_nil!.tls = config.ssl config.server.not_nil!.tls = config.ssl
@ -68,13 +68,13 @@ module Kemal
config.running = true config.running = true
yield config yield config
config.server.not_nil!.listen if config.env != "test" && config.server server.listen(config.host_binding, config.port) if config.env != "test"
end end
def self.stop def self.stop
if config.running if config.running
if config.server if server = config.server
config.server.not_nil!.close server.close unless server.closed?
config.running = false config.running = false
else else
raise "Kemal.config.server is not set. Please use Kemal.run to set the server." raise "Kemal.config.server is not set. Please use Kemal.run to set the server."

View file

@ -21,7 +21,7 @@ module Kemal
property host_binding, ssl, port, env, public_folder, logging, running property host_binding, ssl, port, env, public_folder, logging, running
property always_rescue, server : HTTP::Server?, extra_options, shutdown_message property always_rescue, server : HTTP::Server?, extra_options, shutdown_message
property serve_static : (Bool | Hash(String, Bool)) property serve_static : (Bool | Hash(String, Bool))
property static_headers : (HTTP::Server::Response, String, File::Stat -> Void)? property static_headers : (HTTP::Server::Response, String, File::Info -> Void)?
property powered_by_header : Bool = true property powered_by_header : Bool = true
def initialize def initialize

View file

@ -119,7 +119,7 @@ def send_file(env : HTTP::Server::Context, path : String, mime_type : String? =
minsize = 860 # http://webmasters.stackexchange.com/questions/31750/what-is-recommended-minimum-object-size-for-gzip-performance-benefits ?? minsize = 860 # http://webmasters.stackexchange.com/questions/31750/what-is-recommended-minimum-object-size-for-gzip-performance-benefits ??
request_headers = env.request.headers request_headers = env.request.headers
filesize = File.size(file_path) filesize = File.size(file_path)
filestat = File.stat(file_path) filestat = File.info(file_path)
Kemal.config.static_headers.try(&.call(env.response, file_path, filestat)) Kemal.config.static_headers.try(&.call(env.response, file_path, filestat))
@ -234,6 +234,6 @@ end
# response.headers.add("Content-Size", filestat.size.to_s) # response.headers.add("Content-Size", filestat.size.to_s)
# end # end
# ``` # ```
def static_headers(&headers : HTTP::Server::Response, String, File::Stat -> Void) def static_headers(&headers : HTTP::Server::Response, String, File::Info -> Void)
Kemal.config.static_headers = headers Kemal.config.static_headers = headers
end end

View file

@ -8,7 +8,7 @@ module Kemal
MULTIPART_FORM = "multipart/form-data" MULTIPART_FORM = "multipart/form-data"
PARTS = %w(url query body json) PARTS = %w(url query body json)
# :nodoc: # :nodoc:
alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type) alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Any) | Array(JSON::Any)
getter files getter files
def initialize(@request : HTTP::Request) def initialize(@request : HTTP::Request)
@ -89,7 +89,7 @@ module Kemal
case json = JSON.parse(body).raw case json = JSON.parse(body).raw
when Hash when Hash
json.each do |key, value| json.each do |key, value|
@json[key] = value.as(AllParamTypes) @json[key] = value.raw
end end
when Array when Array
@json["_json"] = json @json["_json"] = json

View file

@ -58,7 +58,7 @@ module Kemal
end end
private def etag(context : HTTP::Server::Context, file_path : String) private def etag(context : HTTP::Server::Context, file_path : String)
etag = %{W/"#{File.lstat(file_path).mtime.epoch.to_s}"} etag = %{W/"#{File.info(file_path).modification_time.epoch.to_s}"}
context.response.headers["ETag"] = etag context.response.headers["ETag"] = etag
return false if !context.request.headers["If-None-Match"]? || context.request.headers["If-None-Match"] != etag return false if !context.request.headers["If-None-Match"]? || context.request.headers["If-None-Match"] != etag
context.response.headers.delete "Content-Type" context.response.headers.delete "Content-Type"