From f7e23a30424b9566c64dda9e83f27f005987a037 Mon Sep 17 00:00:00 2001 From: Samuel Duursma Date: Wed, 23 Mar 2016 16:56:44 +0000 Subject: [PATCH] Add pathStartsWith(_:) to Swift helpers --- CHANGELOG.md | 3 ++ .../Sources/Swift/OHHTTPStubsSwift.swift | 15 ++++++ .../Test Suites/SwiftHelpersTests.swift | 49 ++++++++++++++++++- 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fbf85905..ced9f723 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/OHHTTPStubs/Sources/Swift/OHHTTPStubsSwift.swift b/OHHTTPStubs/Sources/Swift/OHHTTPStubsSwift.swift index f10612b4..9bda9ff2 100644 --- a/OHHTTPStubs/Sources/Swift/OHHTTPStubsSwift.swift +++ b/OHHTTPStubs/Sources/Swift/OHHTTPStubsSwift.swift @@ -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**. * diff --git a/OHHTTPStubs/UnitTests/Test Suites/SwiftHelpersTests.swift b/OHHTTPStubs/UnitTests/Test Suites/SwiftHelpersTests.swift index e725e173..26749dbb 100644 --- a/OHHTTPStubs/UnitTests/Test Suites/SwiftHelpersTests.swift +++ b/OHHTTPStubs/UnitTests/Test Suites/SwiftHelpersTests.swift @@ -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) } @@ -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")