Files
RestKit/Tests/Logic/ObjectMapping/RKParserRegistryTest.m
Blake Watters 10377d0a70 Overhaul RKParser API's for clarity and simplicity:
* Adds support for NSJSONSerialization. closes #476
* Renames RKParser to RKSerialization
* Renames RKParserRegistry to RKMIMETypeSerialization
* Rework serialization implementations API's to use class methods
* Fold together string and regular expression matching for MIME Type registrations
* Port serialization implementations to working off of NSData instead of NSString. refs #762
* Migrate JSONKit to being an optional component. See https://github.com/RestKit/RKJSONKitSerialization
2012-09-04 13:07:31 -04:00

95 lines
3.8 KiB
Objective-C

//
// RKParserRegistryTest.m
// RestKit
//
// Created by Blake Watters on 5/18/11.
// 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 "RKTestEnvironment.h"
#import "RKParserRegistry.h"
#import "RKJSONParserJSONKit.h"
#import "RKXMLParserXMLReader.h"
@interface RKParserRegistryTest : RKTestCase {
}
@end
@implementation RKParserRegistryTest
- (void)testShouldEnableRegistrationFromMIMETypeToParserClasses
{
RKParserRegistry *registry = [[RKParserRegistry new] autorelease];
[registry setParserClass:[RKJSONParserJSONKit class] forMIMEType:RKMIMETypeJSON];
Class parserClass = [registry parserClassForMIMEType:RKMIMETypeJSON];
assertThat(NSStringFromClass(parserClass), is(equalTo(@"RKJSONParserJSONKit")));
}
- (void)testShouldInstantiateParserObjects
{
RKParserRegistry *registry = [[RKParserRegistry new] autorelease];
[registry setParserClass:[RKJSONParserJSONKit class] forMIMEType:RKMIMETypeJSON];
id<RKSerialization> parser = [registry parserForMIMEType:RKMIMETypeJSON];
assertThat(parser, is(instanceOf([RKJSONParserJSONKit class])));
}
- (void)testShouldAutoconfigureBasedOnReflection
{
RKParserRegistry *registry = [[RKParserRegistry new] autorelease];
[registry autoconfigure];
id<RKSerialization> parser = [registry parserForMIMEType:RKMIMETypeJSON];
assertThat(parser, is(instanceOf([RKJSONParserJSONKit class])));
parser = [registry parserForMIMEType:RKMIMETypeXML];
assertThat(parser, is(instanceOf([RKXMLParserXMLReader class])));
}
- (void)testRetrievalOfExactStringMatchForMIMEType
{
RKParserRegistry *registry = [[RKParserRegistry new] autorelease];
[registry setParserClass:[RKJSONParserJSONKit class] forMIMEType:RKMIMETypeJSON];
id<RKSerialization> parser = [registry parserForMIMEType:RKMIMETypeJSON];
assertThat(parser, is(instanceOf([RKJSONParserJSONKit class])));
}
- (void)testRetrievalOfRegularExpressionMatchForMIMEType
{
RKParserRegistry *registry = [[RKParserRegistry new] autorelease];
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"application/xml\\+\\w+" options:0 error:&error];
[registry setParserClass:[RKJSONParserJSONKit class] forMIMETypeRegularExpression:regex];
id<RKSerialization> parser = [registry parserForMIMEType:@"application/xml+whatever"];
assertThat(parser, is(instanceOf([RKJSONParserJSONKit class])));
}
- (void)testRetrievalOfExactStringMatchIsFavoredOverRegularExpression
{
RKParserRegistry *registry = [[RKParserRegistry new] autorelease];
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"application/xml\\+\\w+" options:0 error:&error];
[registry setParserClass:[RKJSONParserJSONKit class] forMIMETypeRegularExpression:regex];
[registry setParserClass:[RKXMLParserXMLReader class] forMIMEType:@"application/xml+whatever"];
// Exact match
id<RKSerialization> exactParser = [registry parserForMIMEType:@"application/xml+whatever"];
assertThat(exactParser, is(instanceOf([RKXMLParserXMLReader class])));
// Fallback to regex
id<RKSerialization> regexParser = [registry parserForMIMEType:@"application/xml+different"];
assertThat(regexParser, is(instanceOf([RKJSONParserJSONKit class])));
}
@end