diff --git a/Sources/PubNub/PubNub.swift b/Sources/PubNub/PubNub.swift index fdee2fa8..ef0d77dd 100644 --- a/Sources/PubNub/PubNub.swift +++ b/Sources/PubNub/PubNub.swift @@ -1077,7 +1077,7 @@ public extension PubNub { includeMeta: Bool = false, includeUUID: Bool = true, includeMessageType: Bool = true, - includeCustomMessageType: Bool = true, + includeCustomMessageType: Bool = false, page: PubNubBoundedPage? = PubNubBoundedPageBase(), custom requestConfig: RequestConfiguration = RequestConfiguration(), completion: ((Result<(messagesByChannel: [String: [PubNubMessage]], next: PubNubBoundedPage?), Error>) -> Void)? diff --git a/Tests/PubNubContractTest/Steps/History/PubNubHistoryContractTestSteps.swift b/Tests/PubNubContractTest/Steps/History/PubNubHistoryContractTestSteps.swift index 65dc8ff4..f23c987b 100644 --- a/Tests/PubNubContractTest/Steps/History/PubNubHistoryContractTestSteps.swift +++ b/Tests/PubNubContractTest/Steps/History/PubNubHistoryContractTestSteps.swift @@ -36,62 +36,42 @@ public class PubNubHistoryContractTestSteps: PubNubContractTestCase { self.wait(for: [historyExpect], timeout: 60.0) } - - Match(["And"], "^history response contains messages (with|without) ('(.*)' and '(.*)') message types$") { args, _ in + + Match(["And"], "^history response contains messages (with|without) (?:customMessageType|'([^']*)' and '([^']*)' (?:message )?types?)$") { args, _ in guard let matches = args, let inclusionFlag = matches.first else { XCTAssertNotNil(args?.first, "Step match failed") return } - guard let lastResult = self.lastResult() else { - XCTAssert(false, "Fetch history didn't returned response") - return + XCTFail("Fetch history didn't returned response"); return } - - guard let result = lastResult as? (messagesByChannel: [String: [PubNubMessageBase]], next: PubNubBoundedPage?), - let channel = result.messagesByChannel.first?.key, let messages = result.messagesByChannel[channel] else { - XCTAssert(false, "Fetch history returned unexpected response") - return + guard + let result = lastResult as? (messagesByChannel: [String: [PubNubMessageBase]], next: PubNubBoundedPage?), + let channel = result.messagesByChannel.first?.key, let messages = result.messagesByChannel[channel] + else { + XCTFail("Fetch history returned unexpected response"); return } XCTAssertGreaterThan(messages.count, 0) - let messagesWithTypes = messages.compactMap { $0.messageType } - XCTAssertFalse(inclusionFlag == "with" && messagesWithTypes.count == 0) - XCTAssertFalse(inclusionFlag == "without" && messagesWithTypes.count > 0) + var messagesWithTypes: [String] = [] - if matches.count > 1 { - XCTAssertTrue(messagesWithTypes.map { String(describing: $0.rawValue) }.allSatisfy { Array(matches[1...]).contains($0) }) - } else { - XCTAssertEqual(inclusionFlag == "with" ? messages.count : 0, messagesWithTypes.count) - } - } - - Match(["And"], "^history response contains messages (with|without) ('(.*)' and '(.*)' )?types$") { args, _ in - guard let matches = args, let inclusionFlag = matches.first else { - XCTAssertNotNil(args?.first, "Step match failed") - return + switch self.currentScenario?.name { + case "Client can fetch history without customMessageType enabled by default": + messagesWithTypes = messages.compactMap { $0.customMessageType } + case "Client can fetch history with customMessageType": + messagesWithTypes = messages.compactMap { $0.customMessageType } + case "Client can fetch history with message types": + messagesWithTypes = messages.compactMap { String(describing: $0.messageType.rawValue) } + default: + XCTFail("Unexpected condition") } - guard let lastResult = self.lastResult() else { - XCTAssert(false, "Fetch history didn't returned response") - return - } - - guard let result = lastResult as? (messagesByChannel: [String: [PubNubMessageBase]], next: PubNubBoundedPage?), - let channel = result.messagesByChannel.first?.key, let messages = result.messagesByChannel[channel] else { - XCTAssert(false, "Fetch history returned unexpected response") - return - } - - XCTAssertGreaterThan(messages.count, 0) - - let messagesWithTypes = messages.compactMap { $0.customMessageType } XCTAssertFalse(inclusionFlag == "with" && messagesWithTypes.count == 0) XCTAssertFalse(inclusionFlag == "without" && messagesWithTypes.count > 0) if matches.count > 1 { - XCTAssertTrue(messagesWithTypes.map { $0 }.allSatisfy { Array(matches[1...]).contains($0) }) + XCTAssertTrue(messagesWithTypes.map { String(describing: $0.rawValue) }.allSatisfy { Array(matches[1...]).contains($0) }) } else { XCTAssertEqual(inclusionFlag == "with" ? messages.count : 0, messagesWithTypes.count) } @@ -118,6 +98,33 @@ public class PubNubHistoryContractTestSteps: PubNubContractTestCase { self.wait(for: [historyExpect], timeout: 60.0) } + + When("^I fetch message history with (messageType|customMessageType) for '(.*)' channel$") { args, _ in + guard let type = args?.first, let channel = args?.last else { + XCTAssertNotNil(args?.first, "Step match failed") + return + } + + let historyExpect = self.expectation(description: "Fetch history Response") + let includeMessageType = true + let includeCustomMessageType = type == "customMessageType" + + self.client.fetchMessageHistory( + for: [channel], + includeMessageType: includeMessageType, + includeCustomMessageType: includeCustomMessageType + ) { result in + switch result { + case let .success((messagesByChannel, next)): + self.handleResult(result: (messagesByChannel, next)) + case let .failure(error): + self.handleResult(result: error) + } + historyExpect.fulfill() + } + + self.wait(for: [historyExpect], timeout: 60.0) + } Then("the response contains pagination info") { _, _ in guard let lastResult = self.lastResult() else {