Commit f25259a8f75adcb357ab16a55167a04633da6176

Authored by Daniel Beardsley
1 parent 98963feb
Exists in master and in 1 other branch production

Notice API: properly transform <var> tags

<var> tags with blank values were ending up completely wrong in the
resulting hash.

This: (a user with a blank name)
    <var key="user">
       <var key="id">123</var>
       <var key="name"/>
    </var>

Gets parsed by hoptoad_notifier into this:
    {
       'key' => 'user',
       'var' => [{
             'key' => 'id',
             '__content__' => '123'
          },{
             'key' => 'name'
          }]
    }

Which when passed through the "rekey" method, ended up like this:
    {
       'user' => {
          'id' => '123',
          'key' => 'name' # bad and wrong
       }
    }

Now, after these changes, it correctly comes through as:
    {
       'user' => {
          'id' => '123',
          'name' => nil # much better!
       }
    }
lib/hoptoad/v2.rb
... ... @@ -18,6 +18,8 @@ module Hoptoad
18 18 {normalize_key(node['key']) => rekey(node['__content__'])}
19 19 elsif node.has_key?('__content__')
20 20 rekey(node['__content__'])
  21 + elsif node.has_key?('key')
  22 + {normalize_key(node['key']) => nil}
21 23 else
22 24 node.inject({}) {|rekeyed, (key, val)| rekeyed.merge(normalize_key(key) => rekey(val))}
23 25 end
... ...
spec/controllers/notices_controller_spec.rb
... ... @@ -24,6 +24,36 @@ describe NoticesController do
24 24 response.body.should match(%r{<url[^>]*>(.+)#{locate_path(@notice.id)}</url>})
25 25 end
26 26  
  27 + it "should trnasform xml <va> tags to hashes correctly" do
  28 + App.should_receive(:report_error!).with(@xml).and_return(@notice)
  29 + request.should_receive(:raw_post).and_return(@xml)
  30 + post :create, :format => :xml
  31 +
  32 + # XML: <var key="SCRIPT_NAME"/>
  33 + @notice.env_vars.should have_key('SCRIPT_NAME')
  34 + @notice.env_vars['SCRIPT_NAME'].should be_nil # blank ends up nil
  35 +
  36 + # XML representation:
  37 + # <var key="rack.session.options">
  38 + # <var key="secure">false</var>
  39 + # <var key="httponly">true</var>
  40 + # <var key="path">/</var>
  41 + # <var key="expire_after"/>
  42 + # <var key="domain"/>
  43 + # <var key="id"/>
  44 + # </var>
  45 + expected = {
  46 + 'secure' => 'false',
  47 + 'httponly' => 'true',
  48 + 'path' => '/',
  49 + 'expire_after' => nil,
  50 + 'domain' => nil,
  51 + 'id' => nil
  52 + }
  53 + @notice.env_vars.should have_key('rack_session_options')
  54 + @notice.env_vars['rack_session_options'].should eql(expected)
  55 + end
  56 +
27 57 it "generates a notice from xml in a data param [POST]" do
28 58 App.should_receive(:report_error!).with(@xml).and_return(@notice)
29 59 post :create, :data => @xml, :format => :xml
... ...