Skip to content

Commit

Permalink
properly encode &'s in tag-values and about-values
Browse files Browse the repository at this point in the history
  • Loading branch information
gridaphobe committed Jun 9, 2011
1 parent 1769048 commit df25e9f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .rvmrc
@@ -1 +1 @@
1.9.2@fluidinfo.rb
rvm 1.9.2@fluidinfo.rb
20 changes: 13 additions & 7 deletions lib/fluidinfo.rb
@@ -1,5 +1,6 @@
require "rest-client"
require "uri"
require "cgi"
require "yajl/json_gem"
require "base64"
require "set"
Expand All @@ -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 => "*/*",
Expand Down Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion test/test_fluidinfo.rb
Expand Up @@ -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)
Expand All @@ -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"]]
Expand Down

0 comments on commit df25e9f

Please sign in to comment.