Crystal 0.25.0 (#452)
This commit is contained in:
parent
c2236acf3a
commit
a5870e7d24
9 changed files with 23 additions and 22 deletions
|
@ -174,7 +174,7 @@ describe "ParamParser" do
|
|||
body_params.to_s.should eq("")
|
||||
|
||||
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
|
||||
|
||||
|
|
|
@ -85,7 +85,6 @@ describe "Kemal::RouteHandler" do
|
|||
post "/" do |env|
|
||||
skills = env.params.json["skills"].as(Array)
|
||||
skills_from_languages = skills.map do |skill|
|
||||
skill = skill.as(Hash)
|
||||
skill["language"]
|
||||
end
|
||||
"Skills #{skills_from_languages.each.join(',')}"
|
||||
|
|
|
@ -131,7 +131,7 @@ describe Kemal::StaticFileHandler do
|
|||
end
|
||||
|
||||
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$/
|
||||
response.headers.add("Access-Control-Allow-Origin", "*")
|
||||
end
|
||||
|
@ -143,7 +143,7 @@ describe Kemal::StaticFileHandler do
|
|||
response = handle HTTP::Request.new("GET", "/dir/test.txt")
|
||||
response.headers.has_key?("Access-Control-Allow-Origin").should be_false
|
||||
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")
|
||||
|
|
|
@ -25,27 +25,29 @@ describe "Kemal::WebSocketHandler" do
|
|||
ws "/" { |socket| socket.send("Match") }
|
||||
ws "/no_match" { |socket| socket.send "No Match" }
|
||||
headers = HTTP::Headers{
|
||||
"Upgrade" => "websocket",
|
||||
"Connection" => "Upgrade",
|
||||
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
|
||||
"Upgrade" => "websocket",
|
||||
"Connection" => "Upgrade",
|
||||
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
|
||||
"Sec-WebSocket-Version" => "13",
|
||||
}
|
||||
request = HTTP::Request.new("GET", "/", headers)
|
||||
|
||||
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
|
||||
|
||||
it "fetches named url parameters" do
|
||||
handler = Kemal::WebSocketHandler::INSTANCE
|
||||
ws "/:id" { |_, c| c.params.url["id"] }
|
||||
headers = HTTP::Headers{
|
||||
"Upgrade" => "websocket",
|
||||
"Connection" => "Upgrade",
|
||||
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
|
||||
"Upgrade" => "websocket",
|
||||
"Connection" => "Upgrade",
|
||||
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
|
||||
"Sec-WebSocket-Version" => "13",
|
||||
}
|
||||
request = HTTP::Request.new("GET", "/1234", headers)
|
||||
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
|
||||
|
||||
it "matches correct verb" do
|
||||
|
|
|
@ -59,7 +59,7 @@ module Kemal
|
|||
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) %}
|
||||
config.server.not_nil!.tls = config.ssl
|
||||
|
@ -68,13 +68,13 @@ module Kemal
|
|||
config.running = true
|
||||
|
||||
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
|
||||
|
||||
def self.stop
|
||||
if config.running
|
||||
if config.server
|
||||
config.server.not_nil!.close
|
||||
if server = config.server
|
||||
server.close unless server.closed?
|
||||
config.running = false
|
||||
else
|
||||
raise "Kemal.config.server is not set. Please use Kemal.run to set the server."
|
||||
|
|
|
@ -21,7 +21,7 @@ module Kemal
|
|||
property host_binding, ssl, port, env, public_folder, logging, running
|
||||
property always_rescue, server : HTTP::Server?, extra_options, shutdown_message
|
||||
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
|
||||
|
||||
def initialize
|
||||
|
|
|
@ -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 ??
|
||||
request_headers = env.request.headers
|
||||
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))
|
||||
|
||||
|
@ -234,6 +234,6 @@ end
|
|||
# response.headers.add("Content-Size", filestat.size.to_s)
|
||||
# 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
|
||||
end
|
||||
|
|
|
@ -8,7 +8,7 @@ module Kemal
|
|||
MULTIPART_FORM = "multipart/form-data"
|
||||
PARTS = %w(url query body json)
|
||||
# :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
|
||||
|
||||
def initialize(@request : HTTP::Request)
|
||||
|
@ -89,7 +89,7 @@ module Kemal
|
|||
case json = JSON.parse(body).raw
|
||||
when Hash
|
||||
json.each do |key, value|
|
||||
@json[key] = value.as(AllParamTypes)
|
||||
@json[key] = value.raw
|
||||
end
|
||||
when Array
|
||||
@json["_json"] = json
|
||||
|
|
|
@ -58,7 +58,7 @@ module Kemal
|
|||
end
|
||||
|
||||
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
|
||||
return false if !context.request.headers["If-None-Match"]? || context.request.headers["If-None-Match"] != etag
|
||||
context.response.headers.delete "Content-Type"
|
||||
|
|
Loading…
Reference in a new issue