Permalink
Browse files

factoring baseURL out of StackMobRequest

- so that StackMobPushRequest doesn't have to duplicate getURL functionality (StackMobHerokuRequest still does, though. that's a refactor for another day)
- moving *Tests.{m,h} files that weren't in the tests/ dir into the right place
- creating a StackMobTestCommon so that we can build convenience test methods that use SenTest macros (which generally rely on being executed inside a SenTestCase subclass)
  • Loading branch information...
1 parent 866d408 commit 3f5354a57fbdfff44563491e2250f5d1cd91754b @arschles arschles committed Nov 15, 2011
@@ -33,20 +33,8 @@ + (id)requestForMethod:(NSString*)method withArguments:(NSDictionary*)arguments
return r;
}
-- (NSURL*)getURL
-{
- // nil method is an invalid request
- if(!self.method) return nil;
-
- NSString* baseURL = [session pushURL];
- NSString * urlString = [baseURL stringByAppendingFormat:@"/%@", self.method];
-
- //add query string if necessary
- if(([[self httpMethod] isEqualToString:@"GET"] || [[self httpMethod] isEqualToString:@"DELETE"]) && [mArguments count] > 0) {
- urlString = [urlString stringByAppendingFormat:@"?%@", [mArguments queryString]];
- }
- SMLog(@"%@", urlString);
- return [NSURL URLWithString:urlString];
+- (NSString *)getBaseURL {
+ return [[session pushURL] stringByAppendingFormat:@"/%@", self.method];
}
@end
@@ -33,7 +33,7 @@ typedef enum {
NSURLConnection* mConnection;
id<SMRequestDelegate> mDelegate;
SEL mSelector;
- BOOL mIsSecure;
+ BOOL mIsSecure;
NSString* mMethod;
NSMutableDictionary* mArguments;
NSMutableDictionary* mHeaders;
@@ -46,8 +46,8 @@ typedef enum {
NSHTTPURLResponse* mHttpResponse;
@protected
- BOOL userBased;
- StackMobSession *session;
+ BOOL userBased;
+ StackMobSession *session;
}
@property(readwrite, retain) id delegate;
@@ -61,6 +61,7 @@ typedef enum {
@property(readonly) BOOL finished;
@property(readonly) NSHTTPURLResponse* httpResponse;
@property(readonly, getter=getStatusCode) NSInteger statusCode;
+@property(readonly, getter=getBaseURL) NSString* baseURL;
@property(readonly, getter=getURL) NSURL* url;
@property(nonatomic) BOOL userBased;
@@ -78,7 +79,7 @@ typedef enum {
/*
* User based requests
- * Use these to
+ * Use these to execute a method on a user object
*/
+ (id)userRequest;
+ (id)userRequestForMethod:(NSString *)method withHttpVerb:(SMHttpVerb)httpVerb;
View
@@ -151,21 +151,22 @@ + (NSString*)stringFromHttpVerb:(SMHttpVerb)httpVerb
}
}
+- (NSString *)getBaseURL {
+ if(mIsSecure) {
+ return [session secureURLForMethod:self.method isUserBased:userBased];
+ }
+ return [session urlForMethod:self.method isUserBased:userBased];
+}
+
- (NSURL*)getURL
{
// nil method is an invalid request
if(!self.method) return nil;
// build URL and add query string if necessary
NSMutableArray *urlComponents = [NSMutableArray arrayWithCapacity:2];
- NSMutableString* sessionUrlString;
- if (mIsSecure) {
- sessionUrlString = [session secureURLForMethod:self.method isUserBased:userBased];
- } else {
- sessionUrlString = [session urlForMethod:self.method isUserBased:userBased];
- }
- [urlComponents addObject:sessionUrlString];
-
+ [urlComponents addObject:self.baseURL];
+
if (([[self httpMethod] isEqualToString:@"GET"] || [[self httpMethod] isEqualToString:@"DELETE"]) &&
[mArguments count] > 0) {
[urlComponents addObject:[mArguments queryString]];
View
@@ -1,16 +0,0 @@
-//
-// StackMobTestUtils.h
-// StackMobiOS
-//
-// Created by Aaron Schlesinger on 11/14/11.
-// Copyright (c) 2011 StackMob, Inc. All rights reserved.
-//
-
-#import <Foundation/Foundation.h>
-#import "StackMob.h"
-
-@interface StackMobTestUtils : NSObject
-+ (void)setStackMobApplication;
-+ (void)runRunLoop:(NSRunLoop *)runLoop untilRequestFinished:(StackMobRequest *)request;
-+ (NSDictionary *)runDefaultRunLoopAndGetDictionaryResultFromRequest:(StackMobRequest *)request;
-@end
@@ -97,8 +97,11 @@
82CE443E1288F17700A73E9C /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82CE433A1288F02E00A73E9C /* SystemConfiguration.framework */; };
82CE45151289025B00A73E9C /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82CE432C1288EFB900A73E9C /* UIKit.framework */; };
82CE45F412892FFA00A73E9C /* APIRequestTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 82CE45F212892FFA00A73E9C /* APIRequestTests.m */; };
- 930326C71471E8E9004FBA11 /* StackMobPushTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 930326C61471E8E9004FBA11 /* StackMobPushTests.m */; };
- 930326CB1471ED36004FBA11 /* StackMobTestUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 930326CA1471ED36004FBA11 /* StackMobTestUtils.m */; };
+ 93694862147228B400575176 /* StackMobPushTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9369485F147228B400575176 /* StackMobPushTests.m */; };
+ 93694863147228B400575176 /* StackMobTestUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 93694861147228B400575176 /* StackMobTestUtils.m */; };
+ 9369486514722C5C00575176 /* StackMobTestCommon.m in Sources */ = {isa = PBXBuildFile; fileRef = 9369485A1472286900575176 /* StackMobTestCommon.m */; };
+ 9369486614722C6500575176 /* StackMobTestCommon.h in Headers */ = {isa = PBXBuildFile; fileRef = 936948591472286900575176 /* StackMobTestCommon.h */; settings = {ATTRIBUTES = (); }; };
+ 9369486714722C8000575176 /* StackMobTestUtils.h in Headers */ = {isa = PBXBuildFile; fileRef = 93694860147228B400575176 /* StackMobTestUtils.h */; };
AA747D9F0F9514B9006C5449 /* StackMobiOS_Prefix.pch in Headers */ = {isa = PBXBuildFile; fileRef = AA747D9E0F9514B9006C5449 /* StackMobiOS_Prefix.pch */; };
AACBBE4A0F95108600F1A2B1 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AACBBE490F95108600F1A2B1 /* Foundation.framework */; };
E33D87DB144616CF00FBEB96 /* StackMobVersion.h in Headers */ = {isa = PBXBuildFile; fileRef = E33D87DA144616CF00FBEB96 /* StackMobVersion.h */; };
@@ -168,10 +171,12 @@
82CE43501288F0DA00A73E9C /* Tests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Tests-Info.plist"; sourceTree = "<group>"; };
82CE45F212892FFA00A73E9C /* APIRequestTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = APIRequestTests.m; path = tests/APIRequestTests.m; sourceTree = "<group>"; };
82CE45F312892FFA00A73E9C /* APIRequestTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = APIRequestTests.h; path = tests/APIRequestTests.h; sourceTree = "<group>"; };
- 930326C51471E8E9004FBA11 /* StackMobPushTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StackMobPushTests.h; sourceTree = "<group>"; };
- 930326C61471E8E9004FBA11 /* StackMobPushTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StackMobPushTests.m; sourceTree = "<group>"; };
- 930326C91471ED36004FBA11 /* StackMobTestUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StackMobTestUtils.h; sourceTree = "<group>"; };
- 930326CA1471ED36004FBA11 /* StackMobTestUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StackMobTestUtils.m; sourceTree = "<group>"; };
+ 936948591472286900575176 /* StackMobTestCommon.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = StackMobTestCommon.h; path = tests/StackMobTestCommon.h; sourceTree = "<group>"; };
+ 9369485A1472286900575176 /* StackMobTestCommon.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = StackMobTestCommon.m; path = tests/StackMobTestCommon.m; sourceTree = "<group>"; };
+ 9369485E147228B400575176 /* StackMobPushTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = StackMobPushTests.h; path = tests/StackMobPushTests.h; sourceTree = "<group>"; };
+ 9369485F147228B400575176 /* StackMobPushTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = StackMobPushTests.m; path = tests/StackMobPushTests.m; sourceTree = "<group>"; };
+ 93694860147228B400575176 /* StackMobTestUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = StackMobTestUtils.h; path = tests/StackMobTestUtils.h; sourceTree = "<group>"; };
+ 93694861147228B400575176 /* StackMobTestUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = StackMobTestUtils.m; path = tests/StackMobTestUtils.m; sourceTree = "<group>"; };
AA747D9E0F9514B9006C5449 /* StackMobiOS_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StackMobiOS_Prefix.pch; sourceTree = SOURCE_ROOT; };
AACBBE490F95108600F1A2B1 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
D2AAC07E0554694100DB518D /* libStackMobiOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libStackMobiOS.a; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -369,12 +374,14 @@
82CE42D41288E4D200A73E9C /* Tests */ = {
isa = PBXGroup;
children = (
+ 9369485E147228B400575176 /* StackMobPushTests.h */,
+ 9369485F147228B400575176 /* StackMobPushTests.m */,
+ 93694860147228B400575176 /* StackMobTestUtils.h */,
+ 93694861147228B400575176 /* StackMobTestUtils.m */,
82CE45F312892FFA00A73E9C /* APIRequestTests.h */,
82CE45F212892FFA00A73E9C /* APIRequestTests.m */,
- 930326C51471E8E9004FBA11 /* StackMobPushTests.h */,
- 930326C61471E8E9004FBA11 /* StackMobPushTests.m */,
- 930326C91471ED36004FBA11 /* StackMobTestUtils.h */,
- 930326CA1471ED36004FBA11 /* StackMobTestUtils.m */,
+ 936948591472286900575176 /* StackMobTestCommon.h */,
+ 9369485A1472286900575176 /* StackMobTestCommon.m */,
);
name = Tests;
sourceTree = "<group>";
@@ -416,6 +423,8 @@
038CB28F14284C3F001DEAC9 /* JSONKit.h in Headers */,
E33D87DB144616CF00FBEB96 /* StackMobVersion.h in Headers */,
E3D728D71448BE5700804149 /* StackMobQuery.h in Headers */,
+ 9369486614722C6500575176 /* StackMobTestCommon.h in Headers */,
+ 9369486714722C8000575176 /* StackMobTestUtils.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -552,8 +561,9 @@
038CB29414284C97001DEAC9 /* StackMobRequest.m in Sources */,
82CE45F412892FFA00A73E9C /* APIRequestTests.m in Sources */,
E3D728D91448BE5700804149 /* StackMobQuery.m in Sources */,
- 930326C71471E8E9004FBA11 /* StackMobPushTests.m in Sources */,
- 930326CB1471ED36004FBA11 /* StackMobTestUtils.m in Sources */,
+ 93694862147228B400575176 /* StackMobPushTests.m in Sources */,
+ 93694863147228B400575176 /* StackMobTestUtils.m in Sources */,
+ 9369486514722C5C00575176 /* StackMobTestCommon.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -18,11 +18,12 @@
#import <UIKit/UIKit.h>
#import "StackMob.h"
#import "StackMobTestUtils.h"
+#import "StackMobTestCommon.h"
//#import "application_headers" as required
-@interface APIRequestTests : SenTestCase {
+@interface APIRequestTests : StackMobTestCommon {
}
View
@@ -21,10 +21,10 @@ @implementation APIRequestTests
- (void) setUp
{
+ [super setUp];
NSLog(@"In setup");
- if (!mySession)
+ if (!mySession)
{
- [StackMobTestUtils setStackMobApplication];
NSLog(@"Created new session");
}
}
@@ -33,6 +33,7 @@ - (void) tearDown
{
NSLog(@"In teardown");
mySession = nil;
+ [super tearDown];
}
- (void) testGet {
@@ -117,30 +118,37 @@ - (void) testAPIList {
- (void) testRequestsThatDefaultToSecure {
StackMobRequest *r;
- StackMobCallback emptyCallback = ^(BOOL success, id result) {};
r = [[StackMob stackmob] loginWithArguments:[NSDictionary dictionary] andCallback:emptyCallback];
+ [self assertNotNSError:r];
STAssertTrue(r.isSecure, @"Login Request Should Default to SSL");
r = [[StackMob stackmob] loginWithFacebookToken:@"WHOCARES" andCallback:emptyCallback];
+ [self assertNotNSError:r];
STAssertTrue(r.isSecure, @"Login w/ Facebook Request Should Default to SSL");
r = [[StackMob stackmob] loginWithTwitterToken:@"WHOCARES" secret:@"WHOCARES2" andCallback:emptyCallback];
+ [self assertNotNSError:r];
STAssertTrue(r.isSecure, @"Login w/ Twitter Request Should Default to SSL");
r = [[StackMob stackmob] linkUserWithFacebookToken:@"ASD" withCallback:emptyCallback];
+ [self assertNotNSError:r];
STAssertTrue(r.isSecure, @"Link With Facebook Token Should Default to SSL");
r = [[StackMob stackmob] linkUserWithTwitterToken:@"WHOCARES" secret:@"WHOCARES" andCallback:emptyCallback];
+ [self assertNotNSError:r];
STAssertTrue(r.isSecure, @"Link With Twitter Token Should Default to SSL");
r = [[StackMob stackmob] registerWithArguments:[NSDictionary dictionary] andCallback:emptyCallback];
+ [self assertNotNSError:r];
STAssertTrue(r.isSecure, @"Register User Should Default to SSL");
r = [[StackMob stackmob] registerWithFacebookToken:@"TOKEN" username:@"UNAME" andCallback:emptyCallback];
+ [self assertNotNSError:r];
STAssertTrue(r.isSecure, @"Register With Facebook Token Should Default to SSL");
r = [[StackMob stackmob] registerWithTwitterToken:@"TOKEN" secret:@"SECRET" username:@"UNAME" andCallback:emptyCallback];
+ [self assertNotNSError:r];
STAssertTrue(r.isSecure, @"Register With Twitter Token Should Defult to SSL");
}
@@ -18,17 +18,18 @@
#import <UIKit/UIKit.h>
#import "StackMob.h"
#import "StackMobTestUtils.h"
+#import "StackMobTestCommon.h"
//#import "application_headers" as required
-extern StackMobCallback EMPTY_CB;
-extern NSString * PUSH_USER;
-extern NSString * PUSH_TOKEN;
-@interface StackMobPushTests : SenTestCase {
- NSDictionary * PUSH_PAYLOAD;
- NSArray * PUSH_USER_TARGETS;
- NSArray * PUSH_TOKEN_TARGETS;
+@interface StackMobPushTests : StackMobTestCommon {
+ NSDictionary * pushPayload;
+ NSArray * pushUserTargets;
+ NSArray * pushTokenTargets;
+ NSString * pushUser;
+ NSString * pushToken;
+
}
- (void)assertResultIsQueued:(NSDictionary *)result;
@@ -17,80 +17,78 @@
#import <UIKit/UIKit.h>
//#import "application_headers" as required
-NSString * PUSH_USER = @"IOS_TEST_PUSH_USER";
-NSString * PUSH_TOKEN = @"DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF";
-NSDictionary * PUSH_PAYLOAD = nil;
-StackMobCallback EMPTY_CB = ^(BOOL success, id result) {};
-
@implementation StackMobPushTests
- (void)assertResultIsQueued:(NSDictionary *)result {
STAssertNotNil([result objectForKey:@"queued"], @"no queued value returned");
}
- (void)registerToken {
- StackMobRequest * request = [[StackMob stackmob] registerForPushWithUser:PUSH_USER token:PUSH_TOKEN andCallback:EMPTY_CB];
+ StackMobRequest * request = [[StackMob stackmob] registerForPushWithUser:pushUser token:pushToken andCallback:emptyCallback];
[StackMobTestUtils runDefaultRunLoopAndGetDictionaryResultFromRequest:request];
}
- (void)setUp {
- [StackMobTestUtils setStackMobApplication];
- if(!PUSH_PAYLOAD) {
- PUSH_PAYLOAD = [NSDictionary dictionaryWithObjectsAndKeys:@"val1", @"key1", nil];
- }
- if(!PUSH_USER_TARGETS) {
- PUSH_USER_TARGETS = [NSArray arrayWithObject:PUSH_USER];
- }
- if(!PUSH_TOKEN_TARGETS) {
- PUSH_TOKEN_TARGETS = [NSArray arrayWithObject:PUSH_TOKEN];
- }
-
- StackMobRequest * request = [[StackMob stackmob] deletePushToken:PUSH_TOKEN andCallback:EMPTY_CB];
+ [super setUp];
+
+ if(!pushUser) pushUser = @"IOS_TEST_PUSH_USER";
+ if(!pushToken) pushToken = @"DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF";
+ if(!pushPayload) pushPayload = [NSDictionary dictionaryWithObjectsAndKeys:@"val1", @"key1", nil];
+ if(!pushUserTargets) pushUserTargets = [NSArray arrayWithObject:pushUser];
+ if(!pushTokenTargets) pushTokenTargets = [NSArray arrayWithObject:pushToken];
+
+ StackMobRequest * request = [[StackMob stackmob] deletePushToken:pushToken andCallback:emptyCallback];
[StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:request];
}
- (void)tearDown {
- StackMobRequest * request = [[StackMob stackmob] deletePushToken:PUSH_TOKEN andCallback:EMPTY_CB];
+ StackMobRequest * request = [[StackMob stackmob] deletePushToken:pushToken andCallback:emptyCallback];
[StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:request];
+ [super tearDown];
}
- (void)testRegisterDeviceToken {
- StackMobRequest * request = [[StackMob stackmob] registerForPushWithUser:PUSH_USER token:PUSH_TOKEN andCallback:EMPTY_CB];
+ StackMobRequest * request = [[StackMob stackmob] registerForPushWithUser:pushUser token:pushToken andCallback:emptyCallback];
NSDictionary * result = [StackMobTestUtils runDefaultRunLoopAndGetDictionaryResultFromRequest:request];
NSLog(@"result: %@", result);
+ [self assertNotNSError:result];
STAssertTrue((BOOL)[result objectForKey:@"registered"], @"token not reported as registered");
}
- (void)testGetDeviceTokens {
- StackMobRequest * request = [[StackMob stackmob] getPushTokensForUsers:[NSArray arrayWithObject:PUSH_USER] andCallback:EMPTY_CB];
+ StackMobRequest * request = [[StackMob stackmob] getPushTokensForUsers:[NSArray arrayWithObject:pushUser] andCallback:emptyCallback];
NSDictionary * result = [StackMobTestUtils runDefaultRunLoopAndGetDictionaryResultFromRequest:request];
NSLog(@"result: %@", result);
+ [self assertNotNSError:result];
STAssertNotNil([result objectForKey:@"tokens"], @"no tokens value returned");
}
- (void)testSendPushBroadcastWithArguments {
- StackMobRequest * request = [[StackMob stackmob] sendPushBroadcastWithArguments:PUSH_PAYLOAD andCallback:EMPTY_CB];
+ StackMobRequest * request = [[StackMob stackmob] sendPushBroadcastWithArguments:pushPayload andCallback:emptyCallback];
NSDictionary * result = [StackMobTestUtils runDefaultRunLoopAndGetDictionaryResultFromRequest:request];
NSLog(@"result: %@", result);
+ [self assertNotNSError:result];
[self assertResultIsQueued:result];
}
- (void)testSendPushToUsersWithArguments {
[self registerToken];
- StackMobRequest * request = [[StackMob stackmob] sendPushToUsersWithArguments:PUSH_PAYLOAD withUserIds:PUSH_USER_TARGETS andCallback:EMPTY_CB];
+ StackMobRequest * request = [[StackMob stackmob] sendPushToUsersWithArguments:pushPayload withUserIds:pushUserTargets andCallback:emptyCallback];
NSDictionary * result = [StackMobTestUtils runDefaultRunLoopAndGetDictionaryResultFromRequest:request];
NSLog(@"result: %@", result);
+ [self assertNotNSError:result];
[self assertResultIsQueued:result];
}
- (void)testSendPushToTokensWithArguments {
[self registerToken];
- StackMobRequest * request = [[StackMob stackmob] sendPushToTokensWithArguments:PUSH_PAYLOAD withTokens:PUSH_TOKEN_TARGETS andCallback:EMPTY_CB];
+ StackMobRequest * request = [[StackMob stackmob] sendPushToTokensWithArguments:pushPayload withTokens:pushTokenTargets andCallback:emptyCallback];
NSDictionary * result = [StackMobTestUtils runDefaultRunLoopAndGetDictionaryResultFromRequest:request];
NSLog(@"result: %@", result);
+ [self assertNotNSError:result];
[self assertResultIsQueued:result];
}
Oops, something went wrong.

0 comments on commit 3f5354a

Please sign in to comment.