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

Added 2 matchers for headers #160

Merged
merged 2 commits into from Mar 15, 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
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