Skip to content

Commit

Permalink
Merge pull request #160 from hq-mobile/swift-header-matchers
Browse files Browse the repository at this point in the history
Added 2 matchers for headers
  • Loading branch information
AliSoftware committed Mar 15, 2016
2 parents 6d305e8 + 2c13a98 commit a97ebe7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# OHHTTPStubs — CHANGELOG

## Master

* Added matchers that check whether a request has a particular header present, and a matcher to check if a request has a header with a key and value.
[@hq-mobile](https://github.com/hq-mobile), [#160](https://github.com/AliSoftware/OHHTTPStubs/pull/160)

## [4.8.0](https://github.com/AliSoftware/OHHTTPStubs/releases/tag/4.8.0)

* Added `isEnabled` and `isEnabledForSessionConfiguration` getter methods.
Expand Down
24 changes: 24 additions & 0 deletions OHHTTPStubs/Sources/Swift/OHHTTPStubsSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,31 @@ public func containsQueryParams(params: [String:String?]) -> OHHTTPStubsTestBloc
}
}

/**
* Matcher testing that the `NSURLRequest` headers contain a specific key
* - Parameter name: the name of the key to search for in the `NSURLRequest`'s **allHTTPHeaderFields** property
*
* - Returns: a matcher that returns true if the `NSURLRequest`'s headers contain a value for the key name
*/
public func hasHeaderNamed(name: String) -> OHHTTPStubsTestBlock {
return { (req: NSURLRequest) -> Bool in
return req.valueForHTTPHeaderField(name) != nil
}
}

/**
* Matcher testing that the `NSURLRequest` headers contain a specific key and the key's value is equal to the parameter value
* - Parameter name: the name of the key to search for in the `NSURLRequest`'s **allHTTPHeaderFields** property
* - Parameter value: the value to compare against the header's value
*
* - Returns: a matcher that returns true if the `NSURLRequest`'s headers contain a value for the key name and it's value
* is equal to the parameter value
*/
public func hasHeaderNamed(name: String, value: String) -> OHHTTPStubsTestBlock {
return { (req: NSURLRequest) -> Bool in
return req.valueForHTTPHeaderField(name) == value
}
}

// MARK: Operators on OHHTTPStubsTestBlock

Expand Down
43 changes: 43 additions & 0 deletions OHHTTPStubs/UnitTests/Test Suites/SwiftHelpersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,49 @@ class SwiftHelpersTests : XCTestCase {
XCTAssert(matcher(req) == result, "containsQueryParams(\"\(params)\") matcher failed when testing url \(url)")
}
}

func testHasHeaderNamedIsTrue() {
let req = NSMutableURLRequest(URL: NSURL(string: "foo://bar")!)
req.addValue("1234567890", forHTTPHeaderField: "ArbitraryKey")

let hasHeader = hasHeaderNamed("ArbitraryKey")(req)

XCTAssertTrue(hasHeader)
}

func testHasHeaderNamedIsFalse() {
let req = NSMutableURLRequest(URL: NSURL(string: "foo://bar")!)

let hasHeader = hasHeaderNamed("ArbitraryKey")(req)

XCTAssertFalse(hasHeader)
}

func testHeaderValueForKeyEqualsIsTrue() {
let req = NSMutableURLRequest(URL: NSURL(string: "foo://bar")!)
req.addValue("bar", forHTTPHeaderField: "foo")

let matchesHeader = hasHeaderNamed("foo", value: "bar")(req)

XCTAssertTrue(matchesHeader)
}

func testHeaderValueForKeyEqualsIsFalse() {
let req = NSMutableURLRequest(URL: NSURL(string: "foo://bar")!)
req.addValue("bar", forHTTPHeaderField: "foo")

let matchesHeader = hasHeaderNamed("foo", value: "baz")(req)

XCTAssertFalse(matchesHeader)
}

func testHeaderValueForKeyEqualsDoesNotExist() {
let req = NSMutableURLRequest(URL: NSURL(string: "foo://bar")!)

let matchesHeader = hasHeaderNamed("foo", value: "baz")(req)

XCTAssertFalse(matchesHeader)
}

let sampleURLs = [
// Absolute URLs
Expand Down

0 comments on commit a97ebe7

Please sign in to comment.