Do not err if headers is a valid headers hash

This commit is contained in:
Akinori MUSHA 2016-11-23 11:17:38 +09:00
parent 9074f3115e
commit 5f5f3cd38f
2 changed files with 22 additions and 7 deletions

View file

@ -136,7 +136,7 @@ module Agents
* `status`: HTTP status as integer. (Almost always 200) When parsing `data_from_event`, this is set to the value of the `status` key in the incoming Event, if it is a number or a string convertible to an integer.
* `headers`: Response headers; for example, `{{ _response_.headers.Content-Type }}` expands to the value of the Content-Type header. Keys are insensitive to cases and -/_. When parsing `data_from_event`, this is constructed from the value of the `headers` key in the incoming Event.
* `headers`: Response headers; for example, `{{ _response_.headers.Content-Type }}` expands to the value of the Content-Type header. Keys are insensitive to cases and -/_. When parsing `data_from_event`, this is constructed from the value of the `headers` key in the incoming Event, if it is a hash.
* `url`: The final URL of the fetched page, following redirects. When parsing `data_from_event`, this is set to the value of the `url` key in the incoming Event. Using this in the `template` option, you can resolve relative URLs extracted from a document like `{{ link | to_uri: _request_.url }}` and `{{ content | rebase_hrefs: _request_.url }}`.
@ -684,12 +684,9 @@ module Agents
class ResponseFromEventDrop < LiquidDroppable::Drop
def headers
case headers = @object.payload[:headers]
when Hash
HeaderDrop.new(Faraday::Utils::Headers.from(headers))
else
HeaderDrop.new({})
end
headers = Faraday::Utils::Headers.from(@object.payload[:headers]) rescue {}
HeaderDrop.new(headers)
end
# Integer value of HTTP status

View file

@ -1219,6 +1219,24 @@ fire: hot
expect(@checker.events.last.payload).to eq(@event.payload.merge('value' => 'world', 'url' => 'http://example.com/world', 'type' => 'application/json', 'status' => 200))
end
it "should convert headers and status in the event data properly" do
@event.payload[:status] = '201'
@event.payload[:headers] = [['Content-Type', 'application/rss+xml']]
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
expect(@checker.events.last.payload).to eq({ 'value' => 'world', 'url' => 'http://example.com/world', 'type' => 'application/rss+xml', 'status' => 201 })
end
it "should ignore inconvertible headers and status in the event data" do
@event.payload[:status] = 'ok'
@event.payload[:headers] = ['Content-Type', 'Content-Length']
expect {
@checker.receive([@event])
}.to change { Event.count }.by(1)
expect(@checker.events.last.payload).to eq({ 'value' => 'world', 'url' => 'http://example.com/world', 'type' => '', 'status' => nil })
end
it "should output an error when nothing can be found at the path" do
@checker.options = @checker.options.merge(
'data_from_event' => '{{ some_object.mistake }}'