FConnectionTest.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/Extension/FirebaseCoreInternal.h"
  18. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseQuery_Private.h"
  19. #import "FirebaseDatabase/Sources/Realtime/FConnection.h"
  20. #import "FirebaseDatabase/Tests/Helpers/FTestBase.h"
  21. #import "FirebaseDatabase/Tests/Helpers/FTestHelpers.h"
  22. @interface FConnectionTest : FTestBase
  23. @end
  24. @interface FTestConnectionDelegate : NSObject <FConnectionDelegate>
  25. @property(nonatomic, copy) void (^onReady)(NSString *);
  26. @property(nonatomic, copy) void (^onDisconnect)(FDisconnectReason);
  27. @end
  28. @implementation FTestConnectionDelegate
  29. - (void)onReady:(FConnection *)fconnection
  30. atTime:(NSNumber *)timestamp
  31. sessionID:(NSString *)sessionID {
  32. self.onReady(sessionID);
  33. }
  34. - (void)onDataMessage:(FConnection *)fconnection withMessage:(NSDictionary *)message {
  35. }
  36. - (void)onDisconnect:(FConnection *)fwebSocket withReason:(FDisconnectReason)reason {
  37. self.onDisconnect(reason);
  38. }
  39. - (void)onKill:(FConnection *)fconnection withReason:(NSString *)reason {
  40. }
  41. @end
  42. @implementation FConnectionTest
  43. - (void)XXXtestObtainSessionId {
  44. NSString *host =
  45. [NSString stringWithFormat:@"%@.firebaseio.com", [[FIRApp defaultApp] options].projectID];
  46. FRepoInfo *info = [[FRepoInfo alloc] initWithHost:host isSecure:YES withNamespace:@"default"];
  47. FConnection *conn = [[FConnection alloc] initWith:info
  48. andDispatchQueue:[FIRDatabaseQuery sharedQueue]
  49. googleAppID:@"fake-app-id"
  50. lastSessionID:nil
  51. appCheckToken:nil];
  52. FTestConnectionDelegate *delegate = [[FTestConnectionDelegate alloc] init];
  53. __block BOOL done = NO;
  54. delegate.onDisconnect = ^(FDisconnectReason reason) {
  55. if (reason == DISCONNECT_REASON_SERVER_RESET) {
  56. // It is very likely that the first connection attempt sends us a redirect to the project's
  57. // designated server. We need follow that redirect before 'onReady' is invoked.
  58. [conn open];
  59. }
  60. };
  61. delegate.onReady = ^(NSString *sessionID) {
  62. NSAssert(sessionID, @"sessionID cannot be null");
  63. NSAssert([sessionID length] != 0, @"sessionID must have length > 0");
  64. done = YES;
  65. };
  66. conn.delegate = delegate;
  67. [conn open];
  68. WAIT_FOR(done);
  69. }
  70. @end