Skip to content

Commit b7d28f8

Browse files
authored
Merge pull request #126 from devxoul/url-contains-another-url
Fix a bug that pattern matches when the url contains another url
2 parents dfa7da7 + de3f61f commit b7d28f8

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

Sources/URLMatcher/URLMatcher.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,14 @@ open class URLMatcher {
128128
}
129129

130130
func stringPathComponents(from url: URLConvertible) -> [String] {
131-
return url.urlStringValue.components(separatedBy: "/").lazy
132-
.filter { !$0.isEmpty }
133-
.filter { !$0.hasSuffix(":") }
131+
return url.urlStringValue.components(separatedBy: "/").lazy.enumerated()
132+
.filter { index, component in !component.isEmpty }
133+
.filter { index, component in !self.isScheme(index, component) }
134+
.map { index, component in component }
135+
}
136+
137+
private func isScheme(_ index: Int, _ component: String) -> Bool {
138+
return index == 0 && component.hasSuffix(":")
134139
}
135140

136141
func pathComponents(from url: URLPattern) -> [URLPathComponent] {

Tests/URLMatcherTests/URLMatcherSpec.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ final class URLMatcherSpec: QuickSpec {
3838
expect(result).to(beNil())
3939
}
4040

41-
fit("returns a result for totally matching url") {
41+
it("returns a result for totally matching url") {
4242
let candidates = ["myapp://hello/<name>", "myapp://hello/world"]
4343
let result = matcher.match("myapp://hello/world", from: candidates)
4444
expect(result).notTo(beNil())
4545
expect(result?.pattern) == "myapp://hello/world"
4646
expect(result?.values.count) == 0
4747
}
4848

49-
fit("returns a result for totally matching url2") {
49+
it("returns a result for the longest matching url") {
5050
let candidates = ["myapp://<path:path>", "myapp://hello/<name>"]
5151
let result = matcher.match("myapp://hello/world", from: candidates)
5252
expect(result).notTo(beNil())
@@ -165,5 +165,11 @@ final class URLMatcherSpec: QuickSpec {
165165
let result2 = matcher.match("http://host/anything", from: candidates2)
166166
expect(result1?.pattern).to(equal(result2?.pattern))
167167
}
168+
169+
it("returns nil when there is anotehr url in the path (#123)") {
170+
let candidates = ["myapp://browser/<url>"]
171+
let result = matcher.match("myapp://browser/http://google.fr", from: candidates)
172+
expect(result).to(beNil())
173+
}
168174
}
169175
}

0 commit comments

Comments
 (0)