Kemal POST parameters



examples/kemal/src/post_params.cr
require "kemal"

get "/" do
  form("")
end

post "/" do |env|
  response = ""
  text = env.params.body["text"]?
  if !text.nil?
    response = "You typed in <b>#{text}</b>"
  end
  form(response)
end

def form(response)
  %{
    <form method="POST" action="/">
    <input name="text">
    <input type="submit" value="Echo">
    </form>
    #{response}
  }
end

Kemal.run

examples/kemal/spec/post_params_spec.cr
ENV["KEMAL_ENV"] = "test"
require "spec-kemal"
require "../src/post_params"

describe "Web Application" do
  it "renders /" do
    get "/"
    response.status_code.should eq 200
    response.headers["Content-Type"].should eq "text/html"
    response.body.should contain(%{<form method="POST" action="/">})
    response.body.should_not contain(%{You typed in <b>})
  end

  it "renders / POST text=Foo Bar" do
    post "/", body: "text=Foo Bar", headers: HTTP::Headers{"Content-Type" => "application/x-www-form-urlencoded"}
    response.status_code.should eq 200
    response.headers["Content-Type"].should eq "text/html"
    response.body.should contain(%{<form method="POST" action="/">})
    response.body.should contain(%{You typed in <b>Foo Bar</b>})
  end
end

crystal spec/post_params_spec.cr