ReachabilityTests.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // ReachabilityTests.swift
  3. // ReachabilityTests
  4. //
  5. // Created by Ashley Mills on 23/11/2015.
  6. // Copyright © 2015 Ashley Mills. All rights reserved.
  7. //
  8. import XCTest
  9. import Reachability
  10. class ReachabilityTests: XCTestCase {
  11. override func setUp() {
  12. super.setUp()
  13. }
  14. override func tearDown() {
  15. super.tearDown()
  16. }
  17. func testInvalidHost() {
  18. let reachability: Reachability
  19. let invalidHostName = "google.com"
  20. do {
  21. try reachability = Reachability(hostname: invalidHostName)
  22. } catch {
  23. XCTAssert(false, "Unable to create reachability")
  24. return
  25. }
  26. let expectation = expectationWithDescription("Check invalid host")
  27. reachability.whenReachable = { reachability in
  28. dispatch_async(dispatch_get_main_queue()) {
  29. XCTAssert(false, "\(invalidHostName) should be unreachable - \(reachability)")
  30. expectation.fulfill()
  31. }
  32. }
  33. reachability.whenUnreachable = { reachability in
  34. dispatch_async(dispatch_get_main_queue()) {
  35. print("Pass: \(invalidHostName) is unreachable - \(reachability))")
  36. expectation.fulfill()
  37. }
  38. }
  39. do {
  40. try reachability.startNotifier()
  41. } catch {
  42. XCTAssert(false, "Unable to start notifier")
  43. return
  44. }
  45. waitForExpectationsWithTimeout(5, handler: nil)
  46. reachability.stopNotifier()
  47. }
  48. func testaValidHost() {
  49. let reachability: Reachability
  50. let validHostName = "google.com"
  51. do {
  52. try reachability = Reachability(hostname: validHostName)
  53. } catch {
  54. XCTAssert(false, "Unable to create reachability")
  55. return
  56. }
  57. let expectation = expectationWithDescription("Check invalid host")
  58. reachability.whenReachable = { reachability in
  59. dispatch_async(dispatch_get_main_queue()) {
  60. print("Pass: \(validHostName) is reachable - \(reachability)")
  61. expectation.fulfill()
  62. }
  63. }
  64. reachability.whenUnreachable = { reachability in
  65. dispatch_async(dispatch_get_main_queue()) {
  66. XCTAssert(false, "\(validHostName) should be reachable - \(reachability)")
  67. expectation.fulfill()
  68. }
  69. }
  70. do {
  71. try reachability.startNotifier()
  72. } catch {
  73. XCTAssert(false, "Unable to start notifier")
  74. return
  75. }
  76. waitForExpectationsWithTimeout(5, handler: nil)
  77. reachability.stopNotifier()
  78. }
  79. }