Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated to version 1.0.1

  • Loading branch information...
commit 2753dfb7b3d103b12ba5b34a9e96cc79fd14d75b 1 parent 85b6aaf
@nicklockwood authored
View
2  LICENCE.md
@@ -1,6 +1,6 @@
RequestUtils
-Version 1.0, January 16th, 2012
+Version 1.0.1, May 19th, 2013
Copyright (C) 2012 Charcoal Design
View
6 README.md
@@ -7,9 +7,9 @@ RequestUtils is a collection of category methods designed to simplify the proces
Supported OS & SDK Versions
-----------------------------
-* Supported build target - iOS 5.1 / Mac OS 10.8 (Xcode 4.4, Apple LLVM compiler 4.0)
-* Earliest supported deployment target - iOS 4.3 / Mac OS 10.7
-* Earliest compatible deployment target - iOS 3.0 / Mac OS 10.6
+* Supported build target - iOS 6.1 / Mac OS 10.8 (Xcode 4.6.2, Apple LLVM compiler 4.2)
+* Earliest supported deployment target - iOS 5.1 / Mac OS 10.7
+* Earliest compatible deployment target - iOS 4.3 / Mac OS 10.6.8
NOTE: 'Supported' means that the library has been tested with this version. 'Compatible' means that the library should work on this iOS version (i.e. it doesn't rely on any unavailable SDK features) but is no longer being tested for compatibility and may require tweaking or bug fixes to run correctly.
View
6 RELEASE NOTES.md
@@ -1,3 +1,9 @@
+Version 1.0.1
+
+- Updated for Xcode 4.6
+- URLEncoding now copes gracefully with non-string values (e.g NSNumber)
+- Fixed bug in stringByMergingURLQuery:options: method
+
Version 1.0
- Initial release.
View
13 RequestUtils.podspec
@@ -0,0 +1,13 @@
+Pod::Spec.new do |s|
+ s.name = 'RequestUtils'
+ s.version = '1.0.1'
+ s.license = 'zlib'
+ s.summary = 'RequestUtils is a collection of category methods designed to simplify the process of HTTP request construction and manipulation in Cocoa.'
+ s.homepage = 'https://github.com/nicklockwood/RequestUtils'
+ s.authors = 'Nick Lockwood'
+ s.source = { :git => 'https://github.com/nicklockwood/RequestUtils.git', :tag => '1.0.1' }
+ s.source_files = 'RequestUtils'
+ s.requires_arc = false
+ s.osx.deployment_target = '10.6'
+ s.ios.deployment_target = '4.3'
+end
View
12 RequestUtils/RequestUtils.h
@@ -1,7 +1,7 @@
//
// RequestUtils.h
//
-// Version 1.0
+// Version 1.0.1
//
// Created by Nick Lockwood on 11/01/2012.
// Copyright (C) 2012 Charcoal Design
@@ -36,11 +36,11 @@
typedef enum
{
URLQueryOptionDefault = 0,
- URLQueryOptionKeepLastValue = 1,
- URLQueryOptionKeepFirstValue = 2,
- URLQueryOptionUseArrays = 3,
- URLQueryOptionAlwaysUseArrays = 4,
- URLQueryOptionUseArraySyntax = 8
+ URLQueryOptionKeepLastValue = 1,
+ URLQueryOptionKeepFirstValue = 2,
+ URLQueryOptionUseArrays = 3,
+ URLQueryOptionAlwaysUseArrays = 4,
+ URLQueryOptionUseArraySyntax = 8
}
URLQueryOptions;
View
94 RequestUtils/RequestUtils.m
@@ -1,7 +1,7 @@
//
// RequestUtils.m
//
-// Version 1.0
+// Version 1.0.1
//
// Created by Nick Lockwood on 11/01/2012.
// Copyright (C) 2012 Charcoal Design
@@ -51,7 +51,7 @@ @implementation NSString (RequestUtils)
- (NSString *)URLEncodedString
{
CFStringRef encoded = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
- (__bridge CFStringRef)self,
+ (__bridge CFStringRef)[self description],
NULL,
CFSTR("!*'\"();:@&=+$,/?%#[]% "),
kCFStringEncodingUTF8);
@@ -60,7 +60,7 @@ - (NSString *)URLEncodedString
- (NSString *)URLDecodedString:(BOOL)decodePlusAsSpace
{
- NSString *string = self;
+ NSString *string = [self description];
if (decodePlusAsSpace)
{
string = [string stringByReplacingOccurrencesOfString:@"+" withString:@" "];
@@ -177,7 +177,7 @@ + (NSString *)URLQueryWithParameters:(NSDictionary *)parameters options:(URLQuer
for (NSString *key in parameters)
{
NSString *encodedKey = [key URLEncodedString];
- id value = [parameters objectForKey:key];
+ id value = parameters[key];
if ([value isKindOfClass:[NSArray class]])
{
if (arrayHandling == URLQueryOptionKeepFirstValue && [value count])
@@ -186,7 +186,7 @@ + (NSString *)URLQueryWithParameters:(NSDictionary *)parameters options:(URLQuer
{
[result appendString:@"&"];
}
- [result appendFormat:@"%@=%@", encodedKey, [[value objectAtIndex:0] URLEncodedString]];
+ [result appendFormat:@"%@=%@", encodedKey, [value[0] URLEncodedString]];
}
else if (arrayHandling == URLQueryOptionKeepLastValue && [value count])
{
@@ -354,8 +354,8 @@ - (NSString *)stringByMergingURLQuery:(NSString *)query options:(URLQueryOptions
NSDictionary *newParameters = [query URLQueryParametersWithOptions:options];
for (NSString *key in newParameters)
{
- id value = [newParameters objectForKey:key];
- id oldValue = [parameters objectForKey:key];
+ id value = newParameters[key];
+ id oldValue = parameters[key];
if ([oldValue isKindOfClass:[NSArray class]])
{
if ([value isKindOfClass:[NSArray class]])
@@ -371,7 +371,7 @@ - (NSString *)stringByMergingURLQuery:(NSString *)query options:(URLQueryOptions
{
if ([value isKindOfClass:[NSArray class]])
{
- value = [[NSArray arrayWithObject:oldValue] arrayByAddingObjectsFromArray:value];
+ value = [@[oldValue] arrayByAddingObjectsFromArray:value];
}
else if (arrayHandling == URLQueryOptionKeepFirstValue)
{
@@ -380,21 +380,17 @@ - (NSString *)stringByMergingURLQuery:(NSString *)query options:(URLQueryOptions
else if (arrayHandling == URLQueryOptionUseArrays ||
arrayHandling == URLQueryOptionAlwaysUseArrays)
{
- value = [NSArray arrayWithObjects:oldValue, value, nil];
+ value = @[oldValue, value];
}
}
else if (arrayHandling == URLQueryOptionAlwaysUseArrays)
{
- value = [NSArray arrayWithObject:value];
+ value = @[value];
}
- [parameters setObject:value forKey:key];
+ parameters[key] = value;
}
-
- NSRange queryRange = [self rangeOfURLQuery];
- NSString *prefix = [self substringToIndex:queryRange.location];
- NSString *suffix = [self substringFromIndex:queryRange.location + queryRange.length];
- return [prefix stringByAppendingFormat:@"%@%@",
- [NSString URLQueryWithParameters:parameters options:options], suffix];
+
+ return [self stringByReplacingURLQueryWithQuery:[NSString URLQueryWithParameters:parameters options:options]];
}
- (NSDictionary *)URLQueryParameters
@@ -413,16 +409,16 @@ - (NSDictionary *)URLQueryParametersWithOptions:(URLQueryOptions)options
for (NSString *parameter in parameters)
{
NSArray *parts = [parameter componentsSeparatedByString:@"="];
- NSString *key = [[parts objectAtIndex:0] URLDecodedString:YES];
+ NSString *key = [parts[0] URLDecodedString:YES];
if ([parts count] > 1)
{
- id value = [[parts objectAtIndex:1] URLDecodedString:YES];
+ id value = [parts[1] URLDecodedString:YES];
BOOL arrayValue = [key hasSuffix:@"[]"];
if (arrayValue)
{
key = [key substringToIndex:[key length] - 2];
}
- id existingValue = [result objectForKey:key];
+ id existingValue = result[key];
if ([existingValue isKindOfClass:[NSArray class]])
{
value = [existingValue arrayByAddingObject:value];
@@ -435,15 +431,15 @@ - (NSDictionary *)URLQueryParametersWithOptions:(URLQueryOptions)options
}
else if (arrayHandling != URLQueryOptionKeepLastValue)
{
- value = [NSArray arrayWithObjects:existingValue, value, nil];
+ value = @[existingValue, value];
}
}
else if ((arrayValue && arrayHandling == URLQueryOptionUseArrays) ||
arrayHandling == URLQueryOptionAlwaysUseArrays)
{
- value = [NSArray arrayWithObject:value];
+ value = @[value];
}
- [result setObject:value forKey:key];
+ result[key] = value;
}
}
return result;
@@ -543,14 +539,22 @@ - (NSString *)base64EncodedString
}
//truncate data to match actual output length
- outputBytes = realloc(outputBytes, outputLength);
- NSString *result = [[NSString alloc] initWithBytesNoCopy:outputBytes length:outputLength encoding:NSASCIIStringEncoding freeWhenDone:YES];
-
+ if (outputLength)
+ {
+ outputBytes = realloc(outputBytes, outputLength);
+ NSString *result = [[NSString alloc] initWithBytesNoCopy:outputBytes length:outputLength encoding:NSASCIIStringEncoding freeWhenDone:YES];
+
#if !__has_feature(objc_arc)
- [result autorelease];
+ [result autorelease];
#endif
-
- return (outputLength >= 4)? result: nil;
+
+ return (outputLength >= 4)? result: nil;
+ }
+ else
+ {
+ free(outputBytes);
+ return nil;
+ }
}
- (NSString *)base64DecodedString
@@ -626,12 +630,12 @@ @implementation NSURL (RequestUtils)
+ (NSURL *)URLWithComponents:(NSDictionary *)components
{
NSString *URL = @"";
- NSString *fragment = [components objectForKey:URLFragmentComponent];
+ NSString *fragment = components[URLFragmentComponent];
if (fragment)
{
URL = [NSString stringWithFormat:@"#%@", fragment];
}
- NSString *query = [components objectForKey:URLQueryComponent];
+ NSString *query = components[URLQueryComponent];
if (query)
{
if ([query isKindOfClass:[NSDictionary class]])
@@ -640,37 +644,37 @@ + (NSURL *)URLWithComponents:(NSDictionary *)components
}
URL = [NSString stringWithFormat:@"?%@%@", query, URL];
}
- NSString *parameterString = [components objectForKey:URLParameterStringComponent];
+ NSString *parameterString = components[URLParameterStringComponent];
if (parameterString)
{
URL = [NSString stringWithFormat:@";%@%@", parameterString, URL];
}
- NSString *path = [components objectForKey:URLPathComponent];
+ NSString *path = components[URLPathComponent];
if (path)
{
URL = [path stringByAppendingString:URL];
}
- NSString *port = [components objectForKey:URLPortComponent];
+ NSString *port = components[URLPortComponent];
if (port)
{
URL = [NSString stringWithFormat:@":%@%@", port, URL];
}
- NSString *host = [components objectForKey:URLHostComponent];
+ NSString *host = components[URLHostComponent];
if (host)
{
URL = [host stringByAppendingString:URL];
}
- NSString *user = [components objectForKey:URLUserComponent];
+ NSString *user = components[URLUserComponent];
if (user)
{
- NSString *password = [components objectForKey:URLPasswordComponent];
+ NSString *password = components[URLPasswordComponent];
if (password)
{
user = [user stringByAppendingFormat:@":%@", password];
}
URL = [user stringByAppendingFormat:@"@%@", URL];
}
- NSString *scheme = [components objectForKey:URLSchemeComponent];
+ NSString *scheme = components[URLSchemeComponent];
if (scheme)
{
URL = [scheme stringByAppendingFormat:@"://%@", URL];
@@ -681,18 +685,16 @@ + (NSURL *)URLWithComponents:(NSDictionary *)components
- (NSDictionary *)components
{
NSMutableDictionary *result = [NSMutableDictionary dictionary];
- for (NSString *key in [NSArray arrayWithObjects:
- URLSchemeComponent, URLHostComponent,
+ for (NSString *key in @[URLSchemeComponent, URLHostComponent,
URLPortComponent, URLUserComponent,
URLPasswordComponent, URLPortComponent,
URLPathComponent, URLParameterStringComponent,
- URLQueryComponent, URLFragmentComponent,
- nil])
+ URLQueryComponent, URLFragmentComponent])
{
id value = [self valueForKey:key];
if (value)
{
- [result setObject:value forKey:key];
+ result[key] = value;
}
}
return result;
@@ -708,7 +710,7 @@ - (NSURL *)URLWithValue:(NSString *)value forComponent:(NSString *)component
if (value)
{
- [components setObject:value forKey:component];
+ components[component] = value;
}
else
{
@@ -826,13 +828,13 @@ - (NSArray *)HTTPBasicAuthComponents
}
else
{
- return [NSArray arrayWithObjects:[self.URL user] ?: @"", [self.URL password] ?: @"", nil];
+ return @[[self.URL user] ?: @"", [self.URL password] ?: @""];
}
}
- (NSString *)HTTPBasicAuthUser
{
- return [[self HTTPBasicAuthComponents] objectAtIndex:0];
+ return [self HTTPBasicAuthComponents][0];
}
- (NSString *)HTTPBasicAuthPassword
View
12 Tests/RequestUtilsTests.xcodeproj/project.pbxproj
@@ -152,7 +152,7 @@
0177128414BF91160050C6BC /* Project object */ = {
isa = PBXProject;
attributes = {
- LastUpgradeCheck = 0430;
+ LastUpgradeCheck = 0460;
};
buildConfigurationList = 0177128714BF91160050C6BC /* Build configuration list for PBXProject "RequestUtilsTests" */;
compatibilityVersion = "Xcode 3.2";
@@ -193,6 +193,10 @@
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_64_BIT)";
CLANG_ENABLE_OBJC_ARC = YES;
+ CLANG_WARN_CONSTANT_CONVERSION = YES;
+ CLANG_WARN_ENUM_CONVERSION = YES;
+ CLANG_WARN_INT_CONVERSION = YES;
+ CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
@@ -207,6 +211,7 @@
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
+ GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.7;
ONLY_ACTIVE_ARCH = YES;
@@ -220,6 +225,10 @@
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_64_BIT)";
CLANG_ENABLE_OBJC_ARC = YES;
+ CLANG_WARN_CONSTANT_CONVERSION = YES;
+ CLANG_WARN_ENUM_CONVERSION = YES;
+ CLANG_WARN_INT_CONVERSION = YES;
+ CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
GCC_C_LANGUAGE_STANDARD = gnu99;
@@ -228,6 +237,7 @@
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
+ GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.7;
SDKROOT = macosx;
View
86 Tests/RequestUtilsTests.xcodeproj/xcshareddata/xcschemes/RequestUtilsTests.xcscheme
@@ -0,0 +1,86 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Scheme
+ LastUpgradeVersion = "0460"
+ version = "1.3">
+ <BuildAction
+ parallelizeBuildables = "YES"
+ buildImplicitDependencies = "YES">
+ <BuildActionEntries>
+ <BuildActionEntry
+ buildForTesting = "YES"
+ buildForRunning = "YES"
+ buildForProfiling = "YES"
+ buildForArchiving = "YES"
+ buildForAnalyzing = "YES">
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "0177128C14BF91160050C6BC"
+ BuildableName = "RequestUtilsTests"
+ BlueprintName = "RequestUtilsTests"
+ ReferencedContainer = "container:RequestUtilsTests.xcodeproj">
+ </BuildableReference>
+ </BuildActionEntry>
+ </BuildActionEntries>
+ </BuildAction>
+ <TestAction
+ selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
+ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
+ shouldUseLaunchSchemeArgsEnv = "YES"
+ buildConfiguration = "Debug">
+ <Testables>
+ </Testables>
+ <MacroExpansion>
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "0177128C14BF91160050C6BC"
+ BuildableName = "RequestUtilsTests"
+ BlueprintName = "RequestUtilsTests"
+ ReferencedContainer = "container:RequestUtilsTests.xcodeproj">
+ </BuildableReference>
+ </MacroExpansion>
+ </TestAction>
+ <LaunchAction
+ selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
+ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
+ launchStyle = "0"
+ useCustomWorkingDirectory = "NO"
+ buildConfiguration = "Debug"
+ ignoresPersistentStateOnLaunch = "NO"
+ debugDocumentVersioning = "YES"
+ allowLocationSimulation = "YES">
+ <BuildableProductRunnable>
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "0177128C14BF91160050C6BC"
+ BuildableName = "RequestUtilsTests"
+ BlueprintName = "RequestUtilsTests"
+ ReferencedContainer = "container:RequestUtilsTests.xcodeproj">
+ </BuildableReference>
+ </BuildableProductRunnable>
+ <AdditionalOptions>
+ </AdditionalOptions>
+ </LaunchAction>
+ <ProfileAction
+ shouldUseLaunchSchemeArgsEnv = "YES"
+ savedToolIdentifier = ""
+ useCustomWorkingDirectory = "NO"
+ buildConfiguration = "Release"
+ debugDocumentVersioning = "YES">
+ <BuildableProductRunnable>
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "0177128C14BF91160050C6BC"
+ BuildableName = "RequestUtilsTests"
+ BlueprintName = "RequestUtilsTests"
+ ReferencedContainer = "container:RequestUtilsTests.xcodeproj">
+ </BuildableReference>
+ </BuildableProductRunnable>
+ </ProfileAction>
+ <AnalyzeAction
+ buildConfiguration = "Debug">
+ </AnalyzeAction>
+ <ArchiveAction
+ buildConfiguration = "Release"
+ revealArchiveInOrganizer = "YES">
+ </ArchiveAction>
+</Scheme>
View
32 Tests/RequestUtilsTests/StringTests.m
@@ -35,6 +35,21 @@ - (void)testURLFragment
#pragma mark Paths
+- (void)textURLEncoding
+{
+ NSString *input = @"foo bar";
+ NSString *expected = @"foo%20bar";
+ NSAssert([expected isEqualToString:[input URLEncodedString]], @"URLEncoding test failed");
+}
+
+- (void)textNilURLEncoding
+{
+ NSString *nilString = nil;
+ NSAssert([nilString URLValue] == nil, @"URLEncoding nil test failed");
+}
+
+#pragma mark Paths
+
- (void)testAppendPath
{
NSString *URLString = @"http://hello";
@@ -70,19 +85,16 @@ - (void)testAppendPathExtension
- (void)testSimpleQueryString
{
NSString *query = @"?foo=bar&bar=foo";
- NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:@"bar", @"foo", @"foo", @"bar", nil];
+ NSDictionary *result = @{@"foo": @"bar", @"bar": @"foo"};
NSAssert([[query URLQueryParametersWithOptions:0] isEqual:result], @"URLQueryParameters test failed");
}
- (void)testArrayQueryString
{
NSString *query = @"?foo=bar&bar=foo&bar=bar";
- NSDictionary *result1 = [NSDictionary dictionaryWithObjectsAndKeys:
- @"bar", @"foo", [NSArray arrayWithObjects:@"foo", @"bar", nil], @"bar", nil];
- NSDictionary *result2 = [NSDictionary dictionaryWithObjectsAndKeys:@"bar", @"foo", @"bar", @"bar", nil];
- NSDictionary *result3 = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSArray arrayWithObject:@"bar"], @"foo",
- [NSArray arrayWithObjects:@"foo", @"bar", nil], @"bar", nil];
+ NSDictionary *result1 = @{@"foo": @"bar", @"bar": @[@"foo", @"bar"]};
+ NSDictionary *result2 = @{@"foo": @"bar", @"bar": @"bar"};
+ NSDictionary *result3 = @{@"foo": @[@"bar"], @"bar": @[@"foo", @"bar"]};
NSAssert([[query URLQueryParametersWithOptions:URLQueryOptionUseArrays] isEqual:result1], @"URLQueryParameters test failed");
NSAssert([[query URLQueryParametersWithOptions:URLQueryOptionKeepLastValue] isEqual:result2], @"URLQueryParameters test failed");
NSAssert([[query URLQueryParametersWithOptions:URLQueryOptionAlwaysUseArrays] isEqual:result3], @"URLQueryParameters test failed");
@@ -91,10 +103,8 @@ - (void)testArrayQueryString
- (void)testArrayQueryString2
{
NSString *query = @"?foo[]=bar&bar[]=foo&bar[]=bar";
- NSDictionary *result1 = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSArray arrayWithObject:@"bar"], @"foo",
- [NSArray arrayWithObjects:@"foo", @"bar", nil], @"bar", nil];
- NSDictionary *result2 = [NSDictionary dictionaryWithObjectsAndKeys:@"bar", @"foo", @"bar", @"bar", nil];
+ NSDictionary *result1 = @{@"foo": @[@"bar"], @"bar": @[@"foo", @"bar"]};
+ NSDictionary *result2 = @{@"foo": @"bar", @"bar": @"bar"};
NSAssert([[query URLQueryParametersWithOptions:URLQueryOptionUseArrays] isEqual:result1], @"URLQueryParameters test failed");
NSAssert([[query URLQueryParametersWithOptions:URLQueryOptionKeepLastValue] isEqual:result2], @"URLQueryParameters test failed");
}
Please sign in to comment.
Something went wrong with that request. Please try again.