-
-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Extends #787] - Improve json conversion with tests and support for R…
…ails older than 4.1 (#812) * Improve json_safe_and_pretty method to handle parse error * Add tests to json_safe_and_pretty method * Add old json escape method for Rails versions less than 4 * Add json safe and pretty argument class test * Add rails version less than method * Add tests for json encoding helper methods * Move rails_version_less_than method into Utils module * Extract json escaping logic into JsonOutput class * Remove "or equal" logic from rails_version_less_than util method * Use monkey patch json escape from Rails 4.2 instead of Rails 4 * Memoize result for static method rails_version_less_than * Update json escaping method into class methods
- Loading branch information
Showing
7 changed files
with
202 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require "active_support/core_ext/string/output_safety" | ||
|
||
module ReactOnRails | ||
class JsonOutput | ||
ESCAPE_REPLACEMENT = { | ||
"&" => '\u0026', | ||
">" => '\u003e', | ||
"<" => '\u003c', | ||
"\u2028" => '\u2028', | ||
"\u2029" => '\u2029' | ||
}.freeze | ||
ESCAPE_REGEXP = /[\u2028\u2029&><]/u | ||
|
||
def self.escape(json) | ||
return escape_without_erb_util(json) if Utils.rails_version_less_than_4_1_1 | ||
|
||
ERB::Util.json_escape(json) | ||
end | ||
|
||
def self.escape_without_erb_util(json) | ||
# https://github.com/rails/rails/blob/60257141462137331387d0e34931555cf0720886/activesupport/lib/active_support/core_ext/string/output_safety.rb#L113 | ||
|
||
json.to_s.gsub(ESCAPE_REGEXP, ESCAPE_REPLACEMENT) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require_relative "spec_helper" | ||
require "react_on_rails/json_output" | ||
|
||
module ReactOnRails | ||
describe JsonOutput do | ||
let(:hash_value) do | ||
{ | ||
simple: "hello world", | ||
special: '<>&\u2028\u2029' | ||
} | ||
end | ||
|
||
let(:escaped_json) do | ||
'{"simple":"hello world","special":"\\u003c\\u003e\\u0026\\\\u2028\\\\u2029"}' | ||
end | ||
|
||
shared_examples :escaped_json do | ||
it "returns a well-formatted json with escaped characters" do | ||
expect(subject).to eq(escaped_json) | ||
end | ||
end | ||
|
||
describe ".escape" do | ||
subject { described_class.escape(hash_value.to_json) } | ||
|
||
context "with Rails version 4.1.1 and higher" do | ||
before { allow(Rails).to receive(:version).and_return("4.1.1") } | ||
|
||
it_behaves_like :escaped_json | ||
end | ||
|
||
context "with Rails version lower than 4.1.1" do | ||
before { allow(Rails).to receive(:version).and_return("4.1.0") } | ||
|
||
it_behaves_like :escaped_json | ||
end | ||
end | ||
|
||
describe ".escaped_without_erb_utils" do | ||
subject { described_class.escape_without_erb_util(hash_value.to_json) } | ||
|
||
it_behaves_like :escaped_json | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
require_relative "spec_helper" | ||
|
||
module ReactOnRails | ||
RSpec.describe Utils do | ||
subject { Utils.rails_version_less_than("4") } | ||
|
||
describe ".rails_version_less_than" do | ||
before(:each) { Utils.instance_variable_set :@rails_version_less_than, nil } | ||
|
||
context "with Rails 3" do | ||
before { allow(Rails).to receive(:version).and_return("3") } | ||
|
||
it { expect(subject).to eq(true) } | ||
end | ||
|
||
context "with Rails 3.2" do | ||
before { allow(Rails).to receive(:version).and_return("3.2") } | ||
|
||
it { expect(subject).to eq(true) } | ||
end | ||
|
||
context "with Rails 4" do | ||
before { allow(Rails).to receive(:version).and_return("4") } | ||
|
||
it { expect(subject).to eq(false) } | ||
end | ||
|
||
context "with Rails 4.2" do | ||
before { allow(Rails).to receive(:version).and_return("4.2") } | ||
|
||
it { expect(subject).to eq(false) } | ||
end | ||
|
||
context "with Rails 10.0" do | ||
before { allow(Rails).to receive(:version).and_return("10.0") } | ||
|
||
it { expect(subject).to eq(false) } | ||
end | ||
|
||
context "called twice" do | ||
before do | ||
allow(Rails).to receive(:version).and_return("4.2") | ||
end | ||
|
||
it "should memoize the result" do | ||
2.times { Utils.rails_version_less_than("4") } | ||
|
||
expect(Rails).to have_received(:version).once | ||
end | ||
end | ||
end | ||
|
||
describe ".rails_version_less_than_4_1_1" do | ||
subject { Utils.rails_version_less_than_4_1_1 } | ||
|
||
before(:each) { Utils.instance_variable_set :@rails_version_less_than, nil } | ||
|
||
context "with Rails 4.1.0" do | ||
before { allow(Rails).to receive(:version).and_return("4.1.0") } | ||
|
||
it { expect(subject).to eq(true) } | ||
end | ||
|
||
context "with Rails 4.1.1" do | ||
before { allow(Rails).to receive(:version).and_return("4.1.1") } | ||
|
||
it { expect(subject).to eq(false) } | ||
end | ||
end | ||
end | ||
end |