FConnectionTest.m 2.6 KB

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