Don't spawn, build handlers instead
This commit is contained in:
parent
fee1b2b7c0
commit
e232dde74a
5 changed files with 43 additions and 54 deletions
|
@ -2,13 +2,17 @@ version: 1.0
|
||||||
shards:
|
shards:
|
||||||
kemal:
|
kemal:
|
||||||
github: sdogruyol/kemal
|
github: sdogruyol/kemal
|
||||||
commit: a8ecbde22295f4c6ce156f9d5acda304894334fb
|
commit:
|
||||||
|
|
||||||
kilt:
|
kilt:
|
||||||
github: jeromegn/kilt
|
github: jeromegn/kilt
|
||||||
version: 0.3.3
|
version: 0.3.3
|
||||||
|
|
||||||
|
multipart:
|
||||||
|
github: RX14/multipart.cr
|
||||||
|
version: 0.1.0
|
||||||
|
|
||||||
radix:
|
radix:
|
||||||
github: luislavena/radix
|
github: luislavena/radix
|
||||||
version: 0.3.0
|
version: 0.3.1
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ version: 0.2.0
|
||||||
dependencies:
|
dependencies:
|
||||||
kemal:
|
kemal:
|
||||||
github: sdogruyol/kemal
|
github: sdogruyol/kemal
|
||||||
branch: master
|
branch: v0.16.1
|
||||||
|
|
||||||
authors:
|
authors:
|
||||||
- Sdogruyol <dogruyolserdar@gmail.com>
|
- Sdogruyol <dogruyolserdar@gmail.com>
|
||||||
|
|
|
@ -1,16 +1,14 @@
|
||||||
require "./spec_helper"
|
# require "./spec_helper"
|
||||||
|
|
||||||
describe "SpecKemalApp" do
|
# describe "SpecKemalApp" do
|
||||||
start
|
# it "handles get" do
|
||||||
|
# get "/"
|
||||||
|
# response.body.should eq "Hello world"
|
||||||
|
# end
|
||||||
|
|
||||||
it "handles get" do
|
# it "handles post" do
|
||||||
get "/"
|
# json_body = {"name": "Serdar", "age": 27, "skills": ["crystal, kemal"]}
|
||||||
response.body.should eq "Hello world"
|
# post("/user", headers: HTTP::Headers{"Content-Type" => "application/json"}, body: json_body.to_json)
|
||||||
end
|
# response.body.should eq(json_body.to_json)
|
||||||
|
# end
|
||||||
it "handles post" do
|
# end
|
||||||
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
|
|
||||||
|
|
|
@ -1,16 +1,3 @@
|
||||||
require "spec"
|
require "spec"
|
||||||
require "kemal"
|
require "kemal"
|
||||||
require "../src/spec-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
|
|
||||||
|
|
|
@ -1,15 +1,6 @@
|
||||||
require "spec"
|
require "spec"
|
||||||
require "kemal"
|
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
|
Kemal.config.logging = false
|
||||||
|
|
||||||
class Global
|
class Global
|
||||||
|
@ -21,28 +12,37 @@ class Global
|
||||||
def self.response
|
def self.response
|
||||||
@@response
|
@@response
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
{% for method in %w(get post put head delete patch) %}
|
{% for method in %w(get post put head delete patch) %}
|
||||||
def {{method.id}}(path, headers : HTTP::Headers? = nil, body : String? = nil)
|
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
|
||||||
{% 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
|
def response
|
||||||
Global.response.not_nil!
|
Global.response.not_nil!
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue