From df612da2f648fe2524542dc6aa67a83925d26b43 Mon Sep 17 00:00:00 2001 From: 417-72KI <417.72ki@gmail.com> Date: Fri, 9 Jul 2021 16:40:44 +0900 Subject: [PATCH 1/4] Add matcher "hasJsonBody" with array --- .../OHHTTPStubsSwift/OHHTTPStubsSwift.swift | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Sources/OHHTTPStubsSwift/OHHTTPStubsSwift.swift b/Sources/OHHTTPStubsSwift/OHHTTPStubsSwift.swift index 66554ec7..aba74df4 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 + * - Parameter jsonArray: the JSON array to expect + * + * - Returns: a matcher that returns true if the `NSURLRequest`'s body contains a JSON array with the values 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 From 68c90cd990ca5b940405d7bc355176e74935423a Mon Sep 17 00:00:00 2001 From: 417-72KI <417.72ki@gmail.com> Date: Fri, 9 Jul 2021 17:11:32 +0900 Subject: [PATCH 2/4] Add test cases --- .../SwiftHelpersTests.swift | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) 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() { From 9baed52671408cc8f43bdc34fa07349b7fb970e5 Mon Sep 17 00:00:00 2001 From: 417-72KI <417.72ki@gmail.com> Date: Fri, 9 Jul 2021 17:59:22 +0900 Subject: [PATCH 3/4] fix doc comment --- Sources/OHHTTPStubsSwift/OHHTTPStubsSwift.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/OHHTTPStubsSwift/OHHTTPStubsSwift.swift b/Sources/OHHTTPStubsSwift/OHHTTPStubsSwift.swift index aba74df4..7204835b 100644 --- a/Sources/OHHTTPStubsSwift/OHHTTPStubsSwift.swift +++ b/Sources/OHHTTPStubsSwift/OHHTTPStubsSwift.swift @@ -427,10 +427,10 @@ public func hasJsonBody(_ jsonObject: [AnyHashable : Any]) -> HTTPStubsTestBlock #endif /** - * Matcher testing that the `NSURLRequest` body contains a JSON array with the same values + * 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 values as the parameter value + * - 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 { From 1f4d6fc7cb5bfa7b10cda0c565ad4142ea66fd08 Mon Sep 17 00:00:00 2001 From: 417-72KI <417.72ki@gmail.com> Date: Fri, 9 Jul 2021 17:59:31 +0900 Subject: [PATCH 4/4] Update CHANGELOG --- CHANGELOG.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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)