Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
shopify_iossdk
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cemarose
shopify_iossdk
Commits
7b5cf27c
Commit
7b5cf27c
authored
May 18, 2016
by
Dima Bart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tests for polling.
parent
7542270b
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
89 additions
and
9 deletions
+89
-9
BUYRequestOperationTests.m
...e Buy SDK/Mobile Buy SDK Tests/BUYRequestOperationTests.m
+85
-6
BUYRequestOperation.h
...e Buy SDK/Mobile Buy SDK/Operations/BUYRequestOperation.h
+4
-3
No files found.
Mobile Buy SDK/Mobile Buy SDK Tests/BUYRequestOperationTests.m
View file @
7b5cf27c
...
...
@@ -9,6 +9,7 @@
#import <XCTest/XCTest.h>
#import <OHHTTPStubs/OHHTTPStubs.h>
#import "BUYRequestOperation.h"
#import "BUYClient.h"
@interface
BUYRequestOperationTests
:
XCTestCase
...
...
@@ -69,7 +70,7 @@
};
NSData
*
payloadData
=
[
NSJSONSerialization
dataWithJSONObject
:
payload
options
:
0
error
:
nil
];
OHHTTPStubsResponse
*
response
=
[
OHHTTPStubsResponse
responseWithData
:
payloadData
statusCode
:
200
headers
:
nil
];
OHHTTPStubsResponse
*
response
=
[
OHHTTPStubsResponse
responseWithData
:
payloadData
statusCode
:
BUYStatusComplete
headers
:
nil
];
[
OHHTTPStubs
stubRequestsPassingTest
:
^
BOOL
(
NSURLRequest
*
request
)
{
return
YES
;
...
...
@@ -85,7 +86,7 @@
XCTAssertNil
(
error
);
XCTAssertEqualObjects
(
json
,
payload
);
XCTAssertEqual
(
response
.
statusCode
,
200
);
XCTAssertEqual
(
response
.
statusCode
,
BUYStatusComplete
);
}];
[
self
.
queue
addOperation
:
operation
];
...
...
@@ -102,7 +103,7 @@
};
NSData
*
payloadData
=
[
NSJSONSerialization
dataWithJSONObject
:
errorPayload
options
:
0
error
:
nil
];
OHHTTPStubsResponse
*
response
=
[
OHHTTPStubsResponse
responseWithData
:
payloadData
statusCode
:
400
headers
:
nil
];
OHHTTPStubsResponse
*
response
=
[
OHHTTPStubsResponse
responseWithData
:
payloadData
statusCode
:
BUYStatusFailed
headers
:
nil
];
[
OHHTTPStubs
stubRequestsPassingTest
:
^
BOOL
(
NSURLRequest
*
request
)
{
return
YES
;
...
...
@@ -118,7 +119,7 @@
XCTAssertNotNil
(
error
);
XCTAssertEqualObjects
(
error
.
userInfo
,
errorPayload
);
XCTAssertEqual
(
response
.
statusCode
,
400
);
XCTAssertEqual
(
response
.
statusCode
,
BUYStatusFailed
);
}];
[
self
.
queue
addOperation
:
operation
];
...
...
@@ -190,6 +191,44 @@
XCTAssertEqualObjects
(
container
,
@"1134"
);
}
-
(
void
)
testPollingActivatedWithHandler
{
[
self
stubRequestsWithDelay
:
0
.
1
status
:
BUYStatusProcessing
];
XCTestExpectation
*
completion
=
[
self
expectationWithDescription
:
@"Should complete after polling"
];
BUYRequestOperation
*
operation
=
[
self
operationFulfillingExpectation
:
nil
completion
:
^
{
[
completion
fulfill
];
}];
__block
int
pollCount
=
0
;
XCTestExpectation
*
expectation
=
[
self
expectationWithDescription
:
@"Should stop polling at 10 iterations"
];
operation
.
pollingHandler
=
^
BOOL
(
NSDictionary
*
json
,
NSHTTPURLResponse
*
response
,
NSError
*
error
)
{
[
self
stubRequestsWithDelay
:
0
.
1
status
:
BUYStatusProcessing
];
XCTAssertNotNil
(
json
);
XCTAssertNotNil
(
response
);
XCTAssertNil
(
error
);
if
(
response
.
statusCode
==
BUYStatusComplete
)
{
[
expectation
fulfill
];
}
if
(
response
.
statusCode
==
BUYStatusProcessing
)
{
pollCount
+=
1
;
if
(
pollCount
==
10
)
{
[
self
stubRequestsWithDelay
:
0
.
1
status
:
BUYStatusComplete
];
}
return
YES
;
}
return
NO
;
};
[
self
.
queue
addOperation
:
operation
];
[
self
waitForExpectationsWithTimeout
:
5
.
0
handler
:
nil
];
}
-
(
void
)
testCancellationBeforeExecution
{
[
self
stubRequests
];
...
...
@@ -223,6 +262,32 @@
[
self
waitForExpectationsWithTimeout
:
4
.
0
handler
:
nil
];
}
-
(
void
)
testCancellationDuringPolling
{
[
self
stubRequestsWithDelay
:
0
.
1
status
:
BUYStatusProcessing
];
BUYRequestOperation
*
operation
=
[
self
operationFulfillingExpectation
:
nil
completion
:
^
{
XCTAssert
(
NO
,
@"Operation should not call completion if cancelled."
);
}];
__block
int
pollCount
=
0
;
operation
.
pollingHandler
=
^
BOOL
(
NSDictionary
*
json
,
NSHTTPURLResponse
*
response
,
NSError
*
error
)
{
pollCount
+=
1
;
return
YES
;
};
[
self
.
queue
addOperation
:
operation
];
[
self
after
:
0
.
5
block
:
^
{
[
operation
cancel
];
}];
[
self
createExpectationDelay
:
1
.
0
block
:
YES
];
XCTAssertTrue
(
pollCount
<
5
);
}
#pragma mark - Convenience -
-
(
void
)
asyncMain
:
(
dispatch_block_t
)
block
...
...
@@ -240,12 +305,21 @@
[
self
createExpectationDelay
:
1
.
0
];
}
-
(
void
)
createExpectationDelay
:
(
NSTimeInterval
)
delay
-
(
void
)
createExpectationDelay
:
(
NSTimeInterval
)
delay
{
[
self
createExpectationDelay
:
delay
block
:
NO
];
}
-
(
void
)
createExpectationDelay
:
(
NSTimeInterval
)
delay
block
:
(
BOOL
)
block
{
XCTestExpectation
*
expectation
=
[
self
expectationWithDescription
:
@"Delay"
];
[
self
after
:
delay
block
:
^
{
[
expectation
fulfill
];
}];
if
(
block
)
{
[
self
waitForExpectationsWithTimeout
:
delay
+
0
.
1
handler
:
nil
];
}
}
-
(
BUYRequestOperation
*
)
operationFulfillingExpectation
:
(
XCTestExpectation
*
)
expectation
completion
:
(
dispatch_block_t
)
completion
...
...
@@ -280,13 +354,18 @@
-
(
void
)
stubRequestsWithDelay
:
(
NSTimeInterval
)
delay
{
[
self
stubRequestsWithDelay
:
delay
status
:
BUYStatusProcessing
];
}
-
(
void
)
stubRequestsWithDelay
:
(
NSTimeInterval
)
delay
status
:
(
int
)
status
{
NSDictionary
*
payload
=
@{
@"first_name"
:
@"John"
,
@"last_name"
:
@"Smith"
,
};
NSData
*
payloadData
=
[
NSJSONSerialization
dataWithJSONObject
:
payload
options
:
0
error
:
nil
];
OHHTTPStubsResponse
*
response
=
[
OHHTTPStubsResponse
responseWithData
:
payloadData
statusCode
:
200
headers
:
nil
];
OHHTTPStubsResponse
*
response
=
[
OHHTTPStubsResponse
responseWithData
:
payloadData
statusCode
:
status
headers
:
nil
];
response
.
requestTime
=
delay
;
[
OHHTTPStubs
stubRequestsPassingTest
:
^
BOOL
(
NSURLRequest
*
request
)
{
...
...
Mobile Buy SDK/Mobile Buy SDK/Operations/BUYRequestOperation.h
View file @
7b5cf27c
...
...
@@ -29,14 +29,15 @@ NS_ASSUME_NONNULL_BEGIN
@protocol
BUYSerializable
;
typedef
void
(
^
BUYRequestOperationCompletion
)(
NSDictionary
*
_Nullable
json
,
NSURLResponse
*
_Nullable
response
,
NSError
*
_Nullable
error
);
typedef
BOOL
(
^
BUYRequestOperationPollingHandler
)(
NSDictionary
*
_Nullable
json
,
NSURLResponse
*
_Nullable
response
,
NSError
*
_Nullable
error
);
typedef
void
(
^
BUYRequestOperationCompletion
)(
NSDictionary
*
_Nullable
json
,
NS
HTTP
URLResponse
*
_Nullable
response
,
NSError
*
_Nullable
error
);
typedef
BOOL
(
^
BUYRequestOperationPollingHandler
)(
NSDictionary
*
_Nullable
json
,
NS
HTTP
URLResponse
*
_Nullable
response
,
NSError
*
_Nullable
error
);
@interface
BUYRequestOperation
:
BUYOperation
@property
(
strong
,
nonatomic
,
readonly
,
nonnull
)
NSURLSession
*
session
;
@property
(
strong
,
nonatomic
,
readonly
,
nonnull
)
NSURLRequest
*
originalRequest
;
@property
(
strong
,
nonatomic
,
readonly
,
nullable
)
BUYRequestOperationPollingHandler
pollingHandler
;
@property
(
strong
,
nonatomic
,
nullable
)
BUYRequestOperationPollingHandler
pollingHandler
;
+
(
instancetype
)
operationWithSession
:(
NSURLSession
*
)
session
request
:(
NSURLRequest
*
)
request
payload
:(
id
<
BUYSerializable
>
_Nullable
)
payload
completion
:(
BUYRequestOperationCompletion
)
completion
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment