Make Docs::URL#subpath_to work with unqualified URLs

pull/15/head
Thibaut 11 years ago
parent 258c97e387
commit 4df387d475

@ -59,7 +59,7 @@ module Docs
end end
def subpath_to(url, options = nil) def subpath_to(url, options = nil)
assert_absolute self, url = self.class.parse(url) url = self.class.parse(url)
return unless origin == url.origin return unless origin == url.origin
base = path base = path

@ -79,131 +79,143 @@ class DocsUrlTest < MiniTest::Spec
end end
describe "#subpath_to" do describe "#subpath_to" do
context "when the URL is relative" do context "when the URL is '/'" do
let :url do let :url do
URL.parse '/' URL.parse '/'
end end
it "raises an error with a relative url" do it "returns nil with ''" do
assert_raises(ArgumentError) { url.subpath_to '/' } assert_nil url.subpath_to('')
end end
it "raises an error with an absolute url" do it "returns '' with '/'" do
assert_raises(ArgumentError) { url.subpath_to 'http://example.com' } assert_equal '', url.subpath_to('/')
end end
it "returns 'path' with '/path'" do
assert_equal 'path', url.subpath_to('/path')
end end
context "when the URL is 'http://example.com'" do it "returns nil with 'path'" do
let :url do assert_nil url.subpath_to('path')
URL.parse 'http://example.com'
end end
it "raises an error with a relative url" do it "returns nil with 'http://example.com/'" do
assert_raises(ArgumentError) { url.subpath_to '/' } assert_nil url.subpath_to('http://example.com/')
end
end end
it "returns '' with 'http://example.com'" do context "when the URL is '/path/to'" do
assert_equal '', url.subpath_to('http://example.com') let :url do
URL.parse '/path/to'
end end
it "returns '' with 'HTTP://EXAMPLE.COM'" do it "returns nil with '/path/'" do
assert_equal '', url.subpath_to('HTTP://EXAMPLE.COM') assert_nil url.subpath_to('/path/')
end end
it "returns '/' with 'http://example.com/'" do it "returns '' with '/path/to'" do
assert_equal '/', url.subpath_to('http://example.com/') assert_equal '', url.subpath_to('/path/to')
end end
it "returns '/path' with 'http://example.com/path'" do it "returns '/file' with '/path/to/file'" do
assert_equal '/path', url.subpath_to('http://example.com/path') assert_equal '/file', url.subpath_to('/path/to/file')
end end
it "returns '/' with 'http://example.com/?query'" do it "returns nil with 'path/to/file'" do
assert_equal '/', url.subpath_to('http://example.com/?query') assert_nil url.subpath_to('path/to/file')
end end
it "returns '/' with 'http://example.com/#frag'" do it "returns nil with '/path/tofile'" do
assert_equal '/', url.subpath_to('http://example.com/#frag') assert_nil url.subpath_to('/path/tofile')
end end
it "returns nil with 'https://example.com/'" do it "returns nil with '/PATH/to/file'" do
assert_nil url.subpath_to('https://example.com/') assert_nil url.subpath_to('/PATH/to/file')
end end
it "returns nil with 'http://not.example.com/'" do context "and :ignore_case is true" do
assert_nil url.subpath_to('http://not.example.com/') it "returns '/file' with '/PATH/to/file'" do
assert_equal '/file', url.subpath_to('/PATH/to/file', ignore_case: true)
end
end end
end end
context "when the URL is 'http://example.com/'" do context "when the URL is '/path/to/'" do
let :url do let :url do
URL.parse 'http://example.com/' URL.parse '/path/to/'
end end
it "returns nil with 'http://example.com'" do it "returns nil with '/path/to'" do
assert_equal nil, url.subpath_to('http://example.com') assert_nil url.subpath_to('/path/to')
end end
it "returns '' with 'http://example.com/'" do it "returns 'file' with '/path/to/file'" do
assert_equal '', url.subpath_to('http://example.com/') assert_equal 'file', url.subpath_to('/path/to/file')
end end
end end
context "when the URL is 'http://example.com/path/to'" do context "when the URL is 'path/to'" do
let :url do let :url do
URL.parse 'http://example.com/path/to' URL.parse 'path/to'
end end
it "returns nil with 'http://example.com'" do it "returns nil with '/path/to'" do
assert_nil url.subpath_to('http://example.com') assert_nil url.subpath_to('/path/to')
end end
it "returns nil with 'http://example.com/'" do it "returns '/file' with 'path/to/file'" do
assert_nil url.subpath_to('http://example.com/') assert_equal '/file', url.subpath_to('path/to/file')
end
end end
it "returns nil with 'http://example.com/path/'" do context "when the URL is 'http://example.com'" do
assert_nil url.subpath_to('http://example.com/path/') let :url do
URL.parse 'http://example.com'
end end
it "returns '' with 'http://example.com/path/to'" do it "returns '' with 'HTTP://EXAMPLE.COM'" do
assert_equal '', url.subpath_to('http://example.com/path/to') assert_equal '', url.subpath_to('HTTP://EXAMPLE.COM')
end end
it "returns '/file' with 'http://example.com/path/to/file'" do it "returns '/path' with 'http://example.com/path?query#frag'" do
assert_equal '/file', url.subpath_to('http://example.com/path/to/file') assert_equal '/path', url.subpath_to('http://example.com/path?query#frag')
end end
it "returns nil with 'http://example.com/path/tofile'" do it "returns nil with 'https://example.com/'" do
assert_nil url.subpath_to('http://example.com/path/tofile') assert_nil url.subpath_to('https://example.com/')
end end
it "returns nil with 'http://example.com/PATH/to/file'" do it "returns nil with 'http://not.example.com/'" do
assert_nil url.subpath_to('http://example.com/PATH/to/file') assert_nil url.subpath_to('http://not.example.com/')
end
end end
context "and :ignore_case is true" do context "when the URL is 'http://example.com/'" do
it "returns '/file' with 'http://example.com/PATH/to/file'" do let :url do
assert_equal '/file', url.subpath_to('http://example.com/PATH/to/file', ignore_case: true) URL.parse 'http://example.com/'
end end
it "returns nil with 'http://example.com'" do
assert_equal nil, url.subpath_to('http://example.com')
end end
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 let :url do
URL.parse 'http://example.com/path/to/' URL.parse 'http://example.com/path/to'
end end
it "returns nil with 'http://example.com/path/to'" do it "returns '/file' with 'http://example.com/path/to/file'" do
assert_nil url.subpath_to('http://example.com/path/to') assert_equal '/file', url.subpath_to('http://example.com/path/to/file')
end end
it "returns '' with 'http://example.com/path/to/'" do it "returns nil with 'http://example.com/path/tofile'" do
assert_equal '', url.subpath_to('http://example.com/path/to/') assert_nil url.subpath_to('http://example.com/path/tofile')
end end
it "returns 'file' with 'http://example.com/path/to/file'" do it "returns nil with '/path/to/file'" do
assert_equal 'file', url.subpath_to('http://example.com/path/to/file') assert_nil url.subpath_to('/path/tofile')
end end
end end
end end

Loading…
Cancel
Save