Skip to content

Commit

Permalink
Merge pull request #109 from peterfication/ignore-null-headers
Browse files Browse the repository at this point in the history
Prevent setting nil headers
  • Loading branch information
rmosolgo authored Feb 23, 2024
2 parents e7bfd39 + 670c06b commit 5066dfc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/graphiql/rails/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/graphiql/rails/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

class ConfigTest < ActiveSupport::TestCase
class MockViewContext
attr_accessor :customer_header_value

def form_authenticity_token
"abc-123"
end
end

setup do
@config = GraphiQL::Rails::Config.new
@config.headers["X-Custom-Header"] = ->(view_context) { view_context.customer_header_value }
@view_context = MockViewContext.new
end

Expand All @@ -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

0 comments on commit 5066dfc

Please sign in to comment.