From cb918e2dd1cf86a9f1a56d86c83d77e336ea5aed Mon Sep 17 00:00:00 2001 From: Jessica Lynn Suttles Date: Mon, 10 Mar 2014 12:26:24 -0700 Subject: [PATCH] Add specs for Microformats2::AbsoluteUri --- lib/microformats2/absolute_uri.rb | 2 +- spec/lib/microformats2/absolute_uri_spec.rb | 73 +++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 spec/lib/microformats2/absolute_uri_spec.rb diff --git a/lib/microformats2/absolute_uri.rb b/lib/microformats2/absolute_uri.rb index 13eafad..a28e0d0 100644 --- a/lib/microformats2/absolute_uri.rb +++ b/lib/microformats2/absolute_uri.rb @@ -19,7 +19,7 @@ module Microformats2 uri.normalize! uri.to_s - rescue URI::InvalidURIError => e + rescue URI::BadURIError, URI::InvalidURIError => e relative.to_s end end diff --git a/spec/lib/microformats2/absolute_uri_spec.rb b/spec/lib/microformats2/absolute_uri_spec.rb new file mode 100644 index 0000000..4aa8a08 --- /dev/null +++ b/spec/lib/microformats2/absolute_uri_spec.rb @@ -0,0 +1,73 @@ +require "spec_helper" +require "microformats2/absolute_uri" + +describe Microformats2::AbsoluteUri do + let(:subject) { Microformats2::AbsoluteUri } + + describe "#absolutize" do + context "when relative is nil" do + it "returns nil" do + base = nil + relative = nil + expect(subject.new(base, relative).absolutize).to be_nil + end + end + + context "when relative is an empty string" do + it "returns nil" do + base = nil + relative = "" + expect(subject.new(base, relative).absolutize).to be_nil + end + end + + context "when relative is a valid URI" do + context "and relative is absolute" do + it "returns normalized relative" do + base = nil + relative = "http://google.com" + result = "http://google.com/" + expect(subject.new(base, relative).absolutize).to eq result + end + end + + context "and relative is not absolute" do + context "and base is present but not absolute" do + it "returns normalized relative" do + base = "foo" + relative = "bar/qux" + result = "bar/qux" + expect(subject.new(base, relative).absolutize).to eq result + end + end + + context "and base is present and absolute" do + it "returns normalized base and relative joined" do + base = "http://google.com" + relative = "foo/bar" + result = "http://google.com/foo/bar" + expect(subject.new(base, relative).absolutize).to eq result + end + end + + context "and base is not present" do + it "returns normalized relative" do + base = nil + relative = "foo/bar" + result = "foo/bar" + expect(subject.new(base, relative).absolutize).to eq result + end + end + end + end + + context "when relative in an invliad URI" do + it "returns relative" do + base = nil + relative = "git@github.com:G5/microformats2.git" + result = "git@github.com:G5/microformats2.git" + expect(subject.new(base, relative).absolutize).to eq result + end + end + end +end