FPRHermeticTestServer.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebasePerformance/Tests/Unit/Server/FPRHermeticTestServer.h"
  15. @interface FPRHermeticTestServer ()
  16. /** The server object. */
  17. @property(nonatomic) GCDWebServer *server;
  18. // Redeclare as readwrite and mutable.
  19. @property(nonatomic, readwrite) NSMutableDictionary<NSString *, NSURL *> *registeredTestPaths;
  20. @end
  21. @implementation FPRHermeticTestServer
  22. - (instancetype)init {
  23. self = [super init];
  24. if (self) {
  25. [GCDWebServer setLogLevel:3];
  26. _server = [[GCDWebServer alloc] init];
  27. _registeredTestPaths = [[NSMutableDictionary alloc] init];
  28. }
  29. return self;
  30. }
  31. - (void)dealloc {
  32. [_server stop];
  33. }
  34. - (void)registerTestPaths {
  35. [self registerPathIndex];
  36. [self registerPathTest];
  37. [self registerPathTestRedirect];
  38. [self registerPathTestDownload];
  39. [self registerPathTestBigDownload];
  40. [self registerPathTestUpload];
  41. }
  42. - (void)start {
  43. NSAssert(self.server.isRunning == NO, @"The server should not be already running.");
  44. NSError *error;
  45. [self.server
  46. startWithOptions:@{GCDWebServerOption_Port : @0, GCDWebServerOption_BindToLocalhost : @YES}
  47. error:&error];
  48. NSAssert(error == nil, @"Error when starting server: %@", error);
  49. }
  50. - (void)stop {
  51. NSAssert(self.server.isRunning, @"The server should be running before stopping.");
  52. [self.server stop];
  53. }
  54. - (BOOL)isRunning {
  55. return [self.server isRunning];
  56. }
  57. - (NSURL *)serverURL {
  58. return _server.serverURL;
  59. }
  60. #pragma mark - HTTP Path handling methods
  61. /** Registers the index path, "/". */
  62. - (void)registerPathIndex {
  63. id processBlock = ^GCDWebServerResponse *(GCDWebServerRequest *request) {
  64. GCDWebServerDataResponse *response = [[GCDWebServerDataResponse alloc] initWithHTML:@"Hello!"];
  65. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC),
  66. dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  67. if (self->_responseCompletedBlock) {
  68. self->_responseCompletedBlock(request, response);
  69. }
  70. });
  71. return response;
  72. };
  73. [self.server addHandlerForMethod:@"GET"
  74. path:@"/"
  75. requestClass:[GCDWebServerRequest class]
  76. processBlock:processBlock];
  77. }
  78. /** Registers the "/test" path, which responds with plain HTML. */
  79. - (void)registerPathTest {
  80. id processBlock = ^GCDWebServerResponse *(__kindof GCDWebServerRequest *request) {
  81. GCDWebServerDataResponse *response = [[GCDWebServerDataResponse alloc] initWithHTML:@"Hello2!"];
  82. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC),
  83. dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  84. if (self->_responseCompletedBlock) {
  85. self->_responseCompletedBlock(request, response);
  86. }
  87. });
  88. return response;
  89. };
  90. [self.server addHandlerForMethod:@"GET"
  91. path:@"/test"
  92. requestClass:[GCDWebServerRequest class]
  93. processBlock:processBlock];
  94. }
  95. /** Registers the "/testRedirect" path, which sends a redirect response. */
  96. - (void)registerPathTestRedirect {
  97. id processBlock = ^GCDWebServerResponse *(__kindof GCDWebServerRequest *request) {
  98. NSURL *redirectURL = [NSURL URLWithString:@"/test"];
  99. GCDWebServerDataResponse *response =
  100. [[GCDWebServerDataResponse alloc] initWithRedirect:redirectURL permanent:NO];
  101. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC),
  102. dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  103. if (self->_responseCompletedBlock) {
  104. self->_responseCompletedBlock(request, response);
  105. }
  106. });
  107. return response;
  108. };
  109. [self.server addHandlerForMethod:@"GET"
  110. path:@"/testRedirect"
  111. requestClass:[GCDWebServerRequest class]
  112. processBlock:processBlock];
  113. }
  114. /** Registers the "/testDownload" path, which responds with a small amount of data. */
  115. - (void)registerPathTestDownload {
  116. NSString *filePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"smallDownloadFile"
  117. ofType:@""];
  118. id processBlock = ^GCDWebServerResponse *(__kindof GCDWebServerRequest *request) {
  119. GCDWebServerFileResponse *response = [[GCDWebServerFileResponse alloc] initWithFile:filePath];
  120. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC),
  121. dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  122. if (self->_responseCompletedBlock) {
  123. self->_responseCompletedBlock(request, response);
  124. }
  125. });
  126. return response;
  127. };
  128. [self.server addHandlerForMethod:@"GET"
  129. path:@"/testDownload"
  130. requestClass:[GCDWebServerRequest class]
  131. processBlock:processBlock];
  132. }
  133. /** Registers the "/testBigDownload" path, which responds with a larger amount of data. */
  134. - (void)registerPathTestBigDownload {
  135. NSString *filePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"bigDownloadFile"
  136. ofType:@""];
  137. id processBlock = ^GCDWebServerResponse *(__kindof GCDWebServerRequest *request) {
  138. GCDWebServerFileResponse *response = [[GCDWebServerFileResponse alloc] initWithFile:filePath];
  139. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC),
  140. dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  141. if (self->_responseCompletedBlock) {
  142. self->_responseCompletedBlock(request, response);
  143. }
  144. });
  145. return response;
  146. };
  147. [self.server addHandlerForMethod:@"GET"
  148. path:@"/testBigDownload"
  149. requestClass:[GCDWebServerRequest class]
  150. processBlock:processBlock];
  151. }
  152. /** Registers the "/testUpload" path, which accepts some data. */
  153. - (void)registerPathTestUpload {
  154. id processBlock = ^GCDWebServerResponse *(__kindof GCDWebServerRequest *request) {
  155. GCDWebServerResponse *response = [[GCDWebServerDataResponse alloc] initWithHTML:@"ok"];
  156. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC),
  157. dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  158. if (self->_responseCompletedBlock) {
  159. self->_responseCompletedBlock(request, response);
  160. }
  161. });
  162. return response;
  163. };
  164. [self.server addHandlerForMethod:@"POST"
  165. path:@"/testUpload"
  166. requestClass:[GCDWebServerRequest class]
  167. processBlock:processBlock];
  168. }
  169. @end