-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
ENV["KEMAL_ENV"] = "test" | ||
require "spec-kemal" | ||
require "../src/set_header" | ||
|
||
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(%{Hello Kemal}) | ||
end | ||
|
||
it "renders /sitemap.xml" do | ||
get "/sitemap.xml" | ||
response.status_code.should eq 200 | ||
response.headers["Content-Type"].should eq "application/xml" | ||
response.body.should contain(%{<?xml version="1.0" encoding="UTF-8"?>}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require "kemal" | ||
|
||
class HTTP::Server::Context | ||
getter start_time : Time::Span = Time.monotonic | ||
end | ||
|
||
get "/" do |env| | ||
# spend some time | ||
x = 0 | ||
loop do | ||
x += 1 | ||
break if x > 10_000_000 | ||
end | ||
text = "This is some text" | ||
render "src/views/time.ecr", "src/views/layouts/layout_with_elapsed_time.ecr" | ||
end | ||
|
||
Kemal.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require "kemal" | ||
|
||
base_url = "https://code-maven.com" | ||
|
||
get "/" do | ||
"Hello Kemal" | ||
end | ||
|
||
# source | ||
get "/sitemap.xml" do |env| | ||
now = Time.utc | ||
now_str = now.to_s("%Y-%m-%d") | ||
env.response.content_type = "application/xml" | ||
xml = %{ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
<url> | ||
<loc>#{base_url}/</loc> | ||
<lastmod>#{now_str}</lastmod> | ||
</url> | ||
</urlset> | ||
} | ||
xml | ||
end | ||
|
||
Kemal.run |
15 changes: 15 additions & 0 deletions
15
crystal/examples/kemal/src/views/layouts/layout_with_elapsed_time.ecr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes"> | ||
<title></title> | ||
</head> | ||
<body> | ||
<%= content %> | ||
<hr> | ||
<div> | ||
Elapsed time <%= (Time.monotonic - env.start_time).total_milliseconds %> ms | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
<%= text %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters