Skip to content

Commit

Permalink
Do not err if headers is a valid headers hash
Browse files Browse the repository at this point in the history
  • Loading branch information
knu committed Nov 27, 2016
1 parent 9074f31 commit 5f5f3cd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
11 changes: 4 additions & 7 deletions app/models/agents/website_agent.rb
Expand Up @@ -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 }}`.
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions spec/models/agents/website_agent_spec.rb
Expand Up @@ -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 }}'
Expand Down

0 comments on commit 5f5f3cd

Please sign in to comment.