Fix MLNNetworkConfiguration not forwarding 'didReceiveResponse' to its delegate. (#4393)

This commit is contained in:
Albin Törnqvist
2026-07-09 18:28:53 +02:00
committed by GitHub
parent 08579ca76d
commit fa6f19f0a0
2 changed files with 58 additions and 0 deletions

View File

@@ -101,6 +101,7 @@ NSString *const kMLNDownloadPerformanceEvent = @"mobile.performance_trace";
MLNNetworkResponse *tempResponse = [MLNNetworkResponse responseWithData:response.data
urlResponse:response.response
error:response.error];
tempResponse = [self.delegate didReceiveResponse:tempResponse];
if (tempResponse) {
MLNInternalNetworkResponse *internalResponse =
[MLNInternalNetworkResponse responseWithData:tempResponse.data

View File

@@ -2,6 +2,23 @@
#import <XCTest/XCTest.h>
#import "MLNNetworkConfiguration_Private.h"
@interface MLNTestNetworkConfigurationDelegate : NSObject <MLNNetworkConfigurationDelegate>
@property (nonatomic) NSUInteger receivedResponseCount;
@property (nonatomic, nullable) NSURLResponse *replacementResponse;
@end
@implementation MLNTestNetworkConfigurationDelegate
- (MLNNetworkResponse *)didReceiveResponse:(MLNNetworkResponse *)response {
self.receivedResponseCount += 1;
if (self.replacementResponse) {
response.response = self.replacementResponse;
}
return response;
}
@end
@interface MLNNetworkConfigurationTests : XCTestCase
@end
@@ -40,4 +57,44 @@
[self waitForExpectations:@[expectation] timeout:10.0];
}
// Regression test: received responses were converted for the delegate but the delegate was
// never actually called, so `didReceiveResponse:` implementations had no effect.
- (void)testDidReceiveResponseIsForwardedToTheDelegate {
MLNNetworkConfiguration *configuration = [[MLNNetworkConfiguration alloc] init];
MLNTestNetworkConfigurationDelegate *delegate = [[MLNTestNetworkConfigurationDelegate alloc] init];
configuration.delegate = delegate;
NSURL *url = [NSURL URLWithString:@"test://tile"];
delegate.replacementResponse = [[NSHTTPURLResponse alloc] initWithURL:url
statusCode:404
HTTPVersion:@"HTTP/1.1"
headerFields:nil];
MLNInternalNetworkResponse *internalResponse = [[MLNInternalNetworkResponse alloc] init];
internalResponse.response = [[NSHTTPURLResponse alloc] initWithURL:url
statusCode:403
HTTPVersion:@"HTTP/1.1"
headerFields:nil];
internalResponse.data = [@"forbidden" dataUsingEncoding:NSUTF8StringEncoding];
MLNInternalNetworkResponse *processedResponse =
[(id<MLNNativeNetworkDelegate>)configuration didReceiveResponse:internalResponse];
XCTAssertEqual(delegate.receivedResponseCount, 1);
XCTAssertEqual(((NSHTTPURLResponse *)processedResponse.response).statusCode, 404);
XCTAssertEqualObjects(processedResponse.data, internalResponse.data);
}
- (void)testDidReceiveResponseWithoutDelegateReturnsResponseUnchanged {
MLNNetworkConfiguration *configuration = [[MLNNetworkConfiguration alloc] init];
MLNInternalNetworkResponse *internalResponse = [[MLNInternalNetworkResponse alloc] init];
internalResponse.data = [@"payload" dataUsingEncoding:NSUTF8StringEncoding];
MLNInternalNetworkResponse *processedResponse =
[(id<MLNNativeNetworkDelegate>)configuration didReceiveResponse:internalResponse];
XCTAssertEqual(processedResponse, internalResponse);
}
@end