Skip to content

Commit

Permalink
Add tests and support for using bitwise specifiers for `enqueuedObjec…
Browse files Browse the repository at this point in the history
…tRequestOperationsWithMethod:matchingPathPattern:` refs RestKit#1476
  • Loading branch information
Blake Watters authored and 4PixelsDev committed Dec 12, 2013
1 parent 2405949 commit 0a6db0b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Code/Network/RKObjectManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ RKMappingResult, RKRequestDescriptor, RKResponseDescriptor;
Paths are matches against the `path` of the `NSURL` of the `NSURLRequest` of each `RKObjectRequestOperation` contained in the receiver's operation queue using a `RKPathMatcher` object.
@param method The HTTP method to match for the cancelled requests, such as `RKRequestMethodGET`, `RKRequestMethodPOST`, `RKRequestMethodPUT`, `RKRequestMethodPatch`, or `RKRequestMethodDELETE`. If `RKRequestMethodAny`, all object request operations with URLs matching the given path pattern will be cancelled.
@param method The HTTP method to match for the cancelled requests, such as `RKRequestMethodGET`, `RKRequestMethodPOST`, `RKRequestMethodPUT`, `RKRequestMethodPatch`, or `RKRequestMethodDELETE`. If `RKRequestMethodAny`, all object request operations with URLs matching the given path pattern will be cancelled. Multiple methods may be specified by using a bitwise OR operation.
@param pathPattern The pattern to match against the path of the request URL for executing object request operations considered for cancellation.
@return A new array containing all enqueued `RKObjectRequestOperation` objects that match the given HTTP method and path pattern.
@see `RKPathMatcher`
Expand Down
6 changes: 3 additions & 3 deletions Code/Network/RKObjectManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -820,16 +820,16 @@ - (void)enqueueObjectRequestOperation:(RKObjectRequestOperation *)objectRequestO
- (NSArray *)enqueuedObjectRequestOperationsWithMethod:(RKRequestMethod)method matchingPathPattern:(NSString *)pathPattern
{
NSMutableArray *matches = [NSMutableArray array];
NSString *methodName = RKStringFromRequestMethod(method);
RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:pathPattern];
for (NSOperation *operation in [self.operationQueue operations]) {
if (![operation isKindOfClass:[RKObjectRequestOperation class]]) {
continue;
}
NSURLRequest *request = [(RKObjectRequestOperation *)operation HTTPRequestOperation].request;
NSString *pathAndQueryString = RKPathAndQueryStringFromURLRelativeToURL([request URL], self.baseURL);

if ((!methodName || [methodName isEqualToString:[request HTTPMethod]]) && [pathMatcher matchesPath:pathAndQueryString tokenizeQueryStrings:NO parsedArguments:nil]) {

RKRequestMethod operationMethod = RKRequestMethodFromString([request HTTPMethod]);
if ((method & operationMethod) && [pathMatcher matchesPath:pathAndQueryString tokenizeQueryStrings:NO parsedArguments:nil]) {
[matches addObject:operation];
}
}
Expand Down
20 changes: 20 additions & 0 deletions Tests/Logic/ObjectMapping/RKObjectManagerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,26 @@ - (void)testEnqueuedObjectRequestOperationByPathMatchForBaseURLWithPath
expect([[_objectManager enqueuedObjectRequestOperationsWithMethod:RKRequestMethodGET matchingPathPattern:@":objectID/cancel"] count]).to.equal(1);
}

- (void)testEnqueuedObjectRequestOperationByMultipleBitmaskMethodAndPath
{
NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/object_manager/cancel" relativeToURL:self.objectManager.HTTPClient.baseURL]];
NSMutableURLRequest *request2 = [request1 mutableCopy];
request2.HTTPMethod = @"POST";
NSMutableURLRequest *request3 = [request1 mutableCopy];
request3.HTTPMethod = @"DELETE";
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request1 responseDescriptors:self.objectManager.responseDescriptors];

RKObjectRequestOperation *secondOperation = [[RKObjectRequestOperation alloc] initWithRequest:request2 responseDescriptors:self.objectManager.responseDescriptors];
RKObjectRequestOperation *thirdOperation = [[RKObjectRequestOperation alloc] initWithRequest:request3 responseDescriptors:self.objectManager.responseDescriptors];
[_objectManager enqueueObjectRequestOperation:operation];
[_objectManager enqueueObjectRequestOperation:secondOperation];
[_objectManager enqueueObjectRequestOperation:thirdOperation];
NSArray *operations = [_objectManager enqueuedObjectRequestOperationsWithMethod:RKRequestMethodGET | RKRequestMethodPOST matchingPathPattern:@"/object_manager/cancel"];
expect(operations).to.haveCountOf(2);
expect(operations).to.contain(operation);
expect(operations).to.contain(secondOperation);
}

- (void)testShouldProperlyFireABatchOfOperations
{
NSManagedObjectContext *managedObjectContext = [[RKTestFactory managedObjectStore] persistentStoreManagedObjectContext];
Expand Down

0 comments on commit 0a6db0b

Please sign in to comment.