From b50d4904e03fa317f3b6cdb0c6ede0a5dd8dd225 Mon Sep 17 00:00:00 2001 From: Marvin Ahlgrimm Date: Mon, 5 Aug 2024 12:36:11 +0200 Subject: [PATCH] Handle case if no user agent is set --- spec/marten-turbo/ext/marten/http/request_spec.cr | 10 ++++++++++ src/marten_turbo/ext/marten/http/request.cr | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/spec/marten-turbo/ext/marten/http/request_spec.cr b/spec/marten-turbo/ext/marten/http/request_spec.cr index 60a53eb..25c4a6e 100644 --- a/spec/marten-turbo/ext/marten/http/request_spec.cr +++ b/spec/marten-turbo/ext/marten/http/request_spec.cr @@ -43,5 +43,15 @@ describe Marten::HTTP::Request do request.turbo_native_app?.should be_true end + + it "correctly returns false if the request headers do not contain a user agent" do + request = Marten::HTTP::Request.new( + method: "GET", + resource: "/test/xyz", + headers: HTTP::Headers{"Accept" => "text/html"}, + ) + + request.turbo_native_app?.should be_false + end end end diff --git a/src/marten_turbo/ext/marten/http/request.cr b/src/marten_turbo/ext/marten/http/request.cr index 9dd3545..2a2203c 100644 --- a/src/marten_turbo/ext/marten/http/request.cr +++ b/src/marten_turbo/ext/marten/http/request.cr @@ -5,6 +5,7 @@ class Marten::HTTP::Request def turbo_native_app? user_agent = @request.headers["User-Agent"]? - user_agent.includes?("Turbo Native") if user_agent + + user_agent ? user_agent.includes?("Turbo Native") : false end end