mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-14 23:28:07 +08:00
Refactored duplicated code for returning the MIME Type based on file path extension using Core Services UTI.
90 lines
3.5 KiB
Objective-C
90 lines
3.5 KiB
Objective-C
//
|
|
// RKURLSpec.m
|
|
// RestKit
|
|
//
|
|
// Created by Blake Watters on 6/29/11.
|
|
// Copyright 2011 RestKit
|
|
//
|
|
// 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 "RKSpecEnvironment.h"
|
|
#import "RKURL.h"
|
|
#import "NSURL+RestKit.h"
|
|
|
|
@interface RKURLSpec : RKSpec
|
|
@end
|
|
|
|
@implementation RKURLSpec
|
|
|
|
- (void)itShouldNotExplodeBecauseOfUnicodeCharacters {
|
|
NSException* failed = nil;
|
|
@try {
|
|
[RKURL URLWithBaseURLString:@"http://test.com" resourcePath:@"/places.json?category=Automóviles"];
|
|
}
|
|
@catch (NSException *exception) {
|
|
failed = exception;
|
|
}
|
|
@finally {
|
|
NSAssert((failed == nil), @"No exception should be generated by creating URL with Unicode chars");
|
|
}
|
|
}
|
|
|
|
- (void)itShouldEscapeQueryParameters {
|
|
NSDictionary* queryParams = [NSDictionary dictionaryWithObjectsAndKeys:@"What is your #1 e-mail?", @"question", @"john+restkit@gmail.com", @"answer", nil];
|
|
RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org" resourcePath:@"/test" queryParams:queryParams];
|
|
assertThat([URL absoluteString], is(equalTo(@"http://restkit.org/test?answer=john%2Brestkit%40gmail.com&question=What%20is%20your%20%231%20e-mail%3F")));
|
|
}
|
|
|
|
- (void)itShouldHandleNilQueryParameters {
|
|
RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org" resourcePath:@"/test" queryParams:nil];
|
|
assertThat([URL absoluteString], is(equalTo(@"http://restkit.org/test")));
|
|
}
|
|
|
|
- (void)itShouldHandleEmptyQueryParameters {
|
|
RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org" resourcePath:@"/test" queryParams:[NSDictionary dictionary]];
|
|
assertThat([URL absoluteString], is(equalTo(@"http://restkit.org/test")));
|
|
}
|
|
|
|
- (void)itShouldHandleResourcePathWithoutLeadingSlash {
|
|
RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org" resourcePath:@"test"];
|
|
assertThat([URL absoluteString], is(equalTo(@"http://restkit.org/test")));
|
|
}
|
|
|
|
- (void)itShouldHandleEmptyResourcePath {
|
|
RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org" resourcePath:@""];
|
|
assertThat([URL absoluteString], is(equalTo(@"http://restkit.org")));
|
|
}
|
|
|
|
- (void)itShouldHandleBaseURLsWithAPath {
|
|
RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org/this" resourcePath:@"/test" queryParams:nil];
|
|
assertThat([URL absoluteString], is(equalTo(@"http://restkit.org/this/test")));
|
|
}
|
|
|
|
- (void)itShouldSimplifyURLsWithSeveralSlashes {
|
|
RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org//this//" resourcePath:@"/test" queryParams:nil];
|
|
assertThat([URL absoluteString], is(equalTo(@"http://restkit.org/this/test")));
|
|
}
|
|
|
|
- (void)itShouldPreserveTrailingSlash {
|
|
RKURL* URL = [RKURL URLWithBaseURLString:@"http://restkit.org" resourcePath:@"/test/" queryParams:nil];
|
|
assertThat([URL absoluteString], is(equalTo(@"http://restkit.org/test/")));
|
|
}
|
|
|
|
- (void)itShouldReturnTheMIMETypeForURL {
|
|
NSURL *URL = [NSURL URLWithString:@"http://restkit.org/path/to/resource.xml"];
|
|
assertThat([URL MIMETypeForPathExtension], is(equalTo(@"application/xml")));
|
|
}
|
|
|
|
@end
|
|
|