Skip to content

Commit ba50608

Browse files
authored
Record the size of the request bytes for POST/PUT URL Requests (#9452)
1 parent f6271d8 commit ba50608

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

FirebasePerformance/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Pending
2+
* [fixed] Record the request payload size for POST/PUT requests.
3+
14
# Version 8.13.0
25
* [fixed] Make pre-warming identification more reliable by moving the pre-warm check to the earliest phase of app start.
36

FirebasePerformance/Sources/Instrumentation/FPRNetworkTrace.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ - (void)start {
199199
self.backgroundActivityTracker = [[FPRTraceBackgroundActivityTracker alloc] init];
200200
[self checkpointState:FPRNetworkTraceCheckpointStateInitiated];
201201

202+
if ([self.URLRequest.HTTPMethod isEqualToString:@"POST"] ||
203+
[self.URLRequest.HTTPMethod isEqualToString:@"PUT"]) {
204+
self.requestSize = self.URLRequest.HTTPBody.length;
205+
}
202206
FPRSessionManager *sessionManager = [FPRSessionManager sharedInstance];
203207
[self updateTraceWithCurrentSession:[sessionManager.sessionDetails copy]];
204208
[sessionManager.sessionNotificationCenter addObserver:self

FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,32 @@ - (void)testDataTaskWithRequestAndCompletionHandler {
584584
[instrument deregisterInstrumentors];
585585
}
586586

587+
/** Tests that dataTaskWithRequest:completionHandler: for a POST request returns a non-nil object
588+
* and collects request size. */
589+
- (void)testDataTaskWithPostRequestAndCompletionHandler {
590+
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
591+
[instrument registerInstrumentors];
592+
NSURLSession *session = [NSURLSession sharedSession];
593+
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"test"];
594+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
595+
request.HTTPMethod = @"POST";
596+
request.HTTPBody = [@"sampleData" dataUsingEncoding:NSUTF8StringEncoding];
597+
XCTestExpectation *expectation = [self expectationWithDescription:@"completionHandler called"];
598+
599+
void (^completionHandler)(NSData *_Nullable, NSURLResponse *_Nullable, NSError *_Nullable) =
600+
^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
601+
[expectation fulfill];
602+
};
603+
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
604+
completionHandler:completionHandler];
605+
XCTAssertNotNil(dataTask);
606+
[dataTask resume];
607+
FPRNetworkTrace *trace = [FPRNetworkTrace networkTraceFromObject:dataTask];
608+
XCTAssertEqual(trace.requestSize, 10);
609+
[self waitForExpectationsWithTimeout:10.0 handler:nil];
610+
[instrument deregisterInstrumentors];
611+
}
612+
587613
/** Tests that dataTaskWithUrl:completionHandler: returns a non-nil object. */
588614
- (void)testDataTaskWithUrlAndCompletionHandler {
589615
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];

0 commit comments

Comments
 (0)