Permalink
Browse files

properly encode &'s in tag-values and about-values

  • Loading branch information...
1 parent 1769048 commit df25e9fcdc419c41b648b35e345c8d5a77fd4c47 @gridaphobe committed Jun 9, 2011
Showing with 36 additions and 9 deletions.
  1. +1 −1 .rvmrc
  2. +13 −7 lib/fluidinfo.rb
  3. +22 −1 test/test_fluidinfo.rb
View
@@ -1 +1 @@
-1.9.2@fluidinfo.rb
+rvm 1.9.2@fluidinfo.rb
View
@@ -1,5 +1,6 @@
require "rest-client"
require "uri"
+require "cgi"
require "yajl/json_gem"
require "base64"
require "set"
@@ -13,15 +14,15 @@ module Fluidinfo
class Client
# The main fluidinfo instance.
- @@MAIN = 'https://fluiddb.fluidinfo.com'
+ MAIN = 'https://fluiddb.fluidinfo.com'
# The sandbox instance, test your code here.
- @@SANDBOX = 'https://sandbox.fluidinfo.com'
+ SANDBOX = 'https://sandbox.fluidinfo.com'
def initialize(instance=:main)
if instance == :sandbox
- @instance = @@SANDBOX
+ @instance = SANDBOX
else
- @instance = @@MAIN
+ @instance = MAIN
end
@headers = {
:accept => "*/*",
@@ -119,12 +120,17 @@ def build_url(path, options={})
arr << "tag=#{tag}"
end
else
- arr << "#{key}=#{val}"
+ arr << "#{key}=#{CGI.escape val.to_s}"
end
- arr
+ arr
end.join('&')
+ # fix for /about API
+ if path.start_with? '/about/'
+ about = path[7..-1]
+ path = "/about/#{CGI.escape about}"
+ end
if args != ''
- URI.escape "#{@instance}#{path}?#{args}"
+ "#{@instance}#{path}?#{args}"
else
"#{@instance}#{path}"
end
@@ -175,7 +175,7 @@ class FluidinfoTest < Test::Unit::TestCase
'indexed' => false
}
resp = @fluid.post "/tags/test/#{new_ns}", :body => tag_body
- file = File.new(__FILE__)
+ file = File.new(__FILE__).read
path = "/objects/#{ns_id}/test/#{new_ns}/#{new_tag}"
resp = @fluid.put path, :body => file, :mime => "text/ruby"
assert_equal File.new(__FILE__).read, @fluid.get(path)
@@ -200,8 +200,29 @@ class FluidinfoTest < Test::Unit::TestCase
def @fluid.test_build_payload(*args)
build_payload(*args)
end
+ def @fluid.test_build_url(*args)
+ build_url(*args)
+ end
end
+ context "build_url" do
+ should "escape &'s in query tag-values" do
+ query = "test/tag=\"1&2\""
+ assert_equal "https://fluiddb.fluidinfo.com/objects?query=test%2Ftag%3D%221%262%22",
+ @fluid.test_build_url("/objects", :query => query)
+ query = "oreilly.com/title=\"HTML & CSS: The Good Parts\""
+ tags = ["oreilly.com/isbn"]
+ assert_equal "https://fluiddb.fluidinfo.com/values?query=oreilly.com%2Ftitle%3D%22HTML+%26+CSS%3A+The+Good+Parts%22&tag=oreilly.com/isbn",
+ @fluid.test_build_url("/values", :query => query, :tags => tags)
+ end
+
+ should "escape &'s in about-values" do
+ about = "tom & jerry"
+ assert_equal "https://fluiddb.fluidinfo.com/about/tom+%26+jerry",
+ @fluid.test_build_url('/about/tom & jerry')
+ end
+ end
+
context "build_payload" do
should "set proper mime-types" do
primitives = [1, 1.1, true, false, nil, ["1", "2", "hi"]]

0 comments on commit df25e9f

Please sign in to comment.