Skip to content

Commit

Permalink
ADD: buildRequestUrl tests & update MigrationGuide.md
Browse files Browse the repository at this point in the history
Change-Id: Ib7a0f4b27514dc2dfaa49df0d901437e15f8b146
  • Loading branch information
liujl committed Sep 13, 2016
1 parent e9c833a commit 6ae6563
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 26 deletions.
24 changes: 20 additions & 4 deletions MigrationGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,31 @@ typedef NS_ENUM(NSInteger, YTKResponseSerializerType) {
};
```
默认的序列化选项是 `YTKResponseSerializerTypeHTTP`。
默认的序列化选项是 `YTKResponseSerializerTypeJSON`。
需要注意的是,YTKNetwork 1.0 没有对响应序列化进行控制,并且主动忽略了 AFN 返回的响应序列化错误。AFN 默认进行的是 JSON 序列化,而在 YTKNetwork 1.0 中即使序列化结果是错误的(例如 Content-Type 不正确,或者返回的是二进制数据),仍然会认为请求是成功的,并且调用 YTKRequest 的成功回调。这一行为在 YTKNetwork 2.0 当中被修正了。如果发现在升级 YTKNetwork 2.0 之后某些之前正常的请求报错,请检查是否是由于响应的类型不正确。
## URL 拼接
YTKNetwork 2.0 中 `baseUrl` 和 `requestUrl` 的拼接实现发生了变化。在 1.X 中这两个 URL 是作为字符串直接拼接起来的,而在 2.0 中改为使用 `[NSURL URLWithString:relativeToURL]` 方法进行拼接,以提升可靠性和兼容性。
这一实现可能导致和之前不同的 URL 拼接结果。当 `requestUrl` 带有 `/` 前缀,同时 `baseUrl` 包含除 Host 之外的内容, `requestUrl` 仍然会被拼接到最顶级,这一差距来源于 `NSURL` 本身的实现:
```Objective-C
NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"];
NSURL *resultURL = [NSURL URLWithString:@"/foo" relativeToURL:baseURL];
// resultURL : http://example.com/foo
```

因此请注意如果 `baseUrl` 包含 Path 那么 `requestUrl` 的前缀不要在加入 `/` ,以免产生错误的的拼接结果。

## 下载请求

原来基于 `AFDownloadRequestOperation` 的下载请求改为使用系统自己的 `NSURLSessionDownloadTask`。当 `YTKRequest``resumableDownloadPath` 属性不为 nil 的情况下,会调用 `NSURLSessionDownloadTask` 进行下载,下载完成后文件会自动保存到给出的路径,无需再进行存储操作。

对于下载请求来说,响应属性的获取行为如下:

* `responseData`:可以获取
* `responseData`不能获取
* `responseString`:不能获取
* `responseObject`:为 NSURL,是下载文件在本地所存储的路径。

Expand All @@ -64,7 +80,7 @@ typedef void (^AFURLSessionTaskProgressBlock)(NSProgress *);
## YTKNetworkPrivate 不再暴露
`YTKNetworkPrivate.h` 将会成为私有头文件,所以依赖于此头文件的方法将不再可用
`YTKNetworkPrivate.h` 将会成为私有头文件,所有依赖于此头文件的方法将不再可用
## Cache API 更新
Expand Down Expand Up @@ -97,7 +113,7 @@ YTKNetwork 2.0 中加入了用于控制是否进行异步写缓存的接口:
## 响应前向处理
`- (void)requestCompleteFilter``- (void)requestFailedFilter` 对应, YTKNetwork 2.0 中加入了用于在响应结束,但是切换回主线程之前执行操作的函数 `- (void)requestCompletePreprocessor``- (void)requestFailedPreprocessor`,在这里执行的操作,可以避免卡顿主线程
与 `- (void)requestCompleteFilter` 和 `- (void)requestFailedFilter` 对应, YTKNetwork 2.0 中加入了用于在响应结束,但是切换回主线程之前执行操作的函数 `- (void)requestCompletePreprocessor` 和 `- (void)requestFailedPreprocessor`,在这里执行的操作,可以避免阻塞主线程
## 命名变更
Expand Down
34 changes: 18 additions & 16 deletions YTKNetworkTests/YTKConcurrencyTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,24 @@ - (void)testBasicConcurrentRequestCreation {
__block NSInteger callbackCount = 0;
for (NSUInteger i = 0; i < dispatchTarget; i++) {
dispatch_async(queue, ^{
YTKBasicHTTPRequest *req = [[YTKBasicHTTPRequest alloc] init];
req.tag = i;

[req startWithCompletionBlockWithSuccess:nil failure:^(__kindof YTKBaseRequest * _Nonnull request) {
// Left is from callback, right is captured by block.
XCTAssertTrue(request.tag == i);
callbackCount ++;
}];

// We just need to simulate concurrent request creation here.
[req.requestTask cancel];

NSLog(@"Current req number: %zd", i);
dispatch_sync(dispatch_get_main_queue(), ^{
completionCount++;
});
@autoreleasepool {
YTKBasicHTTPRequest *req = [[YTKBasicHTTPRequest alloc] init];
req.tag = i;

[req startWithCompletionBlockWithSuccess:nil failure:^(__kindof YTKBaseRequest * _Nonnull request) {
// Left is from callback, right is captured by block.
XCTAssertTrue(request.tag == i);
callbackCount ++;
}];

// We just need to simulate concurrent request creation here.
[req.requestTask cancel];

NSLog(@"Current req number: %zd", i);
dispatch_sync(dispatch_get_main_queue(), ^{
completionCount++;
});
}
});
}

Expand Down
22 changes: 22 additions & 0 deletions YTKNetworkTests/YTKNetworkRequestTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ - (void)tearDown {
[super tearDown];
}

- (void)_testBuildRequestURLWithBaseURL:(NSString *)baseURL detailURL:(NSString *)detailURL resultURL:(NSString *)resultURL{
YTKNetworkConfig *config = [YTKNetworkConfig sharedConfig];
YTKNetworkAgent *agent = [YTKNetworkAgent sharedAgent];

config.baseUrl = baseURL;

YTKBasicHTTPRequest *request = [[YTKBasicHTTPRequest alloc] initWithRequestUrl:detailURL];
NSString *url = [agent buildRequestUrl:request];

XCTAssertTrue([url isEqualToString:resultURL]);
}

- (void)testBuildRequestURL {
[self _testBuildRequestURLWithBaseURL:@"http://www.example.com" detailURL:@"get" resultURL:@"http://www.example.com/get"];
[self _testBuildRequestURLWithBaseURL:@"http://www.example.com/" detailURL:@"get" resultURL:@"http://www.example.com/get"];
[self _testBuildRequestURLWithBaseURL:@"https://www.example.com" detailURL:@"get" resultURL:@"https://www.example.com/get"];
[self _testBuildRequestURLWithBaseURL:@"http://www.example.com" detailURL:@"get/val" resultURL:@"http://www.example.com/get/val"];
[self _testBuildRequestURLWithBaseURL:@"http://www.example.com" detailURL:@"get/val/" resultURL:@"http://www.example.com/get/val/"];
[self _testBuildRequestURLWithBaseURL:@"" detailURL:@"http://www.example.com" resultURL:@"http://www.example.com"];
[self _testBuildRequestURLWithBaseURL:@"" detailURL:@"https://www.example.com" resultURL:@"https://www.example.com"];
}

- (void)testBasicHTTPRequest {
YTKBasicHTTPRequest *get = [[YTKBasicHTTPRequest alloc] initWithRequestUrl:@"get" method:YTKRequestMethodGET];
[self expectSuccess:get];
Expand Down
14 changes: 8 additions & 6 deletions YTKNetworkTests/YTKPerformanceTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ - (void)testBaseRequestCreationPerformance {
// The measure block will be called several times.
[self measureBlock:^{
for (NSUInteger i = 0; i < targetCount; i++) {
YTKBasicHTTPRequest *req = [[YTKBasicHTTPRequest alloc] init];
[req startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
NSNumber *result = request.responseObject;
XCTAssertTrue([result isEqualToNumber:@(i)]);
} failure:nil];
[req stop];
@autoreleasepool {
YTKBasicHTTPRequest *req = [[YTKBasicHTTPRequest alloc] init];
[req startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
NSNumber *result = request.responseObject;
XCTAssertTrue([result isEqualToNumber:@(i)]);
} failure:nil];
[req.requestTask cancel];
}
}
}];
}
Expand Down

0 comments on commit 6ae6563

Please sign in to comment.