Skip to content

Instantly share code, notes, and snippets.

@rcgary
Last active May 12, 2016 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcgary/148a2268b218f0954441e42be4ac20b4 to your computer and use it in GitHub Desktop.
Save rcgary/148a2268b218f0954441e42be4ac20b4 to your computer and use it in GitHub Desktop.
//
//
// Created by Chao Ruan on 23/04/2015.
//
#import "HKTStubber.h"
#import "Mantle/Mantle.h"
@implementation HKTStubber
+ (void)stubResponseWithPath:(NSString*)path responseFilename:(NSString*)responseFilename statusCode:(int)statusCode headers:(NSDictionary*)headers jsonObject:(id)jsonObject
{
if (responseFilename != nil) {
headers = [headers mtl_dictionaryByAddingEntriesFromDictionary:
@{
@"Content-Type": [self isXMLFile:responseFilename] ? @"application/smil+xml" : @"application/json",
}];
}
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [request.URL.path isEqual:[@"/" stringByAppendingString:path]];
}withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request){
if (responseFilename) {
NSString* fixture = OHPathForFile(responseFilename, HKTStubber.class);
return [OHHTTPStubsResponse responseWithFileAtPath:fixture
statusCode:statusCode
headers:headers];
}
else if (jsonObject)
{
return [OHHTTPStubsResponse responseWithJSONObject:jsonObject
statusCode:statusCode
headers:headers];
}
else {
return [OHHTTPStubsResponse responseWithData:[NSData data]
statusCode:statusCode
headers:headers];
}
}];
}
+ (void)stubResponseWithPath:(NSString*)path responseFilename:(NSString*)responseFilename
{
[HKTStubber stubResponseWithPath:path
responseFilename:responseFilename
statusCode:200
headers:@{}
jsonObject:nil];
}
+ (void)stubResponseNoContentWithPath:(NSString *)path
{
[HKTStubber stubResponseWithPath:path
responseFilename:nil
statusCode:204
headers:@{}
jsonObject:nil];
}
+ (void)stubResponseWithPath:(NSString *)path responseObject:(id)jsonObject
{
[HKTStubber stubResponseWithPath:path
responseFilename:nil
statusCode:200
headers:@{}
jsonObject:jsonObject];
}
+ (void)stubForbiddenResponseWithPath:(NSString*)path responseFilename:(NSString*)responseFilename
{
[HKTStubber stubResponseWithPath:path
responseFilename:responseFilename
statusCode:403
headers:@{}
jsonObject:nil];
}
+ (void)stubFailureThenSuccessResponseWithPath:(NSString*)path responseFilename:(NSString*)responseFilename
{
[OHHTTPStubs onStubActivation:^(NSURLRequest * _Nonnull request, id<OHHTTPStubsDescriptor> _Nonnull stub) {
//After the stub has been activated once, remove the current stub and replace it with a success response stub.
[OHHTTPStubs removeStub:stub];
[OHHTTPStubs onStubActivation:^(NSURLRequest * _Nonnull request, id<OHHTTPStubsDescriptor> _Nonnull stub) {}];
[HKTStubber stubResponseWithPath:path responseFilename:responseFilename];
}];
[self stubForbiddenResponseWithPath:path responseFilename:nil]; //Stub a forbidden response the first time "path" is called
}
#pragma mark - Private Methods
+(BOOL) isXMLFile:(NSString*)filename
{
return [[NSPredicate predicateWithFormat:@"SELF MATCHES %@", @".*\\.xml$"] evaluateWithObject:filename];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment