Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More fixes for HTML entity handling inside of JSON

  • Loading branch information...
commit 5670d299c976c7d7e8c43232361a2d8efc4e43bb 1 parent 4f9a593
@chicks chicks authored
View
2  lib/sugarcrm/connection/helper.rb
@@ -46,5 +46,5 @@ def b64_encode(file)
def b64_decode(file)
Base64.decode64(file)
end
-
+
end; end
View
19 lib/sugarcrm/connection/request.rb
@@ -22,14 +22,14 @@ def initialize(url, method, json, debug=false)
end
def escape(json)
- # BUG: SugarCRM doesn't properly handle '"' inside of JSON for some reason. Let's convert it back just in case.
- j = CGI.unescapeHTML(json)
- # Now we convert everything else.
+ # BUG: SugarCRM doesn't properly handle '"' inside of JSON for some reason. Let's unescape any html elements.
+ j = convert_reserved_characters(json)
+ # Now we escape the resulting string.
j = CGI.escape(j)
j
end
- # TODO: Fix this so that it parses.
+ # TODO: Fix this so that it JSON.parse will consume it.
def unescape
j = CGI.unescape(@json)
j.gsub!(/\n/, '')
@@ -46,4 +46,15 @@ def length
def to_s
@request
end
+
+ # A tiny helper for converting reserved characters for html encoding
+ def convert_reserved_characters(string)
+ string.gsub!(/"/, '\"')
+ string.gsub!(/'/, '\'')
+ string.gsub!(/&/, '\&')
+ string.gsub!(/&lt;/, '\<')
+ string.gsub!(/&lt;/, '\>')
+ string
+ end
+
end; end
View
2  test/test_module.rb
@@ -7,7 +7,7 @@ class TestModule < ActiveSupport::TestCase
end
should "return required fields when #required_fields" do
- assert SugarCRM::User._module.required_fields.include? :user_name
+ assert SugarCRM::Account._module.required_fields.include? :name
end
# TODO: Figure out a way to test this.
View
2  test/test_request.rb
@@ -27,7 +27,7 @@ class TestRequest < ActiveSupport::TestCase
end
should "properly escape JSON" do
- assert_equal "%22OMG+HAI%21%22", @request.escape("&quot;OMG HAI!&quot;" )
+ assert_equal "%7B%22hello%22%3A+%22%5C%22OMG+HAI%21%5C%22%22%7D", @request.escape('{"hello": "&quot;OMG HAI!&quot;"}')
end
end
View
3  test/test_sugarcrm.rb
@@ -192,7 +192,8 @@ class TestSugarCRM < ActiveSupport::TestCase
assert !SugarCRM::User.first.blank?
end
end
-
+
+ # TODO: Fix this test so it creates the Note properly before asserting.
context "A SugarCRM::Note instance" do
should "return the correct parent record with the `parent` method" do
note = SugarCRM::Note.first
Please sign in to comment.
Something went wrong with that request. Please try again.