From eb2cba7410bf6ae3d562148df1a8ff0b0ebf07d4 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 12 Aug 2021 14:04:46 +1000 Subject: [PATCH] Add MockCallServer --- Session.xcodeproj/project.pbxproj | 4 ++ .../Calls/MockCallServer.swift | 51 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 SessionMessagingKit/Calls/MockCallServer.swift diff --git a/Session.xcodeproj/project.pbxproj b/Session.xcodeproj/project.pbxproj index 2e3ab67d7..adeeded0b 100644 --- a/Session.xcodeproj/project.pbxproj +++ b/Session.xcodeproj/project.pbxproj @@ -252,6 +252,7 @@ B8B558F326C4CA4600693325 /* MockCallConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8B558F226C4CA4600693325 /* MockCallConfig.swift */; }; B8B558F926C4CE6800693325 /* CallVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8B558F826C4CE6800693325 /* CallVC.swift */; }; B8B558FB26C4D25C00693325 /* MockWebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8B558FA26C4D25C00693325 /* MockWebSocket.swift */; }; + B8B558FD26C4D35400693325 /* MockCallServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8B558FC26C4D35400693325 /* MockCallServer.swift */; }; B8BB82A5238F627000BA5194 /* HomeVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8BB82A4238F627000BA5194 /* HomeVC.swift */; }; B8BC00C0257D90E30032E807 /* General.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8BC00BF257D90E30032E807 /* General.swift */; }; B8C2B2C82563685C00551B4D /* CircleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8C2B2C72563685C00551B4D /* CircleView.swift */; }; @@ -1225,6 +1226,7 @@ B8B558F226C4CA4600693325 /* MockCallConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockCallConfig.swift; sourceTree = ""; }; B8B558F826C4CE6800693325 /* CallVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CallVC.swift; sourceTree = ""; }; B8B558FA26C4D25C00693325 /* MockWebSocket.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockWebSocket.swift; sourceTree = ""; }; + B8B558FC26C4D35400693325 /* MockCallServer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockCallServer.swift; sourceTree = ""; }; B8B5BCEB2394D869003823C9 /* Button.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Button.swift; sourceTree = ""; }; B8BAC75B2695645400EA1759 /* hr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = hr; path = hr.lproj/Localizable.strings; sourceTree = ""; }; B8BAC75C2695648500EA1759 /* sv */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sv; path = sv.lproj/Localizable.strings; sourceTree = ""; }; @@ -2366,6 +2368,7 @@ B806ECA226C4A8C6008BDA44 /* MockTURNServer.swift */, B8B558F226C4CA4600693325 /* MockCallConfig.swift */, B8B558FA26C4D25C00693325 /* MockWebSocket.swift */, + B8B558FC26C4D35400693325 /* MockCallServer.swift */, ); path = Calls; sourceTree = ""; @@ -4741,6 +4744,7 @@ C3DA9C0725AE7396008F7C7E /* ConfigurationMessage.swift in Sources */, B8856CEE256F1054001CE70E /* OWSAudioPlayer.m in Sources */, C32C5EDC256DF501003C73A2 /* YapDatabaseConnection+OWS.m in Sources */, + B8B558FD26C4D35400693325 /* MockCallServer.swift in Sources */, C3BBE0762554CDA60050F1E3 /* Configuration.swift in Sources */, C35D76DB26606304009AA5FB /* ThreadUpdateBatcher.swift in Sources */, B8B32033258B235D0020074B /* Storage+Contacts.swift in Sources */, diff --git a/SessionMessagingKit/Calls/MockCallServer.swift b/SessionMessagingKit/Calls/MockCallServer.swift new file mode 100644 index 000000000..5ee89b1c9 --- /dev/null +++ b/SessionMessagingKit/Calls/MockCallServer.swift @@ -0,0 +1,51 @@ +import Foundation +import PromiseKit + +public struct RoomInfo { + public let roomID: String + public let wssURL: String + public let wssPostURL: String + public let clientID: String + public let isInitiator: String + public let messages: [String] +} + +public enum MockCallServer { + + private static func getRoomURL(for roomID: String) -> String { + let base = MockCallConfig.default.serverURL + "/join/" + return base + "\(roomID)" + } + private static func getLeaveURL(roomID: String, userID: String) -> String { + let base = MockCallConfig.default.serverURL + "/leave/" + return base + "\(roomID)/\(userID)" + } + private static func getMessageURL(roomID: String, userID: String) -> String { + let base = MockCallConfig.default.serverURL + "/message/" + return base + "\(roomID)/\(userID)" + } + + public static func join(roomID: String) -> Promise { + HTTP.execute(.post, getRoomURL(for: roomID)).map2 { json in + guard let status = json["result"] as? String else { throw HTTP.Error.invalidJSON } + if status == "FULL" { preconditionFailure() } + guard let info = json["params"] as? JSON, + let roomID = info["room_id"] as? String, + let wssURL = info["wss_url"] as? String, + let wssPostURL = info["wss_post_url"] as? String, + let clientID = info["client_id"] as? String, + let isInitiator = info["is_initiator"] as? String, + let messages = info["messages"] as? [String] else { throw HTTP.Error.invalidJSON } + return RoomInfo(roomID: roomID, wssURL: wssURL, wssPostURL: wssPostURL, + clientID: clientID, isInitiator: isInitiator, messages: messages) + } + } + + public static func leave(roomID: String, userID: String) -> Promise { + return HTTP.execute(.post, getLeaveURL(roomID: roomID, userID: userID)).map2 { _ in } + } + + public static func send(_ message: Data, roomID: String, userID: String) -> Promise { + HTTP.execute(.post, getMessageURL(roomID: roomID, userID: userID), body: message).map2 { _ in } + } +}