Skip to content

Commit e86ec5e

Browse files
committed
session progress
1 parent 76d7a4b commit e86ec5e

File tree

8 files changed

+145
-13
lines changed

8 files changed

+145
-13
lines changed

QiniuSDK.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109
DFF525341A6235D100D02BA1 /* QNSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525301A6235D100D02BA1 /* QNSessionManager.m */; };
110110
DFF525351A6235D100D02BA1 /* QNSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525301A6235D100D02BA1 /* QNSessionManager.m */; };
111111
DFF525371A626A3700D02BA1 /* QNhttpDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = DFF525361A626A3700D02BA1 /* QNhttpDelegate.h */; };
112+
DFF525391A64079B00D02BA1 /* QNSessionTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525381A64079B00D02BA1 /* QNSessionTest.m */; };
113+
DFF5253A1A64079B00D02BA1 /* QNSessionTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525381A64079B00D02BA1 /* QNSessionTest.m */; };
112114
DFFE0E6019E6575600D7A0FC /* QNFileRecorderTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFFE0E5F19E6575600D7A0FC /* QNFileRecorderTest.m */; };
113115
DFFE0E6119E6575600D7A0FC /* QNFileRecorderTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFFE0E5F19E6575600D7A0FC /* QNFileRecorderTest.m */; };
114116
EE76B381AF33E356C2C8FC56 /* libPods-QiniuSDK MacTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 0EB0B4B6BA4EEC2DEF421644 /* libPods-QiniuSDK MacTests.a */; };
@@ -190,6 +192,7 @@
190192
DFF5252F1A6235D100D02BA1 /* QNSessionManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNSessionManager.h; sourceTree = "<group>"; };
191193
DFF525301A6235D100D02BA1 /* QNSessionManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNSessionManager.m; sourceTree = "<group>"; };
192194
DFF525361A626A3700D02BA1 /* QNhttpDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNhttpDelegate.h; sourceTree = "<group>"; };
195+
DFF525381A64079B00D02BA1 /* QNSessionTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNSessionTest.m; sourceTree = "<group>"; };
193196
DFFE0E5F19E6575600D7A0FC /* QNFileRecorderTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNFileRecorderTest.m; sourceTree = "<group>"; };
194197
/* End PBXFileReference section */
195198

@@ -361,6 +364,7 @@
361364
DFA9B65E19E391A100A15FD1 /* QNTestConfig.h */,
362365
DFFE0E5F19E6575600D7A0FC /* QNFileRecorderTest.m */,
363366
DF609A001A5839B400AC7297 /* QNDnsTest.m */,
367+
DFF525381A64079B00D02BA1 /* QNSessionTest.m */,
364368
);
365369
path = QiniuSDKTests;
366370
sourceTree = SOURCE_ROOT;
@@ -711,6 +715,7 @@
711715
DF2CDE8319DAF67F00CE01FB /* QNCrc32.m in Sources */,
712716
DF293C9719DB865800799011 /* QNCrc32Test.m in Sources */,
713717
DFA9B64519DFE43500A15FD1 /* QNTempFile.m in Sources */,
718+
DFF525391A64079B00D02BA1 /* QNSessionTest.m in Sources */,
714719
DF609A071A58E39D00AC7297 /* QNFormUpload.m in Sources */,
715720
DFF525331A6235D100D02BA1 /* QNSessionManager.m in Sources */,
716721
DFA9B63F19DFD8C900A15FD1 /* QNEtagTest.m in Sources */,
@@ -764,6 +769,7 @@
764769
DF2CDE7719DAF65800CE01FB /* QNCrc32.m in Sources */,
765770
DF293C9819DB865800799011 /* QNCrc32Test.m in Sources */,
766771
DFA9B64619DFE43500A15FD1 /* QNTempFile.m in Sources */,
772+
DFF5253A1A64079B00D02BA1 /* QNSessionTest.m in Sources */,
767773
DF609A091A58E39D00AC7297 /* QNFormUpload.m in Sources */,
768774
DFF525351A6235D100D02BA1 /* QNSessionManager.m in Sources */,
769775
DFA9B64019DFD8C900A15FD1 /* QNEtagTest.m in Sources */,

