Permalink
Browse files

Catch exception on url-decode and return nil instead of bubbling exce…

…ption
  • Loading branch information...
1 parent 9149d47 commit ef089b9cce37079100bec71447d6bc860e67faf6 @abedra abedra committed with weavejester Jun 22, 2011
Showing with 8 additions and 3 deletions.
  1. +3 −1 ring-core/src/ring/util/codec.clj
  2. +5 −2 ring-core/test/ring/util/codec_test.clj
View
4 ring-core/src/ring/util/codec.clj
@@ -14,7 +14,9 @@
"Returns the form-url-decoded version of the given string, using either a
specified encoding or UTF-8 by default."
[encoded & [encoding]]
- (URLDecoder/decode encoded (or encoding "UTF-8")))
+ (try
+ (URLDecoder/decode encoded (or encoding "UTF-8"))
+ (catch Exception e nil)))
(defn base64-encode
"Encode an array of bytes into a base64 encoded string."
View
7 ring-core/test/ring/util/codec_test.clj
@@ -8,8 +8,11 @@
(is (= "foo%FE%FF%00%2Fbar") (url-encode "foo/bar" "UTF-16")))
(deftest test-url-decode
- (is (= "foo/bar" (url-decode "foo%2Fbar")))
- (is (= "foo/bar" (url-decode "foo%FE%FF%00%2Fbar" "UTF-16"))))
+ (testing "standard behavior"
+ (is (= "foo/bar" (url-decode "foo%2Fbar")))
+ (is (= "foo/bar" (url-decode "foo%FE%FF%00%2Fbar" "UTF-16"))))
+ (testing "returns nil when underlying Java methods throw an exception"
+ (is (nil? (url-decode "%")))))
(deftest test-base64-encoding
(let [str-bytes (.getBytes "foo?/+" "UTF-8")]

0 comments on commit ef089b9

Please sign in to comment.