Skip to content

Commit

Permalink
Merge pull request #254 from victorg1991/master
Browse files Browse the repository at this point in the history
Add Absolute url matcher
  • Loading branch information
Liquidsoul authored Jun 12, 2017
2 parents 681607f + 54bbc4f commit 499c505
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# OHHTTPStubs — CHANGELOG

## Master
* Added absolute url matcher.
[@victorg1991](https://github.com/victorg1991)
[#254](https://github.com/AliSoftware/OHHTTPStubs/pull/254)
* Fixed up empty lines with whitespace inside test case classes.
[@mikelupo](https://github.com/mikelupo
[#251](https://github.com/AliSoftware/OHHTTPStubs/pull/251)
Expand Down
14 changes: 14 additions & 0 deletions OHHTTPStubs/Sources/Swift/OHHTTPStubsSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,20 @@ public func isMethodDELETE() -> OHHTTPStubsTestBlock {
return { $0.httpMethod == "DELETE" }
}

/**
* Matcher for testing an `NSURLRequest`'s **absolute url string**.
*
* e.g. the absolute url string is `https://api.example.com/signin?user=foo&password=123#anchor` in `https://api.example.com/signin?user=foo&password=123#anchor`
*
* - Parameter url: The absolute url string to match
*
* - Returns: a matcher (OHHTTPStubsTestBlock) that succeeds only if the request
* has the given absolute url
*/
public func isAbsoluteURLString(_ url: String) -> OHHTTPStubsTestBlock {
return { req in req.url?.absoluteString == url }
}

/**
* Matcher for testing an `NSURLRequest`'s **scheme**.
*
Expand Down
25 changes: 25 additions & 0 deletions OHHTTPStubs/UnitTests/Test Suites/SwiftHelpersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@ class SwiftHelpersTests : XCTestCase {
}
}

func testIsAbsoluteURLString() {
let matcher = isAbsoluteURLString("foo://foo/bar?param1=123&param2=foo#anchor")

let urls = [
"foo:": false,
"foo://foo/bar/baz": false,
"foo://foo/bar?param1=123&param2=foo#anchor": true,
"foo://foo/bar?param1=123&param2=foo": false,
"foo://foo/bar?param1=123": false,
"foo://foo/bar": false,
"bar://foo/bar": false,
"bar://foo/ba": false,
"foobar://": false
]

for (url, result) in urls {
#if swift(>=3.0)
let req = URLRequest(url: URL(string: url)!)
#else
let req = NSURLRequest(URL: NSURL(string: url)!)
#endif
XCTAssert(matcher(req) == result, "isAbsoluteURLString(\"foo://foo/bar?param1=123&param2=foo#anchor\") matcher failed when testing url \(url)")
}
}

func testIsScheme() {
let matcher = isScheme("foo")

Expand Down

0 comments on commit 499c505

Please sign in to comment.