00001
00002
00003
00004
00005
00006
00007
00008
00009 #import "RMSessionTests.h"
00010 #import "MockProtocol.h"
00011 #import "WIRemoting.h"
00012
00013 @implementation RMSessionTests
00014
00015 #pragma mark initialization
00016
00017 - (void) setUp
00018 {
00019 wrapper = [[RMResultDelegateWrapper alloc] initWithObject:self
00020 finished:@selector(finished:)
00021 failed:@selector(failed:error:)];
00022 protocol = [[MockProtocol alloc] init];
00023 call = [[RMCall alloc] initWithProtocol:protocol];
00024 [session addDelegate:wrapper];
00025 session = [[RMSession alloc] initWithAuthenticator:self
00026 call:call];
00027 username = @"admin";
00028 password = @"1234";
00029 }
00030
00031 - (void) tearDown
00032 {
00033 [wrapper release];
00034 [session release];
00035 [call release];
00036 [protocol release];
00037 }
00038
00039 #pragma mark tests
00040
00041 - (void) testSessionAuthentication {
00042 [session authenticate];
00043 if (session.authenticated) {
00044
00045 }
00046 else {
00047 STFail(@"Unable to authenticate.");
00048 }
00049 }
00050
00051 - (void)testFailedAuthentication {
00052 username = @"abc";
00053
00054 [session removeDelegate:wrapper];
00055
00056 RMResultDelegateWrapper *m = [[RMResultDelegateWrapper alloc] initWithObject:self
00057 finished:@selector(finished:)
00058 failed:@selector(expectFailed:error:)];
00059 [session addDelegate:m];
00060 [session authenticate];
00061 if (session.authenticated) {
00062
00063
00064 STFail(@"Incorrectly authenticated.");
00065 }
00066 else {
00067
00068 }
00069
00070 username = @"admin";
00071
00072 [m release];
00073 }
00074
00075 - (void)testSessionCall
00076 {
00077 RMResultDelegateWrapper *m = [[RMResultDelegateWrapper alloc] initWithObject:self
00078 finished:@selector(finished:)
00079 failed:@selector(failed:error:)];
00080
00081 [session removeDelegate:wrapper];
00082 [session addDelegate:wrapper];
00083
00084 [session call:@"json_echo2"
00085 arguments:[NSDictionary
00086 dictionaryWithObjectsAndKeys:@"hello world", @"echo", nil]];
00087
00088
00089 [m release];
00090 }
00091
00092 - (void)testNormalCallWithNoAuthenticationInfo
00093 {
00094 RMResultDelegateWrapper *m = [[RMResultDelegateWrapper alloc] initWithObject:self
00095 finished:@selector(finished:)
00096 failed:@selector(expectFailed:error:)];
00097
00098 [session.call addDelegate:m];
00099 [session.call call:@"json_echo2"
00100 arguments:[NSDictionary
00101 dictionaryWithObjectsAndKeys:@"hello world", @"echo", nil]];
00102
00103 [m release];
00104 }
00105
00106 #pragma mark support functions
00107
00108 - (id<RMCallProtocol>) callProtocol
00109 {
00110 return self;
00111 }
00112
00113 - (void)authenticateWithCall:(RMCall *)aCall
00114 {
00115 if ([aCall call:@"json_auth"
00116 arguments:[NSDictionary
00117 dictionaryWithObjectsAndKeys:
00118 username, @"username",
00119 password, @"password",
00120 nil]]) {
00121
00122 }
00123 else {
00124 STFail(@"The authenticate call is not executed.");
00125 }
00126
00127 }
00128
00129 - (void)adjustRequest:(ASIFormDataRequest *)request
00130 method:(NSString *)method
00131 arguments:(NSDictionary *)arguments
00132 {
00133 [request setPostValue:username forKey:@"username"];
00134 [request setPostValue:password forKey:@"password"];
00135 }
00136
00137 - (BOOL)authenticateResponseString:(NSString *)responseString
00138 {
00139
00140 id r = [responseString JSONValue];
00141 if ([r isKindOfClass:[NSDictionary class]]) {
00142 NSNumber *auth = [r valueForKey:@"authenticated"];
00143
00144 if (nil == auth) {
00145 return YES;
00146 }
00147
00148 if ([auth boolValue]) {
00149
00150 return YES;
00151 }
00152
00153 return NO;
00154 }
00155 else {
00156
00157 return NO;
00158 }
00159 }
00160
00161 - (void)finished:(RMResponse *)response
00162 {
00163 }
00164
00165 - (void)expectFailed:(RMResponse*) response error:(NSError*) error
00166 {
00167 if ([[error domain] isEqual:NSURLErrorDomain] &&
00168 [error code] == EINVAL) {
00169
00170 }
00171 else {
00172 STFail(@"incorrect failure.");
00173 }
00174 }
00175
00176 - (void)failed:(RMResponse *)response error:(NSError *)error
00177 {
00178 STFail(@"Failed request.");
00179 }
00180
00181
00182 @end