diff --git a/lib/grape/dsl/headers.rb b/lib/grape/dsl/headers.rb index a02bdd588..710edaa9e 100644 --- a/lib/grape/dsl/headers.rb +++ b/lib/grape/dsl/headers.rb @@ -10,7 +10,14 @@ module Headers # 4. Delete a specifc header key-value pair def header(key = nil, val = nil) if key - val ? header[key.to_s] = val : header.delete(key.to_s) + if val + unless val.is_a?(String) + warn "Header value for '#{key}' is not a string. Converting to string." + end + header[key.to_s] = val.to_s + else + header.delete(key.to_s) + end else @header ||= Grape::Util::Header.new end diff --git a/spec/grape/dsl/headers_spec.rb b/spec/grape/dsl/headers_spec.rb index 1502176bd..042e94e77 100644 --- a/spec/grape/dsl/headers_spec.rb +++ b/spec/grape/dsl/headers_spec.rb @@ -56,4 +56,17 @@ end end end + + context 'when non-string headers are set' do + describe '#header' do + it 'converts non-string header values to strings' do + subject.header('integer key', 123) + expect(subject.header['integer key']).to eq '123' + end + + it 'emits a warning if the header value is not a string' do + expect { subject.header('integer key', 123) }.to output("Header value for 'integer key' is not a string. Converting to string.\n").to_stderr + end + end + end end