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
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

@ -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

Loading…
Cancel
Save