Merge pull request #980 from stesie/xml-post-agent

Make post_agent handle content_type=xml

Andrew Cantino преди 9 години
родител
ревизия
9468d6442b
променени са 2 файла, в които са добавени 29 реда и са изтрити 2 реда
  1. 4 1
      app/models/agents/post_agent.rb
  2. 25 1
      spec/models/agents/post_agent_spec.rb

+ 4 - 1
app/models/agents/post_agent.rb

@@ -13,7 +13,7 @@ module Agents
13 13
 
14 14
       The `method` used can be any of `get`, `post`, `put`, `patch`, and `delete`.
15 15
 
16
-      By default, non-GETs will be sent with form encoding (`application/x-www-form-urlencoded`).  Change `content_type` to `json` to send JSON instead.
16
+      By default, non-GETs will be sent with form encoding (`application/x-www-form-urlencoded`).  Change `content_type` to `json` to send JSON instead.  Change `content_type` to `xml` to send XML, where the name of the root element may be specified using `xml_root`, defaulting to `post`.
17 17
 
18 18
       Other Options:
19 19
 
@@ -102,6 +102,9 @@ module Agents
102 102
         when 'json'
103 103
           headers['Content-Type'] = 'application/json; charset=utf-8'
104 104
           body = data.to_json
105
+        when 'xml'
106
+          headers['Content-Type'] = 'text/xml; charset=utf-8'
107
+          body = data.to_xml(root: (interpolated(payload)[:xml_root] || 'post'))
105 108
         else
106 109
           body = data
107 110
         end

+ 25 - 1
spec/models/agents/post_agent_spec.rb

@@ -38,11 +38,14 @@ describe Agents::PostAgent do
38 38
       when :get, :delete
39 39
         req.data = request.uri.query
40 40
       else
41
-        case request.headers['Content-Type'][/\A[^;\s]+/]
41
+        content_type = request.headers['Content-Type'][/\A[^;\s]+/]
42
+        case content_type
42 43
         when 'application/x-www-form-urlencoded'
43 44
           req.data = request.body
44 45
         when 'application/json'
45 46
           req.data = ActiveSupport::JSON.decode(request.body)
47
+        when 'text/xml'
48
+          req.data = Hash.from_xml(request.body)
46 49
         else
47 50
           raise "unexpected Content-Type: #{content_type}"
48 51
         end
@@ -152,6 +155,27 @@ describe Agents::PostAgent do
152 155
       expect(@sent_requests[:post][0].data).to eq(@checker.options['payload'])
153 156
     end
154 157
 
158
+    it "sends options['payload'] as XML as a POST request" do
159
+      @checker.options['content_type'] = 'xml'
160
+      expect {
161
+        @checker.check
162
+      }.to change { @sent_requests[:post].length }.by(1)
163
+
164
+      expect(@sent_requests[:post][0].data.keys).to eq([ 'post' ])
165
+      expect(@sent_requests[:post][0].data['post']).to eq(@checker.options['payload'])
166
+    end
167
+
168
+    it "sends options['payload'] as XML with custom root element name, as a POST request" do
169
+      @checker.options['content_type'] = 'xml'
170
+      @checker.options['xml_root'] = 'foobar'
171
+      expect {
172
+        @checker.check
173
+      }.to change { @sent_requests[:post].length }.by(1)
174
+
175
+      expect(@sent_requests[:post][0].data.keys).to eq([ 'foobar' ])
176
+      expect(@sent_requests[:post][0].data['foobar']).to eq(@checker.options['payload'])
177
+    end
178
+
155 179
     it "sends options['payload'] as a GET request" do
156 180
       @checker.options['method'] = 'get'
157 181
       expect {