FConnectionTest.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #import <FirebaseCore/FIRApp.h>
  18. #import <FirebaseCore/FIROptions.h>
  19. #import "FConnection.h"
  20. #import "FIRDatabaseQuery_Private.h"
  21. #import "FTestBase.h"
  22. #import "FTestHelpers.h"
  23. @interface FConnectionTest : FTestBase
  24. @end
  25. @interface FTestConnectionDelegate : NSObject <FConnectionDelegate>
  26. @property(nonatomic, copy) void (^onReady)(NSString *);
  27. @property(nonatomic, copy) void (^onDisconnect)(FDisconnectReason);
  28. @end
  29. @implementation FTestConnectionDelegate
  30. - (void)onReady:(FConnection *)fconnection
  31. atTime:(NSNumber *)timestamp
  32. sessionID:(NSString *)sessionID {
  33. self.onReady(sessionID);
  34. }
  35. - (void)onDataMessage:(FConnection *)fconnection withMessage:(NSDictionary *)message {
  36. }
  37. - (void)onDisconnect:(FConnection *)fwebSocket withReason:(FDisconnectReason)reason {
  38. self.onDisconnect(reason);
  39. }
  40. - (void)onKill:(FConnection *)fconnection withReason:(NSString *)reason {
  41. }
  42. @end
  43. @implementation FConnectionTest
  44. - (void)XXXtestObtainSessionId {
  45. NSString *host =
  46. [NSString stringWithFormat:@"%@.firebaseio.com", [[FIRApp defaultApp] options].projectID];
  47. FRepoInfo *info = [[FRepoInfo alloc] initWithHost:host isSecure:YES withNamespace:@"default"];
  48. FConnection *conn = [[FConnection alloc] initWith:info
  49. andDispatchQueue:[FIRDatabaseQuery sharedQueue]
  50. lastSessionID:nil];
  51. FTestConnectionDelegate *delegate = [[FTestConnectionDelegate alloc] init];
  52. __block BOOL done = NO;
  53. delegate.onDisconnect = ^(FDisconnectReason reason) {
  54. if (reason == DISCONNECT_REASON_SERVER_RESET) {
  55. // It is very likely that the first connection attempt sends us a redirect to the project's
  56. // designated server. We need follow that redirect before 'onReady' is invoked.
  57. [conn open];
  58. }
  59. };
  60. delegate.onReady = ^(NSString *sessionID) {
  61. NSAssert(sessionID, @"sessionID cannot be null");
  62. NSAssert([sessionID length] != 0, @"sessionID must have length > 0");
  63. done = YES;
  64. };
  65. conn.delegate = delegate;
  66. [conn open];
  67. WAIT_FOR(done);
  68. }
  69. @end