diff --git a/lib/graphiql/rails/config.rb b/lib/graphiql/rails/config.rb index e4c9581..417ebad 100644 --- a/lib/graphiql/rails/config.rb +++ b/lib/graphiql/rails/config.rb @@ -36,7 +36,8 @@ def resolve_headers(view_context) end all_headers.each_with_object({}) do |(key, value), memo| - memo[key] = value.call(view_context) + header_value = value.call(view_context) + memo[key] = header_value if !header_value.nil? end end end diff --git a/test/graphiql/rails/config_test.rb b/test/graphiql/rails/config_test.rb index 59361fe..3eed0a1 100644 --- a/test/graphiql/rails/config_test.rb +++ b/test/graphiql/rails/config_test.rb @@ -2,6 +2,8 @@ class ConfigTest < ActiveSupport::TestCase class MockViewContext + attr_accessor :customer_header_value + def form_authenticity_token "abc-123" end @@ -9,6 +11,7 @@ def form_authenticity_token setup do @config = GraphiQL::Rails::Config.new + @config.headers["X-Custom-Header"] = ->(view_context) { view_context.customer_header_value } @view_context = MockViewContext.new end @@ -21,4 +24,14 @@ def form_authenticity_token test "it adds JSON header by default" do assert_equal "application/json", @config.resolve_headers(@view_context)["Content-Type"] end + + test "when the customer header value is nil it is not added" do + @view_context.customer_header_value = nil + assert_equal @config.resolve_headers(@view_context).has_key?("X-Custom-Header"), false + end + + test "when the customer header value is not nil it is added" do + @view_context.customer_header_value = "some-value" + assert_equal "some-value", @config.resolve_headers(@view_context)["X-Custom-Header"] + end end