Permalink
Browse files

properly handle '&' within a Filter using a wrapper around CGI.escape

  • Loading branch information...
1 parent d67b0af commit 65a95077a6d38f924af2f3651ce5399b9db32107 @minimul minimul committed Apr 4, 2013
@@ -75,6 +75,13 @@ def parse_xml(xml)
Nokogiri::XML(xml)
end
+ def escape_filter(str)
+ # Ampersand needs to double encoded within an Oath request.
+ # CGI.escape will take the #26 and properly turn it into #2526
+ str.gsub!('&','%26')
+ CGI.escape(str)
+ end
+
def valid_xml_document(xml)
%Q{<?xml version="1.0" encoding="utf-8"?>\n#{xml.strip}}
end
@@ -86,7 +93,7 @@ def fetch_collection(model, filters = [], page = 1, per_page = 20, sort = nil, o
if filters.is_a?(Array) && filters.length > 0
filter_string = filters.collect { |f| f.to_s }
- post_body_lines << "Filter=#{CGI.escape(filter_string.join(" :AND: "))}"
+ post_body_lines << "Filter=#{escape_filter(filter_string.join(" :AND: "))}"
end
post_body_lines << "PageNum=#{page}"
@@ -106,6 +106,18 @@
@service.send(:fetch_collection, @model, [filter])
end
+ it "with an ampersand in filter value" do
+
+ filter = Quickeebooks::Online::Service::Filter.new(:text, :field => "Name", :value => "Smith & Gentry")
+
+ @service.should_receive(:do_http_post).with(@url,
+ "Filter=Name+%3AEQUALS%3A+Smith+%2526+Gentry&PageNum=1&ResultsPerPage=20",
+ {},
+ {"Content-Type"=>"application/x-www-form-urlencoded"})
+
+ @service.send(:fetch_collection, @model, [filter])
+ end
+
it "paginates" do
@service.should_receive(:do_http_post).with(@url,
"PageNum=2&ResultsPerPage=20",

0 comments on commit 65a9507

Please sign in to comment.