Permalink
Browse files

Fix URI encoding bug

judge.js was being a little zealous in encoding the XHR validation URL. In fact,
it was being encoded in three different ways. This it bad for a number of
reasons, not least because it makes validating values such as email addresses
impossible.
  • Loading branch information...
1 parent 05882ec commit 13f0c60d7a2a7a9004913e24c4e6d495ee3a4762 @joecorcoran committed Mar 5, 2013
Showing with 11 additions and 7 deletions.
  1. +2 −2 app/assets/javascripts/judge.js
  2. +5 −2 lib/judge/controller.rb
  3. +4 −3 spec/javascripts/judge-spec.js
@@ -144,10 +144,10 @@
params = {
'klass' : classFromName(el.name),
'attribute': attrFromName(el.name),
- 'value' : encodeURIComponent(el.value),
+ 'value' : el.value,
'kind' : kind
};
- return encodeURI(path + queryString(params));
+ return path + queryString(params);
};
// Convert an object literal into an encoded query string.
View
@@ -1,3 +1,5 @@
+require 'uri'
+
module Judge
module Controller
@@ -19,9 +21,10 @@ def validation(params)
def normalize_validation_params(params)
params.keep_if { |key| %w{klass attribute value kind}.include?(key) }
- params[:klass] = find_klass(params[:klass])
+ params[:klass] = find_klass(params[:klass])
params[:attribute] = params[:attribute].to_sym
- params[:kind] = params[:kind].to_sym
+ params[:value] = URI.decode(params[:value])
+ params[:kind] = params[:kind].to_sym
params
end
@@ -395,8 +395,8 @@ describe('judge', function() {
var validation;
beforeEach(function() {
validator = _.bind(judge.eachValidators.uniqueness, el);
- el.value = 'foo';
- el.name = 'user[vehicle][registration]';
+ el.value = 'leader@team.com';
+ el.name = 'team[leader][email]';
});
it('returns a pending Validation', function() {
validation = validator({}, {});
@@ -411,7 +411,8 @@ describe('judge', function() {
server.respond();
});
runs(function() {
- expect(server.requests[0].url).toBe('/judge?klass=Vehicle&attribute=registration&value=foo&kind=uniqueness');
+ console.log(server.requests);
+ expect(server.requests[0].url).toBe('/judge?klass=Leader&attribute=email&value=leader%40team.com&kind=uniqueness');
});
});
it('closes Validation as valid if the server responds with an empty JSON array', function() {

0 comments on commit 13f0c60

Please sign in to comment.