2023-06-29 17:07:24 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-06-26 02:03:07 +02:00
|
|
|
require_relative "setup"
|
|
|
|
require "rack/test"
|
|
|
|
|
|
|
|
class ServerDirTest < Test::Unit::TestCase
|
|
|
|
include Rack::Test::Methods
|
|
|
|
|
|
|
|
def test_index
|
|
|
|
get "/"
|
|
|
|
assert_equal 200, last_response.status
|
|
|
|
assert_equal "text/html", last_response.content_type
|
2023-06-27 18:55:12 +02:00
|
|
|
assert_equal bytesize("./test/webroot/index.html"), last_response.content_length
|
2023-06-26 02:03:07 +02:00
|
|
|
end
|
|
|
|
|
2023-06-26 12:49:50 +02:00
|
|
|
def test_ttf_font
|
|
|
|
get "/fonts/roboto-mono-regular.ttf"
|
|
|
|
assert_equal 200, last_response.status
|
|
|
|
assert_equal "font/ttf", last_response.content_type
|
2023-06-27 18:55:12 +02:00
|
|
|
assert_equal bytesize("./test/webroot/fonts/roboto-mono-regular.ttf"),
|
2023-06-26 12:49:50 +02:00
|
|
|
last_response.content_length
|
|
|
|
end
|
|
|
|
|
2023-06-26 15:56:25 +02:00
|
|
|
def test_js_file
|
|
|
|
get "/js/index.js"
|
|
|
|
assert_equal 200, last_response.status
|
|
|
|
assert_equal "application/javascript", last_response.content_type
|
2023-06-27 18:55:12 +02:00
|
|
|
assert_equal bytesize("./test/webroot/js/index.js"),
|
2023-06-26 15:56:25 +02:00
|
|
|
last_response.content_length
|
|
|
|
end
|
|
|
|
|
2023-06-27 09:36:12 +02:00
|
|
|
def test_png_file
|
|
|
|
get "/images/0x1eef.png"
|
|
|
|
assert_equal 200, last_response.status
|
|
|
|
assert_equal "image/png", last_response.content_type
|
2023-06-27 18:55:12 +02:00
|
|
|
assert_equal bytesize("./test/webroot/images/0x1eef.png"),
|
2023-06-27 09:36:12 +02:00
|
|
|
last_response.content_length
|
|
|
|
end
|
|
|
|
|
2023-06-27 18:54:48 +02:00
|
|
|
def test_json_file
|
|
|
|
get "/json/1.json"
|
|
|
|
assert_equal 200, last_response.status
|
|
|
|
assert_equal "application/json", last_response.content_type
|
2023-06-27 18:55:12 +02:00
|
|
|
assert_equal bytesize("./test/webroot/json/1.json"),
|
2023-06-27 18:54:48 +02:00
|
|
|
last_response.content_length
|
|
|
|
end
|
|
|
|
|
2023-06-27 20:22:18 +02:00
|
|
|
def test_internal_server_error
|
2023-06-29 17:07:24 +02:00
|
|
|
def app.finish(request) raise "test" end
|
2023-06-27 20:22:18 +02:00
|
|
|
get "/"
|
|
|
|
assert_equal 500, last_response.status
|
|
|
|
assert_equal "text/plain", last_response.content_type
|
|
|
|
assert_equal "Internal server error (RuntimeError)".bytesize,
|
|
|
|
last_response.content_length
|
|
|
|
assert_equal "Internal server error (RuntimeError)",
|
|
|
|
last_response.body
|
|
|
|
end
|
|
|
|
|
2023-06-26 10:34:58 +02:00
|
|
|
def test_permission_denied
|
2023-06-27 18:55:12 +02:00
|
|
|
File.chmod 0, "./test/webroot/permission_denied.html"
|
2023-06-26 10:34:58 +02:00
|
|
|
get "/permission_denied.html"
|
|
|
|
assert_equal 403, last_response.status
|
|
|
|
assert_equal "text/plain", last_response.content_type
|
|
|
|
assert_equal "Permission denied".bytesize, last_response.content_length
|
|
|
|
assert_equal "Permission denied", last_response.body
|
|
|
|
ensure
|
2023-06-29 17:07:24 +02:00
|
|
|
File.chmod 0o440, "./test/webroot/permission_denied.html"
|
2023-06-26 10:34:58 +02:00
|
|
|
end
|
|
|
|
|
2023-06-26 11:18:18 +02:00
|
|
|
def test_page_not_found
|
|
|
|
get "/foobarbaz"
|
|
|
|
assert_equal 404, last_response.status
|
|
|
|
assert_equal "text/plain", last_response.content_type
|
|
|
|
assert_equal "The requested URL was not found".bytesize, last_response.content_length
|
|
|
|
assert_equal "The requested URL was not found", last_response.body
|
|
|
|
end
|
|
|
|
|
2023-06-26 02:03:07 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def app
|
2023-06-27 18:55:12 +02:00
|
|
|
@app ||= Server.app("./test/webroot/")
|
2023-06-26 02:03:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def bytesize(path)
|
|
|
|
File.binread(path).bytesize
|
|
|
|
end
|
|
|
|
end
|