Skip to content

Commit

Permalink
Merge pull request #163 from sdduursma/add-path-starts-with
Browse files Browse the repository at this point in the history
Add pathStartsWith(_:) to Swift helpers
  • Loading branch information
AliSoftware committed Mar 28, 2016
2 parents a8b64f1 + f7e23a3 commit 23431d5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Master

* Added `pathStartsWith(_:)` to the `Swift` helpers.
[@sdduursma](https://github.com/sdduursma), [#163](https://github.com/AliSoftware/OHHTTPStubs/pull/163)

* Added more logging blocks for debugging and better insight into when OHHTTPStubs returns stubs and redirects.
[@jzucker2](https://github.com/jzucker2), [#161](https://github.com/AliSoftware/OHHTTPStubs/pull/161)

Expand Down
15 changes: 15 additions & 0 deletions OHHTTPStubs/Sources/Swift/OHHTTPStubsSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@ public func isPath(path: String) -> OHHTTPStubsTestBlock {
return { req in req.URL?.path == path }
}

/**
* Matcher for testing the start of an `NSURLRequest`'s **path**.
*
* - Parameter path: The path to match
*
* - Returns: a matcher (OHHTTPStubsTestBlock) that succeeds only if the request
* starts with the given path
*
* - Note: URL paths are usually absolute and thus starts with a '/' (which you
* should include in the `path` parameter unless you're testing relative URLs)
*/
public func pathStartsWith(path: String) -> OHHTTPStubsTestBlock {
return { req in req.URL?.path?.hasPrefix(path) ?? false }
}

/**
* Matcher for testing an `NSURLRequest`'s **path extension**.
*
Expand Down
49 changes: 47 additions & 2 deletions OHHTTPStubs/UnitTests/Test Suites/SwiftHelpersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ class SwiftHelpersTests : XCTestCase {
XCTAssert(matcher(req) == result, "isHost(\"foo\") matcher failed when testing url \(url)")
}
}

func testIsPath_absoluteURL() {
testIsPath("/foo/bar/baz", isAbsoluteMatcher: true)
}

func testIsPath_relativeURL() {
testIsPath("foo/bar/baz", isAbsoluteMatcher: false)
}
Expand Down Expand Up @@ -104,6 +104,51 @@ class SwiftHelpersTests : XCTestCase {
XCTAssert(matcher(req) == result, "isPath(\"\(path)\" matcher failed when testing url \(url)")
}
}

func testPathStartsWith_absoluteURL() {
testPathStartsWith("/foo/bar", isAbsoluteMatcher: true)
}

func testPathStartsWith_relativeURL() {
testPathStartsWith("foo/bar", isAbsoluteMatcher: false)
}

func testPathStartsWith(path: String, isAbsoluteMatcher: Bool) {
let matcher = pathStartsWith(path)

let urls = [
// Absolute URLs
"scheme:": false,
"scheme://": false,
"scheme://foo/bar/baz": false,
"scheme://host/foo/bar": isAbsoluteMatcher,
"scheme://host/foo/bar/baz": isAbsoluteMatcher,
"scheme://host/foo/bar?q=1": isAbsoluteMatcher,
"scheme://host/foo/bar#anchor": isAbsoluteMatcher,
"scheme://host/foo/bar;param": isAbsoluteMatcher,
"scheme://host/path/foo/bar/baz": false,
"scheme://host/path#/foo/bar/baz": false,
"scheme://host/path?/foo/bar/baz": false,
"scheme://host/path;/foo/bar/baz": false,
// Relative URLs
"foo/bar": !isAbsoluteMatcher,
"foo/bar/baz": !isAbsoluteMatcher,
"foo/bar?q=1": !isAbsoluteMatcher,
"foo/bar#anchor": !isAbsoluteMatcher,
"foo/bar;param": !isAbsoluteMatcher,
"path/foo/bar/baz": false,
"path#/foo/bar/baz": false,
"path?/foo/bar/baz": false,
"path;/foo/bar/baz": false,
]

for (url, result) in urls {
let req = NSURLRequest(URL: NSURL(string: url)!)
let p = req.URL?.path
print("URL: \(url) -> Path: \(p)")
XCTAssert(matcher(req) == result, "pathStartsWith(\"\(path)\" matcher failed when testing url \(url)")
}
}

func testIsExtension() {
let matcher = isExtension("txt")
Expand Down

0 comments on commit 23431d5

Please sign in to comment.