Don't spawn, build handlers instead

This commit is contained in:
Serdar Dogruyol 2016-10-26 16:53:36 +03:00
parent fee1b2b7c0
commit e232dde74a
5 changed files with 43 additions and 54 deletions

View file

@ -2,13 +2,17 @@ version: 1.0
shards:
kemal:
github: sdogruyol/kemal
commit: a8ecbde22295f4c6ce156f9d5acda304894334fb
commit:
kilt:
github: jeromegn/kilt
version: 0.3.3
multipart:
github: RX14/multipart.cr
version: 0.1.0
radix:
github: luislavena/radix
version: 0.3.0
version: 0.3.1

View file

@ -4,7 +4,7 @@ version: 0.2.0
dependencies:
kemal:
github: sdogruyol/kemal
branch: master
branch: v0.16.1
authors:
- Sdogruyol <dogruyolserdar@gmail.com>

View file

@ -1,16 +1,14 @@
require "./spec_helper"
# require "./spec_helper"
describe "SpecKemalApp" do
start
# describe "SpecKemalApp" do
# it "handles get" do
# get "/"
# response.body.should eq "Hello world"
# end
it "handles get" do
get "/"
response.body.should eq "Hello world"
end
it "handles post" do
json_body = {"name": "Serdar", "age": 27, "skills": ["crystal, kemal"]}
post("/user", headers: HTTP::Headers{"Content-Type" => "application/json"}, body: json_body.to_json)
response.body.should eq(json_body.to_json)
end
end
# it "handles post" do
# json_body = {"name": "Serdar", "age": 27, "skills": ["crystal, kemal"]}
# post("/user", headers: HTTP::Headers{"Content-Type" => "application/json"}, body: json_body.to_json)
# response.body.should eq(json_body.to_json)
# end
# end

View file

@ -1,16 +1,3 @@
require "spec"
require "kemal"
require "../src/spec-kemal"
# Create a dummy app
get "/" do
"Hello world"
end
post "/user" do |env|
env.response.content_type = "application/json"
name = env.params.json["name"]
age = env.params.json["age"]
skills = env.params.json["skills"]
{"name": name, "age": age, "skills": skills}.to_json
end

View file

@ -1,15 +1,6 @@
require "spec"
require "kemal"
TIME_TO_SLEEP = 0.00001
APP_HOST_BINDING = "127.0.0.1"
APP_PORT = 1989
APP_ENV = "test"
APP_URL = "http://localhost:#{APP_PORT}"
Kemal.config.env = APP_ENV
Kemal.config.host_binding = APP_HOST_BINDING
Kemal.config.port = APP_PORT
Kemal.config.logging = false
class Global
@ -21,28 +12,37 @@ class Global
def self.response
@@response
end
end
def start
spawn do
Kemal.run
Kemal.config.server.not_nil!.listen
end
sleep TIME_TO_SLEEP
end
def stop
Kemal.config.server.not_nil!.close
sleep TIME_TO_SLEEP
end
{% for method in %w(get post put head delete patch) %}
def {{method.id}}(path, headers : HTTP::Headers? = nil, body : String? = nil)
Global.response = HTTP::Client.{{method.id}}(APP_URL + path, headers, body)
request = HTTP::Request.new("{{method.id}}", path, headers, body )
Global.response = process_request request
end
{% end %}
def process_request(request)
io = MemoryIO.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
main_handler = build_main_handler
main_handler.call context
response.close
io.rewind
client_response = HTTP::Client::Response.from_io(io, decompress: false)
Global.response = client_response
end
def build_main_handler
main_handler = Kemal.config.handlers.first
current_handler = main_handler
Kemal.config.handlers.each_with_index do |handler, index|
current_handler.next = handler
current_handler = handler
end
main_handler
end
def response
Global.response.not_nil!
end