GDTCORTestServer.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2019 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 "GDTCORTests/Integration/TestServer/GDTCORTestServer.h"
  17. @interface GDTCORTestServer ()
  18. /** The server object. */
  19. @property(nonatomic) GCDWebServer *server;
  20. // Redeclare as readwrite and mutable.
  21. @property(nonatomic, readwrite) NSMutableDictionary<NSString *, NSURL *> *registeredTestPaths;
  22. @end
  23. @implementation GDTCORTestServer
  24. - (instancetype)init {
  25. self = [super init];
  26. if (self) {
  27. [GCDWebServer setLogLevel:3];
  28. _server = [[GCDWebServer alloc] init];
  29. _registeredTestPaths = [[NSMutableDictionary alloc] init];
  30. }
  31. return self;
  32. }
  33. - (void)dealloc {
  34. [_server stop];
  35. }
  36. - (void)registerTestPaths {
  37. [self registerLogPath];
  38. [self registerLogBatchPath];
  39. }
  40. - (void)start {
  41. NSAssert(self.server.isRunning == NO, @"The server should not be already running.");
  42. NSError *error;
  43. [self.server
  44. startWithOptions:@{GCDWebServerOption_Port : @0, GCDWebServerOption_BindToLocalhost : @YES}
  45. error:&error];
  46. NSAssert(error == nil, @"Error when starting server: %@", error);
  47. }
  48. - (void)stop {
  49. NSAssert(self.server.isRunning, @"The server should be running before stopping.");
  50. [self.server stop];
  51. }
  52. - (BOOL)isRunning {
  53. return [self.server isRunning];
  54. }
  55. - (NSURL *)serverURL {
  56. return _server.serverURL;
  57. }
  58. #pragma mark - HTTP Path handling methods
  59. /** Registers the /log path, which responds with some JSON. */
  60. - (void)registerLogPath {
  61. id processBlock = ^GCDWebServerResponse *(GCDWebServerRequest *request) {
  62. GCDWebServerDataResponse *response = [[GCDWebServerDataResponse alloc] initWithHTML:@"Hello!"];
  63. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC),
  64. dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  65. if (self.responseCompletedBlock) {
  66. self.responseCompletedBlock(request, response);
  67. }
  68. });
  69. return response;
  70. };
  71. [self.server addHandlerForMethod:@"POST"
  72. path:@"/log"
  73. requestClass:[GCDWebServerRequest class]
  74. processBlock:processBlock];
  75. }
  76. /** Registers the /logBatch path, which responds with some JSON. */
  77. - (void)registerLogBatchPath {
  78. id processBlock = ^GCDWebServerResponse *(__kindof GCDWebServerRequest *request) {
  79. GCDWebServerDataResponse *response = [[GCDWebServerDataResponse alloc] initWithHTML:@"Hello2!"];
  80. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC),
  81. dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  82. if (self.responseCompletedBlock) {
  83. self.responseCompletedBlock(request, response);
  84. }
  85. });
  86. return response;
  87. };
  88. [self.server addHandlerForMethod:@"POST"
  89. path:@"/logBatch"
  90. requestClass:[GCDWebServerRequest class]
  91. processBlock:processBlock];
  92. }
  93. @end