Skip to content

Commit

Permalink
Added matchers that check whether a request has a particular header p…
Browse files Browse the repository at this point in the history
…resent, and a matcher to check if a request has a header with a key and value.
  • Loading branch information
Dan committed Mar 15, 2016
1 parent 6d305e8 commit 4b7214c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
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 4b7214c

Please sign in to comment.