diff --git a/app/models/agents/website_agent.rb b/app/models/agents/website_agent.rb index e8b976861c..37fd780a7f 100644 --- a/app/models/agents/website_agent.rb +++ b/app/models/agents/website_agent.rb @@ -136,7 +136,7 @@ class WebsiteAgent < Agent * `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 @@ def url 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 diff --git a/spec/models/agents/website_agent_spec.rb b/spec/models/agents/website_agent_spec.rb index 556f9c33b8..afceb2502b 100644 --- a/spec/models/agents/website_agent_spec.rb +++ b/spec/models/agents/website_agent_spec.rb @@ -1219,6 +1219,24 @@ 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 }}'