Skip to content

Commit

Permalink
More fixes for HTML entity handling inside of JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
chicks committed Nov 26, 2011
1 parent 4f9a593 commit 5670d29
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/sugarcrm/connection/helper.rb
Expand Up @@ -46,5 +46,5 @@ def b64_encode(file)
def b64_decode(file) def b64_decode(file)
Base64.decode64(file) Base64.decode64(file)
end end

end; end end; end
19 changes: 15 additions & 4 deletions lib/sugarcrm/connection/request.rb
Expand Up @@ -22,14 +22,14 @@ def initialize(url, method, json, debug=false)
end end


def escape(json) def escape(json)
# BUG: SugarCRM doesn't properly handle '"' inside of JSON for some reason. Let's convert it back just in case. # BUG: SugarCRM doesn't properly handle '"' inside of JSON for some reason. Let's unescape any html elements.
j = CGI.unescapeHTML(json) j = convert_reserved_characters(json)
# Now we convert everything else. # Now we escape the resulting string.
j = CGI.escape(j) j = CGI.escape(j)
j j
end end


# TODO: Fix this so that it parses. # TODO: Fix this so that it JSON.parse will consume it.
def unescape def unescape
j = CGI.unescape(@json) j = CGI.unescape(@json)
j.gsub!(/\n/, '') j.gsub!(/\n/, '')
Expand All @@ -46,4 +46,15 @@ def length
def to_s def to_s
@request @request
end 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 end; end
2 changes: 1 addition & 1 deletion test/test_module.rb
Expand Up @@ -7,7 +7,7 @@ class TestModule < ActiveSupport::TestCase
end end


should "return required fields when #required_fields" do 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 end


# TODO: Figure out a way to test this. # TODO: Figure out a way to test this.
Expand Down
2 changes: 1 addition & 1 deletion test/test_request.rb
Expand Up @@ -27,7 +27,7 @@ class TestRequest < ActiveSupport::TestCase
end end


should "properly escape JSON" do 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


end end
Expand Down
3 changes: 2 additions & 1 deletion test/test_sugarcrm.rb
Expand Up @@ -192,7 +192,8 @@ class TestSugarCRM < ActiveSupport::TestCase
assert !SugarCRM::User.first.blank? assert !SugarCRM::User.first.blank?
end end
end end


# TODO: Fix this test so it creates the Note properly before asserting.
context "A SugarCRM::Note instance" do context "A SugarCRM::Note instance" do
should "return the correct parent record with the `parent` method" do should "return the correct parent record with the `parent` method" do
note = SugarCRM::Note.first note = SugarCRM::Note.first
Expand Down

0 comments on commit 5670d29

Please sign in to comment.