QiniuSDK/Http/QNSessionManager.m

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ - (instancetype)initWithProxy:(NSDictionary *)proxyDict {
3535
configuration.connectionProxyDictionary = proxyDict;
3636
}
3737
_httpManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
38-
_httpManager.responseSerializer = [AFJSONResponseSerializer serializer];
38+
_httpManager.responseSerializer = [AFHTTPResponseSerializer serializer];
3939
}
4040

4141
return self;
@@ -44,7 +44,7 @@ - (instancetype)initWithProxy:(NSDictionary *)proxyDict {
4444
+ (QNResponseInfo *)buildResponseInfo:(NSHTTPURLResponse *)response
4545
withError:(NSError *)error
4646
withDuration:(double)duration
47-
withResponse:(id)responseObject
47+
withResponse:(NSData *)body
4848
withHost:(NSString *)host {
4949
QNResponseInfo *info;
5050

@@ -53,7 +53,7 @@ + (QNResponseInfo *)buildResponseInfo:(NSHTTPURLResponse *)response
5353
NSString *reqId = headers[@"X-Reqid"];
5454
NSString *xlog = headers[@"X-Log"];
5555
int status = (int)[response statusCode];
56-
info = [[QNResponseInfo alloc] init:status withReqId:reqId withXLog:xlog withHost:host withDuration:duration withBody:responseObject];
56+
info = [[QNResponseInfo alloc] init:status withReqId:reqId withXLog:xlog withHost:host withDuration:duration withBody:body];
5757
}
5858
else {
5959
info = [QNResponseInfo responseInfoWithNetError:error host:host duration:duration];
@@ -67,22 +67,27 @@ - (void) sendRequest:(NSMutableURLRequest *)request
6767
__block NSDate *startTime = [NSDate date];
6868
NSProgress *progress = nil;
6969
__block NSString *host = request.URL.host;
70+
7071
NSURLSessionUploadTask *uploadTask = [_httpManager uploadTaskWithStreamedRequest:request progress:&progress completionHandler: ^(NSURLResponse *response, id responseObject, NSError *error) {
72+
NSData *data = responseObject;
7173
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
7274
double duration = [[NSDate date] timeIntervalSinceDate:startTime];
7375
QNResponseInfo *info;
7476
NSDictionary *resp = nil;
7577
if (error == nil) {
76-
info = [QNSessionManager buildResponseInfo:httpResponse withError:nil withDuration:duration withResponse:responseObject withHost:host];
78+
info = [QNSessionManager buildResponseInfo:httpResponse withError:nil withDuration:duration withResponse:data withHost:host];
7779
if (info.isOK) {
78-
resp = responseObject;
80+
NSError *tmp;
81+
resp = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&tmp];
7982
}
8083
}
8184
else {
82-
info = [QNSessionManager buildResponseInfo:httpResponse withError:error withDuration:duration withResponse:responseObject withHost:host];
85+
info = [QNSessionManager buildResponseInfo:httpResponse withError:error withDuration:duration withResponse:data withHost:host];
8386
}
87+
[progress removeObserver:self forKeyPath:@"fractionCompleted" context:(__bridge void *)(progressBlock)];
8488
completeBlock(info, resp);
8589
}];
90+
[progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:(__bridge void *)(progressBlock)];
8691

8792
[request setTimeoutInterval:kQNTimeoutInterval];
8893

@@ -91,6 +96,19 @@ - (void) sendRequest:(NSMutableURLRequest *)request
9196
[uploadTask resume];
9297
}
9398

99+
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
100+
if ([keyPath isEqualToString:@"fractionCompleted"]) {
101+
NSProgress *progress = (NSProgress *)object;
102+
QNInternalProgressBlock progressBlock = (__bridge QNInternalProgressBlock)context;
103+
if (progress != nil && progressBlock != nil) {
104+
progressBlock(progress.completedUnitCount, progress.totalUnitCount);
105+
}
106+
}
107+
else {
108+
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
109+
}
110+
}
111+
94112
- (void)multipartPost:(NSString *)url
95113
withData:(NSData *)data
96114
withParams:(NSDictionary *)params

