From 0f608ec1acbb745621e10cb56233581d7ef2d74f Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Wed, 22 May 2013 14:40:52 +0100 Subject: [PATCH] [AFHTTPClient init] should raise an exception As discussed on #971, this will prevent anyone from accidentally calling init which results in undefined behaviour. http://developer.apple.com/library/ios/#documentation/general/conceptual/devpedia-cocoacore/MultipleInitializers.html --- AFNetworking/AFHTTPClient.m | 6 ++++++ Tests/AFHTTPClientTests.m | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/AFNetworking/AFHTTPClient.m b/AFNetworking/AFHTTPClient.m index a193a9d..aee0738 100644 --- a/AFNetworking/AFHTTPClient.m +++ b/AFNetworking/AFHTTPClient.m @@ -221,6 +221,12 @@ NSArray * AFQueryStringPairsFromKeyAndValue(NSString *key, id value) { return [[self alloc] initWithBaseURL:url]; } +- (instancetype)init { + @throw [NSException exceptionWithName:NSInternalInconsistencyException + reason:[NSString stringWithFormat:@"%@ Failed to call designated initializer. Invoke `initWithBaseURL:` instead.", NSStringFromClass([self class])] + userInfo:nil]; +} + - (id)initWithBaseURL:(NSURL *)url { NSParameterAssert(url); diff --git a/Tests/AFHTTPClientTests.m b/Tests/AFHTTPClientTests.m index 0ba0032..4fcc287 100644 --- a/Tests/AFHTTPClientTests.m +++ b/Tests/AFHTTPClientTests.m @@ -35,6 +35,10 @@ #pragma mark - +- (void)testInitRaisesException { + expect(^{ (void)[[AFHTTPClient alloc] init]; }).to.raiseAny(); +} + - (void)testDefaultHeaders { [self.client setDefaultHeader:@"x-some-key" value:@"SomeValue"]; expect([self.client defaultValueForHeader:@"x-some-key"]).to.equal(@"SomeValue");