Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pathStartsWith(_:) to Swift helpers #163

Merged
merged 1 commit into from
Mar 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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