Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions config/sus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@

require "covered/sus"
include Covered::Sus

require "tmpdir"
6 changes: 3 additions & 3 deletions lib/luna/middleware/markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def call(request)
return super unless fs_path

if File.directory?(fs_path)
candidate = @index_candidates.map {|n| File.join(fs_path, n)}.find {|p| File.file?(p)}
candidate = @index_candidates.map{|n| File.join(fs_path, n)}.find{|p| File.file?(p)}
return render_file(candidate, head: request.method == "HEAD") if candidate
elsif File.file?(fs_path) && markdown_file?(fs_path)
return render_file(fs_path, head: request.method == "HEAD")
Expand Down Expand Up @@ -68,8 +68,8 @@ def render_file(path, head: false)
content = File.read(path)
html = render_html(content)
headers = [
["content-type", "text/html; charset=utf-8"]
]
["content-type", "text/html; charset=utf-8"]
]
Protocol::HTTP::Response[200, headers, head ? nil : [html]]
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/luna/middleware/static.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ def call(request)
def serve_file(path, head: false)
stat = File.stat(path)
headers = [
["content-type", mime_type_for_extension(File.extname(path))],
["last-modified", stat.mtime.httpdate],
]
["content-type", mime_type_for_extension(File.extname(path))],
["last-modified", stat.mtime.httpdate],
]

body = head ? nil : Protocol::HTTP::Body::File.open(path)

Expand All @@ -92,7 +92,7 @@ def serve_file(path, head: false)

def serve_directory_listing(dir_path, request_path)
entries = Dir.entries(dir_path).sort - ["."]
entries.reject! {|e| e == ".."} if request_path == "/"
entries.reject!{|e| e == ".."} if request_path == "/"
links = entries.map do |e|
name = e
target = File.join(request_path, e)
Expand All @@ -108,8 +108,8 @@ def serve_directory_listing(dir_path, request_path)
HTML

headers = [
["content-type", "text/html; charset=utf-8"]
]
["content-type", "text/html; charset=utf-8"]
]

Protocol::HTTP::Response[200, headers, [html]]
end
Expand Down
37 changes: 16 additions & 21 deletions test/integration/server_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,36 @@
# Copyright, 2026, by Samuel Williams.

require "sus/fixtures/async/http"
require "sus/fixtures/temporary_directory_context"
require "fileutils"

require_relative "../../lib/luna/server"

include Sus::Fixtures::Async::HTTP::ServerContext
include Sus::Fixtures::TemporaryDirectoryContext

around do |&block|
Dir.mktmpdir do |root|
@root = root
super(&block)
end
end

let(:root) {File.join(@root, "www")}
let(:www_root) {File.join(root, "www")}

let(:app) do
FileUtils.mkdir_p(root)
FileUtils.mkdir_p(www_root)
Luna::Server.middleware(
root: root,
markdown: true,
verbose: false,
directory_listing: true
)
root: www_root,
markdown: true,
verbose: false,
directory_listing: true
)
end

it "serves index.html at root" do
File.write(File.join(root, "index.html"), "<h1>Home</h1>")
File.write(File.join(www_root, "index.html"), "<h1>Home</h1>")
response = client.get("/")
expect(response.status).to be == 200
body = response.read
expect(body).to be(:include?, "Home")
end

it "renders markdown files as html" do
File.write(File.join(root, "README.md"), "# Hello\n\nThis is **Markdown**.")
File.write(File.join(www_root, "README.md"), "# Hello\n\nThis is **Markdown**.")
response = client.get("/README.md")
expect(response.status).to be == 200
expect(response.headers["content-type"]).to be(:start_with?, "text/html")
Expand All @@ -48,24 +43,24 @@
end

it "serves directory index when present" do
FileUtils.mkdir_p(File.join(root, "docs"))
File.write(File.join(root, "docs", "index.html"), "<p>Docs</p>")
FileUtils.mkdir_p(File.join(www_root, "docs"))
File.write(File.join(www_root, "docs", "index.html"), "<p>Docs</p>")
response = client.get("/docs/")
expect(response.status).to be == 200
expect(response.read).to be(:include?, "Docs")
end

it "lists directory when no index and listing enabled" do
FileUtils.mkdir_p(File.join(root, "list"))
File.write(File.join(root, "list", "file.txt"), "data")
FileUtils.mkdir_p(File.join(www_root, "list"))
File.write(File.join(www_root, "list", "file.txt"), "data")
response = client.get("/list/")
expect(response.status).to be == 200
expect(response.headers["content-type"]).to be(:include?, "text/html")
expect(response.read).to be(:include?, "file.txt")
end

it "handles HEAD requests without body" do
File.write(File.join(root, "file.json"), '{"a":1}')
File.write(File.join(www_root, "file.json"), '{"a":1}')
response = client.head("/file.json")
expect(response.status).to be == 200
end
22 changes: 9 additions & 13 deletions test/luna/middleware/markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,32 @@
require "fileutils"
require "protocol/http/request"
require "protocol/http/response"
require "sus/fixtures/temporary_directory_context"

