server: add TTF font support

This commit is contained in:
0x1eef 2023-06-26 07:49:50 -03:00
parent 40f51387bd
commit 76b1d24669
3 changed files with 14 additions and 1 deletions

View file

@ -1,6 +1,10 @@
# frozen_string_literal: true
class Server::Dir
MIME_TYPES = {
".ttf" => "font/ttf"
}.freeze
def initialize(root)
@root = File.realpath(root)
end
@ -24,8 +28,9 @@ class Server::Dir
def read(path)
body = File.binread(path)
extn = File.extname(path)
[
{"content-type" => Rack::Mime.mime_type(File.extname(path)),
{"content-type" => MIME_TYPES[extn] || Rack::Mime.mime_type(extn),
"content-length" => body.bytesize},
body.each_line
]

View file

@ -11,6 +11,14 @@ class ServerDirTest < Test::Unit::TestCase
assert_equal bytesize("./test/fakeweb/index.html"), last_response.content_length
end
def test_ttf_font
get "/fonts/roboto-mono-regular.ttf"
assert_equal 200, last_response.status
assert_equal "font/ttf", last_response.content_type
assert_equal bytesize("./test/fakeweb/fonts/roboto-mono-regular.ttf"),
last_response.content_length
end
def test_permission_denied
File.chmod 0, "./test/fakeweb/permission_denied.html"
get "/permission_denied.html"