MockTransport.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // MockTransport.swift
  4. // Starscream
  5. //
  6. // Created by Dalton Cherry on 1/29/19.
  7. // Copyright © 2019 Vluxe. All rights reserved.
  8. //
  9. // Licensed under the Apache License, Version 2.0 (the "License");
  10. // you may not use this file except in compliance with the License.
  11. // You may obtain a copy of the License at
  12. //
  13. // http://www.apache.org/licenses/LICENSE-2.0
  14. //
  15. // Unless required by applicable law or agreed to in writing, software
  16. // distributed under the License is distributed on an "AS IS" BASIS,
  17. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. // See the License for the specific language governing permissions and
  19. // limitations under the License.
  20. //
  21. //////////////////////////////////////////////////////////////////////////////////////////////////
  22. import Foundation
  23. @testable import Starscream
  24. public class MockTransport: Transport {
  25. public var usingTLS: Bool {
  26. return false
  27. }
  28. private weak var delegate: TransportEventClient?
  29. private let id: String
  30. weak var server: MockServer?
  31. var uuid: String {
  32. return id
  33. }
  34. public init(server: MockServer) {
  35. self.server = server
  36. self.id = UUID().uuidString
  37. }
  38. public func register(delegate: TransportEventClient) {
  39. self.delegate = delegate
  40. }
  41. public func connect(url: URL, timeout: Double, certificatePinning: CertificatePinning?) {
  42. server?.connect(transport: self)
  43. delegate?.connectionChanged(state: .connected)
  44. }
  45. public func disconnect() {
  46. server?.disconnect(uuid: uuid)
  47. }
  48. public func write(data: Data, completion: @escaping ((Error?) -> ())) {
  49. server?.write(data: data, uuid: uuid)
  50. }
  51. public func received(data: Data) {
  52. delegate?.connectionChanged(state: .receive(data))
  53. }
  54. public func getSecurityData() -> (SecTrust?, String?) {
  55. return (nil, nil)
  56. }
  57. }
  58. public class MockSecurity: CertificatePinning, HeaderValidator {
  59. public func evaluateTrust(trust: SecTrust, domain: String?, completion: ((PinningState) -> ())) {
  60. completion(.success)
  61. }
  62. public func validate(headers: [String: String], key: String) -> Error? {
  63. return nil
  64. }
  65. }