Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Kemal indicate 404

require "kemal"

get "/" do
  %{
    <a href="/user/foo">/user/foo</a><br>
    <a href="/user/bar">/user/bar</a><br>
    <a href="/user/zorg">/user/zorg</a><br>
  }
end

DATABASE = Set{"foo", "bar"}

get "/user/:fname" do |env|
  fname = env.params.url["fname"]
  if !DATABASE.includes?(fname)
    halt env, status_code: 404, response: "We don't have this user <b>#{fname}</b>"
  end
  "received fname: #{fname}"
end

Kemal.run
ENV["KEMAL_ENV"] = "test"
require "spec-kemal"
require "../src/send_404"

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(%{<a href="/user/foo">/user/foo</a><br>})
    response.body.should_not contain(%{received})
  end

  it "renders /user/foo" do
    get "/user/foo"
    response.status_code.should eq 200
    response.headers["Content-Type"].should eq "text/html"
    response.body.should contain(%{received fname: foo})
  end

  it "renders /user/bar" do
    get "/user/bar"
    response.status_code.should eq 200
    response.headers["Content-Type"].should eq "text/html"
    response.body.should contain(%{received fname: bar})
  end

  it "renders /user/other" do
    get "/user/other"
    response.status_code.should eq 404
    response.headers["Content-Type"].should eq "text/html"
    response.body.should contain(%{We don't have this user <b>other</b>})
  end
end