require_relative "../../../lib/luna/middleware/markdown"

describe Luna::Middleware::Markdown do
around do |&block|
Dir.mktmpdir do |root|
@root = root
super(&block)
end
end
include Sus::Fixtures::TemporaryDirectoryContext

let(:root) {File.join(@root, "www")}
let(:app) {->(request) {Protocol::HTTP::Response[404, { "content-type" => "text/plain" }, ["Not Found"]]}}
let(:middleware) {Luna::Middleware::Markdown.new(app, root: root)}
let(:www_root) {File.join(root, "www")}
let(:app) {->(request){Protocol::HTTP::Response[404, { "content-type" => "text/plain" }, ["Not Found"]]}}
let(:middleware) {Luna::Middleware::Markdown.new(app, root: www_root)}

def request(method, path)
Protocol::HTTP::Request[method, path, [], nil]
end

it "renders markdown file as html" do
FileUtils.mkdir_p(root)
File.write(File.join(root, "README.md"), "# Hello\n\nThis is **Markdown**.")
FileUtils.mkdir_p(www_root)
File.write(File.join(www_root, "README.md"), "# Hello\n\nThis is **Markdown**.")
response = middleware.call(request("GET", "/README.md"))
expect(response.status).to be == 200
expect(response.headers["content-type"]).to be =~ /text\/html/
end

it "renders index.md in directory" do
FileUtils.mkdir_p(File.join(root, "docs"))
File.write(File.join(root, "docs", "index.md"), "# Docs Index")
FileUtils.mkdir_p(File.join(www_root, "docs"))
File.write(File.join(www_root, "docs", "index.md"), "# Docs Index")
response = middleware.call(request("GET", "/docs/"))
expect(response.status).to be == 200
end
Expand Down
34 changes: 15 additions & 19 deletions test/luna/middleware/static.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,33 @@
require "fileutils"
require "protocol/http/request"
require "protocol/http/response"
require "sus/fixtures/temporary_directory_context"

require_relative "../../../lib/luna/middleware/static"

describe Luna::Middleware::Static do
around do |&block|
Dir.mktmpdir do |root|
@root = root
super(&block)
end
end
include Sus::Fixtures::TemporaryDirectoryContext

let(:root) {File.join(@root, "www")}
let(:app) {->(request) {Protocol::HTTP::Response[404, { "content-type" => "text/plain" }, ["Not Found"]]}}
let(:middleware) {Luna::Middleware::Static.new(app, root: root, index: "index.html", directory_listing: true)}
let(:www_root) {File.join(root, "www")}
let(:app) {->(request){Protocol::HTTP::Response[404, { "content-type" => "text/plain" }, ["Not Found"]]}}
let(:middleware) {Luna::Middleware::Static.new(app, root: www_root, index: "index.html", directory_listing: true)}

def request(method, path)
Protocol::HTTP::Request[method, path, [], nil]
end

with "serving files" do
it "serves index.html from root" do
FileUtils.mkdir_p(root)
File.write(File.join(root, "index.html"), "<h1>Hello</h1>")
FileUtils.mkdir_p(www_root)
File.write(File.join(www_root, "index.html"), "<h1>Hello</h1>")
response = middleware.call(request("GET", "/"))
expect(response.status).to be == 200
expect(response.headers["content-type"]).to be == "text/html; charset=utf-8"
end

it "serves a specific file" do
FileUtils.mkdir_p(root)
File.write(File.join(root, "hello.txt"), "world")
FileUtils.mkdir_p(www_root)
File.write(File.join(www_root, "hello.txt"), "world")
response = middleware.call(request("GET", "/hello.txt"))
expect(response.status).to be == 200
expect(response.headers["content-type"]).to be == "text/plain; charset=utf-8"
Expand All @@ -45,15 +41,15 @@ def request(method, path)

with "directories" do
it "serves index file in subdirectory" do
FileUtils.mkdir_p(File.join(root, "blog"))
File.write(File.join(root, "blog", "index.html"), "<h1>Blog</h1>")
FileUtils.mkdir_p(File.join(www_root, "blog"))
File.write(File.join(www_root, "blog", "index.html"), "<h1>Blog</h1>")
response = middleware.call(request("GET", "/blog/"))
expect(response.status).to be == 200
end

it "lists directory when no index" do
FileUtils.mkdir_p(File.join(root, "list"))
File.write(File.join(root, "list", "file.txt"), "data")
FileUtils.mkdir_p(File.join(www_root, "list"))
File.write(File.join(www_root, "list", "file.txt"), "data")
response = middleware.call(request("GET", "/list/"))
expect(response.status).to be == 200
expect(response.headers["content-type"]).to be =~ /text\/html/
Expand All @@ -62,8 +58,8 @@ def request(method, path)

with "head requests" do
it "omits body" do
FileUtils.mkdir_p(root)
File.write(File.join(root, "file.json"), '{"a":1}')
FileUtils.mkdir_p(www_root)
File.write(File.join(www_root, "file.json"), '{"a":1}')
response = middleware.call(request("HEAD", "/file.json"))
expect(response.status).to be == 200
end
Expand Down
Loading