Skip to content

Commit

Permalink
Added tests for Basic and Digest authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
pokeb committed Nov 10, 2008
1 parent 4ae9142 commit cfb68f8
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 10 deletions.
2 changes: 2 additions & 0 deletions ASIFormDataRequestTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ - (void)testPostWithFileUpload
BOOL success = ([[request dataString] isEqualToString:[NSString stringWithFormat:@"post_var: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",@"bigfile",size]]);
STAssertTrue(success,@"Failed to upload the correct data");
}



@end
2 changes: 2 additions & 0 deletions ASIHTTPRequestTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@
- (void)testDownloadProgress;
- (void)testUploadProgress;
- (void)testCookies;
- (void)testBasicAuthentication;
- (void)testDigestAuthentication;

@end
127 changes: 119 additions & 8 deletions ASIHTTPRequestTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@

@implementation ASIHTTPRequestTests

/*
More tests needed for:
- Delegates - success and failure
- Authentication
- Keychains
- Session persistence
*/



- (void)testBasicDownload
Expand Down Expand Up @@ -245,5 +237,124 @@ - (void)testCookies
}


- (void)testBasicAuthentication
{

NSURL *url = [[[NSURL alloc] initWithString:@"http://asi/asi-http-request/tests/basic-authentication"] autorelease];
ASIHTTPRequest *request;
BOOL success;
NSError *err;

request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseKeychainPersistance:NO];
[request start];
success = ([[[[request error] userInfo] objectForKey:@"Description"] isEqualToString:@"Your username and password were incorrect."]);
STAssertTrue(success,@"Failed to generate permission denied error with no credentials");

request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseKeychainPersistance:NO];
[request setUsername:@"wrong"];
[request setPassword:@"wrong"];
[request start];
success = ([[[[request error] userInfo] objectForKey:@"Description"] isEqualToString:@"Your username and password were incorrect."]);
STAssertTrue(success,@"Failed to generate permission denied error with wrong credentials");

request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseSessionPersistance:YES];
[request setUseKeychainPersistance:YES];
[request setUsername:@"secret_username"];
[request setPassword:@"secret_password"];
[request start];
err = [request error];
STAssertNil(err,@"Failed to supply correct username and password");

request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseSessionPersistance:NO];
[request setUseKeychainPersistance:NO];
[request start];
success = ([[[[request error] userInfo] objectForKey:@"Description"] isEqualToString:@"Your username and password were incorrect."]);
STAssertTrue(success,@"Reused credentials when we shouldn't have");

request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseSessionPersistance:YES];
[request setUseKeychainPersistance:NO];
[request start];
err = [request error];
STAssertNil(err,@"Failed to reuse credentials");

[ASIHTTPRequest clearSession];

request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseKeychainPersistance:NO];
[request start];
success = ([[[[request error] userInfo] objectForKey:@"Description"] isEqualToString:@"Your username and password were incorrect."]);
STAssertTrue(success,@"Failed to clear credentials");

//This test may show a dialog!
request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseKeychainPersistance:YES];
[request start];
err = [request error];
STAssertNil(err,@"Failed to use stored credentials");
}



- (void)testDigestAuthentication
{
[ASIHTTPRequest clearSession];

NSURL *url = [[[NSURL alloc] initWithString:@"http://asi/asi-http-request/tests/digest-authentication"] autorelease];
ASIHTTPRequest *request;
BOOL success;
NSError *err;

request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseKeychainPersistance:NO];
[request start];
success = ([[[[request error] userInfo] objectForKey:@"Description"] isEqualToString:@"Your username and password were incorrect."]);
STAssertTrue(success,@"Failed to generate permission denied error with no credentials");

request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseKeychainPersistance:NO];
[request setUsername:@"wrong"];
[request setPassword:@"wrong"];
[request start];
success = ([[[[request error] userInfo] objectForKey:@"Description"] isEqualToString:@"Your username and password were incorrect."]);
STAssertTrue(success,@"Failed to generate permission denied error with wrong credentials");

request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseSessionPersistance:YES];
[request setUseKeychainPersistance:YES];
[request setUsername:@"secret_username"];
[request setPassword:@"secret_password"];
[request start];
err = [request error];
STAssertNil(err,@"Failed to supply correct username and password");

request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseSessionPersistance:NO];
[request setUseKeychainPersistance:NO];
[request start];
success = ([[[[request error] userInfo] objectForKey:@"Description"] isEqualToString:@"Your username and password were incorrect."]);
STAssertTrue(success,@"Reused credentials when we shouldn't have");

request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseSessionPersistance:YES];
[request setUseKeychainPersistance:NO];
[request start];
err = [request error];
STAssertNil(err,@"Failed to reuse credentials");

[ASIHTTPRequest clearSession];

request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseKeychainPersistance:NO];
[request start];
success = ([[[[request error] userInfo] objectForKey:@"Description"] isEqualToString:@"Your username and password were incorrect."]);
STAssertTrue(success,@"Failed to clear credentials");

}


@end
6 changes: 4 additions & 2 deletions asi-http-request.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/asi-http-request.app/Contents/MacOS/asi-http-request";
COPY_PHASE_STRIP = NO;
FRAMEWORK_SEARCH_PATHS = "$(DEVELOPER_LIBRARY_DIR)/Frameworks";
GCC_DYNAMIC_NO_PIC = NO;
Expand All @@ -463,7 +464,7 @@
);
PREBINDING = NO;
PRODUCT_NAME = Tests;
TEST_HOST = "";
TEST_HOST = "$(BUNDLE_LOADER)";
WRAPPER_EXTENSION = octest;
};
name = Debug;
Expand All @@ -472,6 +473,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/asi-http-request.app/Contents/MacOS/asi-http-request";
COPY_PHASE_STRIP = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
FRAMEWORK_SEARCH_PATHS = "$(DEVELOPER_LIBRARY_DIR)/Frameworks";
Expand All @@ -490,7 +492,7 @@
);
PREBINDING = NO;
PRODUCT_NAME = Tests;
TEST_HOST = "";
TEST_HOST = "$(BUNDLE_LOADER)";
WRAPPER_EXTENSION = octest;
ZERO_LINK = NO;
};
Expand Down

0 comments on commit cfb68f8

Please sign in to comment.