mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-23 12:27:52 +08:00
Remove RKTestResponseLoader
This commit is contained in:
@@ -8,6 +8,5 @@
|
||||
|
||||
#import "RKTestFixture.h"
|
||||
#import "RKTestNotificationObserver.h"
|
||||
#import "RKTestResponseLoader.h"
|
||||
#import "RKTestFactory.h"
|
||||
#import "RKMappingTest.h"
|
||||
|
||||
@@ -1,107 +0,0 @@
|
||||
//
|
||||
// RKTestResponseLoader.h
|
||||
// RestKit
|
||||
//
|
||||
// Created by Blake Watters on 1/14/10.
|
||||
// Copyright (c) 2009-2012 RestKit. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
/**
|
||||
An RKTestResponseLoader object provides testing support for asynchronously loading an RKRequest or
|
||||
RKObjectLoader object while blocking the execution of the current thread by spinning the run loop.
|
||||
This enables a straight-forward unit testing workflow for asynchronous network operations.
|
||||
|
||||
RKTestResponseLoader instances are designed to act as as the delegate for an RKObjectLoader or RKRequest
|
||||
object under test. Once assigned as the delegate to a request and the request has been sent,
|
||||
waitForResponse: is invoked to block execution until the response is loaded.
|
||||
*/
|
||||
@interface RKTestResponseLoader : NSObject
|
||||
|
||||
/**
|
||||
The RKResponse object loaded from the RKRequest or RKObjectLoader the receiver is acting as the delegate for.
|
||||
**/
|
||||
@property (nonatomic, strong, readonly) NSHTTPURLResponse *response;
|
||||
|
||||
/**
|
||||
The collection of objects loaded from the RKObjectLoader the receiver is acting as the delegate for.
|
||||
*/
|
||||
@property (nonatomic, strong, readonly) NSArray *objects;
|
||||
|
||||
/**
|
||||
A Boolean value that indicates whether a response was loaded successfully.
|
||||
|
||||
@return YES if a response was loaded successfully.
|
||||
*/
|
||||
@property (nonatomic, readonly, getter = wasSuccessful) BOOL successful;
|
||||
|
||||
/**
|
||||
A Boolean value that indicates whether the RKRequest or RKObjectLoader the receiver is acting as the delegate for was cancelled.
|
||||
|
||||
@return YES if the request was cancelled
|
||||
*/
|
||||
@property (nonatomic, readonly, getter = wasCancelled) BOOL cancelled;
|
||||
|
||||
/**
|
||||
A Boolean value that indicates if an unexpected response was loaded.
|
||||
|
||||
@return YES if the request loaded an unknown response.
|
||||
@see [RKObjectLoaderDelegate objectLoaderDidLoadUnexpectedResponse:]
|
||||
*/
|
||||
@property (nonatomic, readonly, getter = loadedUnexpectedResponse) BOOL unexpectedResponse;
|
||||
|
||||
/**
|
||||
An NSError value that was loaded from the RKRequest or RKObjectLoader the receiver is acting as the delegate for.
|
||||
|
||||
@see [RKRequestDelegate request:didFailLoadWithError:]
|
||||
@see [RKObjectLoaderDelegate objectLoader:didFailWithError:]
|
||||
*/
|
||||
@property (nonatomic, copy, readonly) NSError *error;
|
||||
|
||||
/**
|
||||
The timeout interval, in seconds, to wait for a response to load.
|
||||
|
||||
The default value is 4 seconds.
|
||||
|
||||
@see [RKTestResponseLoader waitForResponse]
|
||||
*/
|
||||
@property (nonatomic, assign) NSTimeInterval timeout;
|
||||
|
||||
/**
|
||||
Creates and returns a test response loader object.
|
||||
|
||||
@return A new response loader object.
|
||||
*/
|
||||
+ (id)responseLoader;
|
||||
|
||||
/**
|
||||
Waits for an asynchronous RKRequest or RKObjectLoader network operation to load a response
|
||||
by spinning the current run loop to block the current thread of execution.
|
||||
|
||||
The wait operation is guarded by a timeout
|
||||
*/
|
||||
- (void)waitForResponse;
|
||||
|
||||
/**
|
||||
Returns the localized description error message for the error.
|
||||
|
||||
TODO: Why not just move this to NSError+RKAdditions?
|
||||
|
||||
@return The localized description of the error or nil.
|
||||
*/
|
||||
- (NSString *)errorMessage;
|
||||
|
||||
@end
|
||||
@@ -1,153 +0,0 @@
|
||||
//
|
||||
// RKTestResponseLoader.m
|
||||
// RestKit
|
||||
//
|
||||
// Created by Blake Watters on 1/14/10.
|
||||
// Copyright (c) 2009-2012 RestKit. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#import "RKTestResponseLoader.h"
|
||||
#import "RKLog.h"
|
||||
|
||||
// Set Logging Component
|
||||
#undef RKLogComponent
|
||||
#define RKLogComponent RKlcl_cRestKitTesting
|
||||
|
||||
NSString * const RKTestResponseLoaderTimeoutException = @"RKTestResponseLoaderTimeoutException";
|
||||
|
||||
@interface RKTestResponseLoader ()
|
||||
|
||||
@property (nonatomic, assign, getter = isAwaitingResponse) BOOL awaitingResponse;
|
||||
@property (nonatomic, strong, readwrite) NSHTTPURLResponse *response;
|
||||
@property (nonatomic, copy, readwrite) NSError *error;
|
||||
@property (nonatomic, strong, readwrite) NSArray *objects;
|
||||
|
||||
@end
|
||||
|
||||
@implementation RKTestResponseLoader
|
||||
|
||||
|
||||
+ (RKTestResponseLoader *)responseLoader
|
||||
{
|
||||
return [[self alloc] init];
|
||||
}
|
||||
|
||||
- (id)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_timeout = 4;
|
||||
_awaitingResponse = NO;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
- (void)waitForResponse
|
||||
{
|
||||
_awaitingResponse = YES;
|
||||
NSDate *startDate = [NSDate date];
|
||||
|
||||
RKLogTrace(@"%@ Awaiting response loaded from for %f seconds...", self, self.timeout);
|
||||
while (_awaitingResponse) {
|
||||
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
||||
if ([[NSDate date] timeIntervalSinceDate:startDate] > self.timeout) {
|
||||
[NSException raise:RKTestResponseLoaderTimeoutException format:@"*** Operation timed out after %f seconds...", self.timeout];
|
||||
_awaitingResponse = NO;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)loadError:(NSError *)theError
|
||||
{
|
||||
_awaitingResponse = NO;
|
||||
_successful = NO;
|
||||
self.error = theError;
|
||||
}
|
||||
|
||||
- (NSString *)errorMessage
|
||||
{
|
||||
if (self.error) {
|
||||
return [[self.error userInfo] valueForKey:NSLocalizedDescriptionKey];
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
//- (void)request:(RKRequest *)request didReceiveResponse:(RKResponse *)response
|
||||
//{
|
||||
// // Implemented for expectations
|
||||
//}
|
||||
//
|
||||
//- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)aResponse
|
||||
//{
|
||||
// self.response = aResponse;
|
||||
//
|
||||
// // If request is an Object Loader, then objectLoader:didLoadObjects:
|
||||
// // will be sent after didLoadResponse:
|
||||
// if (NO == [request isKindOfClass:[RKObjectLoader class]]) {
|
||||
// _awaitingResponse = NO;
|
||||
// _successful = YES;
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//- (void)request:(RKRequest *)request didFailLoadWithError:(NSError *)anError
|
||||
//{
|
||||
// // If request is an Object Loader, then objectLoader:didFailWithError:
|
||||
// // will be sent after didFailLoadWithError:
|
||||
// if (NO == [request isKindOfClass:[RKObjectLoader class]]) {
|
||||
// [self loadError:anError];
|
||||
// }
|
||||
//
|
||||
// // Ensure we get no further delegate messages
|
||||
// [request cancel];
|
||||
//}
|
||||
//
|
||||
//- (void)requestDidCancelLoad:(RKRequest *)request
|
||||
//{
|
||||
// _awaitingResponse = NO;
|
||||
// _successful = NO;
|
||||
// _cancelled = YES;
|
||||
//}
|
||||
//
|
||||
//- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)theObjects
|
||||
//{
|
||||
// RKLogTrace(@"%@ Loaded response for %@ with body: %@", self, objectLoader, [objectLoader.response bodyAsString]);
|
||||
// RKLogDebug(@"%@ Loaded objects for %@: %@", self, objectLoader, _objects);
|
||||
// self.objects = theObjects;
|
||||
// _awaitingResponse = NO;
|
||||
// _successful = YES;
|
||||
//}
|
||||
//
|
||||
//- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)theError
|
||||
//{
|
||||
// [self loadError:theError];
|
||||
//}
|
||||
//
|
||||
//- (void)objectLoaderDidLoadUnexpectedResponse:(RKObjectLoader *)objectLoader
|
||||
//{
|
||||
// RKLogDebug(@"%@ Loaded unexpected response for: %@", self, objectLoader);
|
||||
// _successful = NO;
|
||||
// _awaitingResponse = NO;
|
||||
// _unexpectedResponse = YES;
|
||||
//}
|
||||
//
|
||||
//- (void)objectLoaderDidFinishLoading:(RKObjectLoader *)objectLoader
|
||||
//{
|
||||
// // Implemented for expectations
|
||||
//}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user