Spec: Test a url helper function (#428)

This commit is contained in:
Olle Jonsson 2016-10-01 19:23:48 +02:00 committed by GitHub
parent 5fde22461e
commit 828d639348
2 changed files with 18 additions and 8 deletions

View File

@ -79,7 +79,7 @@ module GitHubChangelogGenerator
last_response = client.last_response
if (last_pg = last_response.rels[:last])
parse_url_for_vars(last_pg.href)["page"].to_i
querystring_as_hash(last_pg.href)["page"].to_i
else
1
end
@ -307,16 +307,12 @@ Make sure, that you push tags to remote repo via 'git push --tags'"
"#{@options[:user]}/#{@options[:project]}"
end
# Parses a URI and returns a hash of all GET variables
# Returns Hash of all querystring variables in given URI.
#
# @param [String] uri eg. https://api.github.com/repositories/43914960/tags?page=37&foo=1
# @return [Hash] of all GET variables. eg. { 'page' => 37, 'foo' => 1 }
def parse_url_for_vars(uri)
URI(uri).query.split("&").each_with_object({}) do |get_var, params|
k, v = get_var.split("=")
params[k] = v
params
end
def querystring_as_hash(uri)
Hash[URI.decode_www_form(URI(uri).query || "")]
end
end
end

View File

@ -426,6 +426,20 @@ describe GitHubChangelogGenerator::OctoFetcher do
end
end
describe "#querystring_as_hash" do
it "works on the blank URL" do
expect { fetcher.send(:querystring_as_hash, "") }.not_to raise_error
end
it "where there are no querystring params" do
expect { fetcher.send(:querystring_as_hash, "http://example.com") }.not_to raise_error
end
it "returns a String-keyed Hash of querystring params" do
expect(fetcher.send(:querystring_as_hash, "http://example.com/o.html?str=18&wis=12")).to include("wis" => "12", "str" => "18")
end
end
describe "#fetch_commit" do
context "when API call is valid", :vcr do
it "returns commit" do