diff --git a/CHANGELOG.md b/CHANGELOG.md index a8daae70..25b91d76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/Sources/OHHTTPStubsSwift/OHHTTPStubsSwift.swift b/Sources/OHHTTPStubsSwift/OHHTTPStubsSwift.swift index 66554ec7..7204835b 100644 --- a/Sources/OHHTTPStubsSwift/OHHTTPStubsSwift.swift +++ b/Sources/OHHTTPStubsSwift/OHHTTPStubsSwift.swift @@ -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 diff --git a/Tests/OHHTTPStubsSwiftTests/SwiftHelpersTests.swift b/Tests/OHHTTPStubsSwiftTests/SwiftHelpersTests.swift index b2791491..38169dc8 100644 --- a/Tests/OHHTTPStubsSwiftTests/SwiftHelpersTests.swift +++ b/Tests/OHHTTPStubsSwiftTests/SwiftHelpersTests.swift @@ -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() {