QiniuSDK/Storage/QNFormUpload.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import <Foundation/Foundation.h>
1010
#import "QNUploadManager.h"
11+
#import "QNhttpDelegate.h"
1112

1213
@class QNHttpManager;
1314
@interface QNFormUpload : NSObject
@@ -17,7 +18,7 @@
1718
withToken:(NSString *)token
1819
withCompletionHandler:(QNUpCompletionHandler)block
1920
withOption:(QNUploadOption *)option
20-
withHttpManager:(QNHttpManager *)http;
21+
withHttpManager:(id <QNHttpDelegate> )http;
2122

2223
- (void)put;
2324

QiniuSDK/Storage/QNFormUpload.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
@interface QNFormUpload ()
2020

2121
@property (nonatomic, strong) NSData *data;
22-
@property (nonatomic, strong) QNHttpManager *httpManager;
22+
@property (nonatomic, strong) id <QNHttpDelegate> httpManager;
2323
@property (nonatomic) int retryTimes;
2424
@property (nonatomic, strong) NSString *key;
2525
@property (nonatomic, strong) NSString *token;
@@ -36,7 +36,7 @@ - (instancetype)initWithData:(NSData *)data
3636
withToken:(NSString *)token
3737
withCompletionHandler:(QNUpCompletionHandler)block
3838
withOption:(QNUploadOption *)option
39-
withHttpManager:(QNHttpManager *)http {
39+
withHttpManager:(id <QNHttpDelegate> )http {
4040
if (self = [super init]) {
4141
_data = data;
4242
_key = key;

QiniuSDK/Storage/QNResumeUpload.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import <Foundation/Foundation.h>
1010
#import "QNUploadManager.h"
11+
#import "QNhttpDelegate.h"
1112

1213
@class QNHttpManager;
1314
@interface QNResumeUpload : NSObject
@@ -21,7 +22,7 @@
2122
withModifyTime:(NSDate *)time
2223
withRecorder:(id <QNRecorderDelegate> )recorder
2324
withRecorderKey:(NSString *)recorderKey
24-
withHttpManager:(QNHttpManager *)http;
25+
withHttpManager:(id <QNHttpDelegate> )http;
2526

2627
- (void)run;
2728

QiniuSDK/Storage/QNResumeUpload.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ - (instancetype)initWithData:(NSData *)data
6767
withModifyTime:(NSDate *)time
6868
withRecorder:(id <QNRecorderDelegate> )recorder
6969
withRecorderKey:(NSString *)recorderKey
70-
withHttpManager:(QNHttpManager *)http {
70+
withHttpManager:(id <QNHttpDelegate> )http {
7171
if (self = [super init]) {
7272
_data = data;
7373
_size = size;
@@ -77,7 +77,7 @@ - (instancetype)initWithData:(NSData *)data
7777
_complete = block;
7878
_headers = @{ @"Authorization":tok, @"Content-Type":@"application/octet-stream" };
7979
_recorder = recorder;
80-
_httpManager = [[QNHttpManager alloc] init];
80+
_httpManager = http;
8181
if (time != nil) {
8282
_modifyTime = [time timeIntervalSince1970];
8383
}

QiniuSDK/Storage/QNUploadManager.m

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,25 @@ - (instancetype)initWithRecorder:(id <QNRecorderDelegate> )recorder
4545
proxy:(NSDictionary *)proxyDict {
4646
if (self = [super init]) {
4747
#if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000) || (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090)
48-
_httpManager = [[QNSessionManager alloc] initWithProxy:proxyDict];
48+
BOOL lowVersion = NO;
49+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED)
50+
float sysVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
51+
if (sysVersion < 7.0) {
52+
lowVersion = YES;
53+
}
54+
#else
55+
NSOperatingSystemVersion sysVersion = [[NSProcessInfo processInfo] operatingSystemVersion];
56+
57+
if ((sysVersion.majorVersion = 10 && sysVersion.minorVersion < 9)) {
58+
lowVersion = YES;
59+
}
60+
#endif
61+
if (lowVersion) {
62+
_httpManager = [[QNHttpManager alloc] init];
63+
}
64+
else {
65+
_httpManager = [[QNSessionManager alloc] initWithProxy:proxyDict];
66+
}
4967
#else
5068
_httpManager = [[QNHttpManager alloc] init];
5169
#endif

QiniuSDKTests/QNSessionTest.m

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//
2+
// QNHttpTest.m
3+
// QiniuSDK
4+
//
5+
// Created by bailong on 14/10/3.
6+
// Copyright (c) 2014年 Qiniu. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000) || (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090)
11+
#import <XCTest/XCTest.h>
12+
13+
#import <AGAsyncTestHelper.h>
14+
15+
#import "QNSessionManager.h"
16+
#import "QNResponseInfo.h"
17+
18+
@interface QNSessionTest : XCTestCase
19+
@property QNSessionManager *httpManager;
20+
@end
21+
22+
@implementation QNSessionTest
23+
24+
- (void)setUp {
25+
[super setUp];
26+
_httpManager = [[QNSessionManager alloc] initWithProxy:nil];
27+
}
28+
29+
- (void)tearDown {
30+
[super tearDown];
31+
}
32+
33+
- (void)testPost {
34+
__block QNResponseInfo *testInfo = nil;
35+
NSData *data = [@"Hello, World!" dataUsingEncoding : NSUTF8StringEncoding];
36+
[_httpManager post:@"http://www.baidu.com" withData:data withParams:nil withHeaders:nil withCompleteBlock: ^(QNResponseInfo *info, NSDictionary *resp) {
37+
testInfo = info;
38+
} withProgressBlock:nil withCancelBlock:nil];
39+
AGWW_WAIT_WHILE(testInfo == nil, 100.0);
40+
NSLog(@"%@", testInfo);
41+
42+
XCTAssert(testInfo.reqId == nil, @"Pass");
43+
44+
testInfo = nil;
45+
46+
[_httpManager post:@"http://up.qiniu.com" withData:nil withParams:nil withHeaders:nil withCompleteBlock: ^(QNResponseInfo *info, NSDictionary *resp) {
47+
testInfo = info;
48+
} withProgressBlock:nil withCancelBlock:nil];
49+
50+
AGWW_WAIT_WHILE(testInfo == nil, 100.0);
51+
NSLog(@"%@", testInfo);
52+
XCTAssert(testInfo.reqId, @"Pass");
53+
54+
testInfo = nil;
55+
[_httpManager post:@"http://httpbin.org/status/500" withData:nil withParams:nil withHeaders:nil withCompleteBlock: ^(QNResponseInfo *info, NSDictionary *resp) {
56+
testInfo = info;
57+
} withProgressBlock:nil withCancelBlock:nil];
58+
59+
AGWW_WAIT_WHILE(testInfo == nil, 100.0);
60+
NSLog(@"%@", testInfo);
61+
XCTAssert(testInfo.statusCode == 500, @"Pass");
62+
XCTAssert(testInfo.error != nil, @"Pass");
63+
64+
testInfo = nil;
65+
[_httpManager post:@"http://httpbin.org/status/418" withData:nil withParams:nil withHeaders:nil withCompleteBlock: ^(QNResponseInfo *info, NSDictionary *resp) {
66+
testInfo = info;
67+
} withProgressBlock:nil withCancelBlock:nil];
68+
69+
AGWW_WAIT_WHILE(testInfo == nil, 100.0);
70+
NSLog(@"%@", testInfo);
71+
XCTAssert(testInfo.statusCode == 418, @"Pass");
72+
XCTAssert(testInfo.error != nil, @"Pass");
73+
74+
testInfo = nil;
75+
[_httpManager post:@"http://httpbin.org/status/200" withData:nil withParams:nil withHeaders:nil withCompleteBlock: ^(QNResponseInfo *info, NSDictionary *resp) {
76+
testInfo = info;
77+
} withProgressBlock:nil withCancelBlock:nil];
78+
79+
AGWW_WAIT_WHILE(testInfo == nil, 100.0);
80+
NSLog(@"%@", testInfo);
81+
XCTAssert(testInfo.statusCode == 200, @"Pass");
82+
XCTAssert(!testInfo.isOK, @"Pass");
83+
XCTAssert(testInfo.error != nil, @"Pass");
84+
}
85+
86+
@end
87+
88+
#endif

0 commit comments

Comments
 (0)