From 3438ba4d3a5dba9a03378544d509404cd5ce043d Mon Sep 17 00:00:00 2001 From: Kainoa Kanter <44733677+ThatOneCalculator@users.noreply.github.com> Date: Wed, 28 Jun 2023 13:15:49 -0700 Subject: [PATCH] feat: custom static error pages (#2) * feat: :sparkles: Detect custom error pages Closes #1 * more descriptive comment * feat: use IO.copy instead of reading the file * chore: avoid building string twice * feat: spec * chore: format --------- Co-authored-by: Evangelos Paterakis --- README.md | 2 +- shard.yml | 2 +- spec/static_file_handler_spec.cr | 22 ++++++++++++++++++++++ src/sabo-tabby/error_handler.cr | 10 ++++++++-- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c53b813..ea4feec 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ They are built & published by our lovely [actions](https://github.com/GeopJr/sab ## Usage ``` -sabo-tabby v1.0.0 +sabo-tabby v1.1.0 Usage: sabo-tabby [arguments] diff --git a/shard.yml b/shard.yml index 64dcd80..d4cb02b 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: sabo-tabby -version: 1.0.0 +version: 1.1.0 authors: - Evangelos Paterakis diff --git a/spec/static_file_handler_spec.cr b/spec/static_file_handler_spec.cr index 72cd3c0..7bc842b 100644 --- a/spec/static_file_handler_spec.cr +++ b/spec/static_file_handler_spec.cr @@ -95,6 +95,28 @@ describe Sabo::Tabby::StaticFileHandler do response.headers["Content-Encoding"]?.should be_nil end + it "should serve custom static error pages" do + page_content = "🫖" + + # Create custom static error page + current_public_folder = Sabo::Tabby.config.public_folder + folder = Path[Dir.tempdir, Random::Secure.hex(5)] + Dir.mkdir(folder) + Sabo::Tabby.config.public_folder = folder.to_s + + custom_page_path = Path[folder, "404.html"] + File.write(custom_page_path, page_content) + + response = handle_request HTTP::Request.new("GET", "/I_DO_NOT_EXIST") + response.status_code.should eq(404) + response.body.should eq(page_content) + + # Cleanup + Sabo::Tabby.config.public_folder = current_public_folder + File.delete(custom_page_path) + Dir.delete(folder) + end + it "should not serve a not found file" do response = handle_request HTTP::Request.new("GET", "/not_found_file.txt") response.status_code.should eq(404) diff --git a/src/sabo-tabby/error_handler.cr b/src/sabo-tabby/error_handler.cr index 32d29c1..d8e8f75 100644 --- a/src/sabo-tabby/error_handler.cr +++ b/src/sabo-tabby/error_handler.cr @@ -34,8 +34,14 @@ module Sabo::Tabby @status_message = message ||= @status.description self.headers.add "Server", Sabo::Tabby::SERVER_HEADER if Sabo::Tabby.config.server_header - # If HTML pages are enabled, call `error_page` else return a basic text/plain one. - if Sabo::Tabby.config.error_page + # If it exists, use file in the public dir matching error_code.html (eg 404.html). + static_error_page = Path[Sabo::Tabby.config.public_folder, "#{status_code}.html"] + if File.exists?(static_error_page) + self.content_type = "text/html; charset=UTF-8" + IO.copy File.open(static_error_page), self + + # If HTML pages are enabled, call `error_page` else return a basic text/plain one. + elsif Sabo::Tabby.config.error_page self.content_type = "text/html; charset=UTF-8" self << error_page(@status.code, @status_message.to_s) << '\n' else