From 4df387d47502f50e6fb15ada56670739895388c4 Mon Sep 17 00:00:00 2001 From: Thibaut Date: Sat, 16 Nov 2013 11:42:44 +0100 Subject: [PATCH] Make Docs::URL#subpath_to work with unqualified URLs --- lib/docs/core/url.rb | 2 +- test/lib/docs/core/url_test.rb | 134 ++++++++++++++++++--------------- 2 files changed, 74 insertions(+), 62 deletions(-) diff --git a/lib/docs/core/url.rb b/lib/docs/core/url.rb index aa897b25..511976e6 100644 --- a/lib/docs/core/url.rb +++ b/lib/docs/core/url.rb @@ -59,7 +59,7 @@ module Docs end def subpath_to(url, options = nil) - assert_absolute self, url = self.class.parse(url) + url = self.class.parse(url) return unless origin == url.origin base = path diff --git a/test/lib/docs/core/url_test.rb b/test/lib/docs/core/url_test.rb index 1daee310..348ea94d 100644 --- a/test/lib/docs/core/url_test.rb +++ b/test/lib/docs/core/url_test.rb @@ -79,131 +79,143 @@ class DocsUrlTest < MiniTest::Spec end describe "#subpath_to" do - context "when the URL is relative" do + context "when the URL is '/'" do let :url do URL.parse '/' end - it "raises an error with a relative url" do - assert_raises(ArgumentError) { url.subpath_to '/' } + it "returns nil with ''" do + assert_nil url.subpath_to('') end - it "raises an error with an absolute url" do - assert_raises(ArgumentError) { url.subpath_to 'http://example.com' } + it "returns '' with '/'" do + assert_equal '', url.subpath_to('/') end - end - context "when the URL is 'http://example.com'" do - let :url do - URL.parse 'http://example.com' + it "returns 'path' with '/path'" do + assert_equal 'path', url.subpath_to('/path') end - it "raises an error with a relative url" do - assert_raises(ArgumentError) { url.subpath_to '/' } + it "returns nil with 'path'" do + assert_nil url.subpath_to('path') end - it "returns '' with 'http://example.com'" do - assert_equal '', url.subpath_to('http://example.com') + it "returns nil with 'http://example.com/'" do + assert_nil url.subpath_to('http://example.com/') end + end - it "returns '' with 'HTTP://EXAMPLE.COM'" do - assert_equal '', url.subpath_to('HTTP://EXAMPLE.COM') + context "when the URL is '/path/to'" do + let :url do + URL.parse '/path/to' end - it "returns '/' with 'http://example.com/'" do - assert_equal '/', url.subpath_to('http://example.com/') + it "returns nil with '/path/'" do + assert_nil url.subpath_to('/path/') end - it "returns '/path' with 'http://example.com/path'" do - assert_equal '/path', url.subpath_to('http://example.com/path') + it "returns '' with '/path/to'" do + assert_equal '', url.subpath_to('/path/to') end - it "returns '/' with 'http://example.com/?query'" do - assert_equal '/', url.subpath_to('http://example.com/?query') + it "returns '/file' with '/path/to/file'" do + assert_equal '/file', url.subpath_to('/path/to/file') end - it "returns '/' with 'http://example.com/#frag'" do - assert_equal '/', url.subpath_to('http://example.com/#frag') + it "returns nil with 'path/to/file'" do + assert_nil url.subpath_to('path/to/file') end - it "returns nil with 'https://example.com/'" do - assert_nil url.subpath_to('https://example.com/') + it "returns nil with '/path/tofile'" do + assert_nil url.subpath_to('/path/tofile') end - it "returns nil with 'http://not.example.com/'" do - assert_nil url.subpath_to('http://not.example.com/') + it "returns nil with '/PATH/to/file'" do + assert_nil url.subpath_to('/PATH/to/file') + end + + context "and :ignore_case is true" do + it "returns '/file' with '/PATH/to/file'" do + assert_equal '/file', url.subpath_to('/PATH/to/file', ignore_case: true) + end end end - context "when the URL is 'http://example.com/'" do + context "when the URL is '/path/to/'" do let :url do - URL.parse 'http://example.com/' + URL.parse '/path/to/' end - it "returns nil with 'http://example.com'" do - assert_equal nil, url.subpath_to('http://example.com') + it "returns nil with '/path/to'" do + assert_nil url.subpath_to('/path/to') end - it "returns '' with 'http://example.com/'" do - assert_equal '', url.subpath_to('http://example.com/') + it "returns 'file' with '/path/to/file'" do + assert_equal 'file', url.subpath_to('/path/to/file') end end - context "when the URL is 'http://example.com/path/to'" do + context "when the URL is 'path/to'" do let :url do - URL.parse 'http://example.com/path/to' + URL.parse 'path/to' end - it "returns nil with 'http://example.com'" do - assert_nil url.subpath_to('http://example.com') + it "returns nil with '/path/to'" do + assert_nil url.subpath_to('/path/to') end - it "returns nil with 'http://example.com/'" do - assert_nil url.subpath_to('http://example.com/') + it "returns '/file' with 'path/to/file'" do + assert_equal '/file', url.subpath_to('path/to/file') + end + end + + context "when the URL is 'http://example.com'" do + let :url do + URL.parse 'http://example.com' end - it "returns nil with 'http://example.com/path/'" do - assert_nil url.subpath_to('http://example.com/path/') + it "returns '' with 'HTTP://EXAMPLE.COM'" do + assert_equal '', url.subpath_to('HTTP://EXAMPLE.COM') end - it "returns '' with 'http://example.com/path/to'" do - assert_equal '', url.subpath_to('http://example.com/path/to') + it "returns '/path' with 'http://example.com/path?query#frag'" do + assert_equal '/path', url.subpath_to('http://example.com/path?query#frag') end - it "returns '/file' with 'http://example.com/path/to/file'" do - assert_equal '/file', url.subpath_to('http://example.com/path/to/file') + it "returns nil with 'https://example.com/'" do + assert_nil url.subpath_to('https://example.com/') end - it "returns nil with 'http://example.com/path/tofile'" do - assert_nil url.subpath_to('http://example.com/path/tofile') + it "returns nil with 'http://not.example.com/'" do + assert_nil url.subpath_to('http://not.example.com/') end + end - it "returns nil with 'http://example.com/PATH/to/file'" do - assert_nil url.subpath_to('http://example.com/PATH/to/file') + context "when the URL is 'http://example.com/'" do + let :url do + URL.parse 'http://example.com/' end - context "and :ignore_case is true" do - it "returns '/file' with 'http://example.com/PATH/to/file'" do - assert_equal '/file', url.subpath_to('http://example.com/PATH/to/file', ignore_case: true) - end + it "returns nil with 'http://example.com'" do + assert_equal nil, url.subpath_to('http://example.com') end end - context "when the URL is 'http://example.com/path/to/'" do + context "when the URL is 'http://example.com/path/to'" do let :url do - URL.parse 'http://example.com/path/to/' + URL.parse 'http://example.com/path/to' end - it "returns nil with 'http://example.com/path/to'" do - assert_nil url.subpath_to('http://example.com/path/to') + it "returns '/file' with 'http://example.com/path/to/file'" do + assert_equal '/file', url.subpath_to('http://example.com/path/to/file') end - it "returns '' with 'http://example.com/path/to/'" do - assert_equal '', url.subpath_to('http://example.com/path/to/') + it "returns nil with 'http://example.com/path/tofile'" do + assert_nil url.subpath_to('http://example.com/path/tofile') end - it "returns 'file' with 'http://example.com/path/to/file'" do - assert_equal 'file', url.subpath_to('http://example.com/path/to/file') + it "returns nil with '/path/to/file'" do + assert_nil url.subpath_to('/path/tofile') end end end