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 hasJsonBody(_:) matcher for array #343

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

## Master

* Added `hasFormBody(_:)` matcher.
* Added `hasJsonBody(_:)` matcher for array.
[@417-72KI](https://github.com/417-72KI)

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

* Added `hasFormBody(_:)` matcher.
[@417-72KI](https://github.com/417-72KI)
* Added fix for Xcode 12 - Warnings related to iOS 8 support (Swift Package Manager) #328
[@kikeenrique](https://github.com/kikeenrique)
Expand Down
20 changes: 20 additions & 0 deletions Sources/OHHTTPStubsSwift/OHHTTPStubsSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,26 @@ public func hasJsonBody(_ jsonObject: [AnyHashable : Any]) -> HTTPStubsTestBlock
}
#endif

/**
* Matcher testing that the `NSURLRequest` body contains a JSON array with the same values on same order
* - Parameter jsonArray: the JSON array to expect
*
* - Returns: a matcher that returns true if the `NSURLRequest`'s body contains a JSON array with the same values on same order as the parameter value
*/
#if swift(>=3.0)
public func hasJsonBody(_ jsonArray: [Any]) -> HTTPStubsTestBlock {
return { req in
guard
let httpBody = req.ohhttpStubs_httpBody,
let jsonBody = (try? JSONSerialization.jsonObject(with: httpBody, options: [])) as? [Any]
else {
return false
}
return NSArray(array: jsonBody).isEqual(to: jsonArray)
}
}
#endif

#if swift(>=3.0)
/**
* Matcher testing that the `NSURLRequest` content-type is `application/x-www-form-urlencoded` and body contains a query parameter
Expand Down
48 changes: 48 additions & 0 deletions Tests/OHHTTPStubsSwiftTests/SwiftHelpersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,54 @@ class SwiftHelpersTests : XCTestCase {
}
#endif

#if swift(>=3.0)
func testHasJsonArrayBodyIsTrue() {
let jsonStringsAndObjects = [
// Exact match
("[\"foo\", \"bar\", \"baz\", 42, \"qux\", true]",
["foo", "bar", "baz", 42, "qux", true]),
// Newlines and indentations
("[\"foo\", \"bar\", \n\"baz\", 42, \"qux\", true]",
["foo", "bar", "baz", 42, "qux", true]),
// Nested objects
("[[ \"foo\", \"bar\", \"baz\" ], { \"qux\": true, \"quux\": [\"spam\", \"ham\", \"eggs\"] }]",
[["foo", "bar", "baz"], ["qux": true, "quux": ["spam", "ham", "eggs"]]]),
]

for (jsonString, expectedJsonObject) in jsonStringsAndObjects {
var req = URLRequest(url: URL(string: "foo://bar")!)
req.httpBody = jsonString.data(using: .utf8)
let matchesJsonBody = hasJsonBody(expectedJsonObject)(req)

XCTAssertTrue(matchesJsonBody)
}
}
#endif

#if swift(>=3.0)
func testHasJsonArrayBodyIsFalse() {
let jsonStringsAndObjects = [
// Changed value
("[ \"foo\", \"bar\" ]",
["foo", "qux"]),
// Changed order
("[ \"foo\", \"bar\" ]",
["bar", "foo"]),
// Nested objects with changed order
("[ { \"foo\": \"bar\", \"baz\": { \"qux\": true } }, { \"quux\": [ \"spam\", \"ham\", \"eggs\" ] } ]",
[["quux": ["spam", "ham", "eggs"]], ["foo": "bar", "baz": ["qux": true]]])
] as [(String, [Any])]

for (jsonString, expectedJsonObject) in jsonStringsAndObjects {
var req = URLRequest(url: URL(string: "foo://bar")!)
req.httpBody = jsonString.data(using: .utf8)
let matchesJsonBody = hasJsonBody(expectedJsonObject)(req)

XCTAssertFalse(matchesJsonBody)
}
}
#endif

#if swift(>=3.0)
@available(iOS 8.0, OSX 10.10, *)
func testHasFormBodyIsTrue() {
Expand Down