root / asi-http-request-with-pithos / Classes / Tests / ASIDataCompressorTests.m @ be116d22
History | View | Annotate | Download (7.1 kB)
1 |
// |
---|---|
2 |
// ASIDataCompressorTests.m |
3 |
// Mac |
4 |
// |
5 |
// Created by Ben Copsey on 17/08/2010. |
6 |
// Copyright 2010 All-Seeing Interactive. All rights reserved. |
7 |
// |
8 |
// Sadly these tests only work on Mac because of the dependency on NSTask, but I'm fairly sure this class should behave in the same way on iOS |
9 |
|
10 |
#import "ASIDataCompressorTests.h" |
11 |
#import "ASIDataCompressor.h" |
12 |
#import "ASIDataDecompressor.h" |
13 |
#import "ASIHTTPRequest.h" |
14 |
|
15 |
@implementation ASIDataCompressorTests |
16 |
|
17 |
- (void)setUp |
18 |
{ |
19 |
NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; |
20 |
|
21 |
// Download a 1.7MB text file |
22 |
NSString *filePath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"story.txt"]; |
23 |
if (![fileManager fileExistsAtPath:filePath] || [[[fileManager attributesOfItemAtPath:filePath error:NULL] objectForKey:NSFileSize] unsignedLongLongValue] < 1693961) { |
24 |
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_hound_of_the_baskervilles.text"]]; |
25 |
[request setDownloadDestinationPath:[[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"story.txt"]]; |
26 |
[request startSynchronous]; |
27 |
} |
28 |
} |
29 |
|
30 |
- (void)testInflateData |
31 |
{ |
32 |
|
33 |
NSString *originalString = [NSString stringWithContentsOfFile:[[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"story.txt"] encoding:NSUTF8StringEncoding error:NULL]; |
34 |
|
35 |
// Test in-memory inflate using uncompressData:error: |
36 |
NSError *error = nil; |
37 |
NSString *filePath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"uncompressed_file.txt"]; |
38 |
NSString *gzippedFilePath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"uncompressed_file.txt.gz"]; |
39 |
[ASIHTTPRequest removeFileAtPath:gzippedFilePath error:&error]; |
40 |
if (error) { |
41 |
GHFail(@"Failed to remove old file, cannot proceed with test"); |
42 |
} |
43 |
[originalString writeToFile:filePath atomically:NO encoding:NSUTF8StringEncoding error:&error]; |
44 |
if (error) { |
45 |
GHFail(@"Failed to write string, cannot proceed with test"); |
46 |
} |
47 |
|
48 |
NSTask *task = [[[NSTask alloc] init] autorelease]; |
49 |
[task setLaunchPath:@"/usr/bin/gzip"]; |
50 |
[task setArguments:[NSArray arrayWithObject:filePath]]; |
51 |
[task launch]; |
52 |
[task waitUntilExit]; |
53 |
|
54 |
NSData *deflatedData = [NSData dataWithContentsOfFile:gzippedFilePath]; |
55 |
|
56 |
NSData *inflatedData = [ASIDataDecompressor uncompressData:deflatedData error:&error]; |
57 |
if (error) { |
58 |
GHFail(@"Inflate failed because %@",error); |
59 |
} |
60 |
|
61 |
NSString *inflatedString = [[[NSString alloc] initWithBytes:[inflatedData bytes] length:[inflatedData length] encoding:NSUTF8StringEncoding] autorelease]; |
62 |
|
63 |
|
64 |
BOOL success = [inflatedString isEqualToString:originalString]; |
65 |
GHAssertTrue(success,@"inflated data is not the same as original"); |
66 |
|
67 |
// Test file to file inflate |
68 |
NSString *inflatedFilePath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"inflated_file.txt"]; |
69 |
[ASIHTTPRequest removeFileAtPath:inflatedFilePath error:&error]; |
70 |
if (error) { |
71 |
GHFail(@"Failed to remove old file, cannot proceed with test"); |
72 |
} |
73 |
|
74 |
if (![ASIDataDecompressor uncompressDataFromFile:gzippedFilePath toFile:inflatedFilePath error:&error]) { |
75 |
GHFail(@"Inflate failed because %@",error); |
76 |
} |
77 |
|
78 |
originalString = [NSString stringWithContentsOfFile:inflatedFilePath encoding:NSUTF8StringEncoding error:&error]; |
79 |
if (error) { |
80 |
GHFail(@"Failed to read the inflated data, cannot proceed with test"); |
81 |
} |
82 |
|
83 |
success = [inflatedString isEqualToString:originalString]; |
84 |
GHAssertTrue(success,@"inflated data is not the same as original"); |
85 |
|
86 |
} |
87 |
|
88 |
- (void)testDeflateData |
89 |
{ |
90 |
|
91 |
NSString *originalString = [NSString stringWithContentsOfFile:[[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"story.txt"] encoding:NSUTF8StringEncoding error:NULL]; |
92 |
|
93 |
// Test in-memory deflate using compressData:error: |
94 |
NSError *error = nil; |
95 |
NSData *deflatedData = [ASIDataCompressor compressData:[originalString dataUsingEncoding:NSUTF8StringEncoding] error:&error]; |
96 |
if (error) { |
97 |
GHFail(@"Failed to deflate the data"); |
98 |
} |
99 |
|
100 |
NSString *gzippedFilePath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"uncompressed_file.txt.gz"]; |
101 |
[ASIHTTPRequest removeFileAtPath:gzippedFilePath error:&error]; |
102 |
if (error) { |
103 |
GHFail(@"Failed to remove old file, cannot proceed with test"); |
104 |
} |
105 |
|
106 |
[deflatedData writeToFile:gzippedFilePath options:0 error:&error]; |
107 |
if (error) { |
108 |
GHFail(@"Failed to write data, cannot proceed with test"); |
109 |
} |
110 |
|
111 |
NSString *filePath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"uncompressed_file.txt"]; |
112 |
[ASIHTTPRequest removeFileAtPath:filePath error:&error]; |
113 |
if (error) { |
114 |
GHFail(@"Failed to remove old file, cannot proceed with test"); |
115 |
} |
116 |
|
117 |
NSTask *task = [[[NSTask alloc] init] autorelease]; |
118 |
[task setLaunchPath:@"/usr/bin/gzip"]; |
119 |
[task setArguments:[NSArray arrayWithObjects:@"-d",gzippedFilePath,nil]]; |
120 |
[task launch]; |
121 |
[task waitUntilExit]; |
122 |
|
123 |
NSString *inflatedString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error]; |
124 |
if (error) { |
125 |
GHFail(@"Failed to read the inflated data, cannot proceed with test"); |
126 |
} |
127 |
|
128 |
BOOL success = [inflatedString isEqualToString:originalString]; |
129 |
GHAssertTrue(success,@"inflated data is not the same as original"); |
130 |
|
131 |
|
132 |
// Test file to file deflate |
133 |
[ASIHTTPRequest removeFileAtPath:gzippedFilePath error:&error]; |
134 |
|
135 |
if (![ASIDataCompressor compressDataFromFile:filePath toFile:gzippedFilePath error:&error]) { |
136 |
GHFail(@"Deflate failed because %@",error); |
137 |
} |
138 |
[ASIHTTPRequest removeFileAtPath:filePath error:&error]; |
139 |
|
140 |
task = [[[NSTask alloc] init] autorelease]; |
141 |
[task setLaunchPath:@"/usr/bin/gzip"]; |
142 |
[task setArguments:[NSArray arrayWithObjects:@"-d",gzippedFilePath,nil]]; |
143 |
[task launch]; |
144 |
[task waitUntilExit]; |
145 |
|
146 |
inflatedString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error]; |
147 |
|
148 |
success = ([inflatedString isEqualToString:originalString]); |
149 |
GHAssertTrue(success,@"deflate data is not the same as that generated by gzip"); |
150 |
|
151 |
// Test for bug https://github.com/pokeb/asi-http-request/issues/147 |
152 |
[ASIHTTPRequest removeFileAtPath:gzippedFilePath error:&error]; |
153 |
[ASIHTTPRequest removeFileAtPath:filePath error:&error]; |
154 |
|
155 |
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://spaceharvest.com/i/screen6.png"]]; |
156 |
[request setDownloadDestinationPath:filePath]; |
157 |
[request startSynchronous]; |
158 |
|
159 |
if (![ASIDataCompressor compressDataFromFile:filePath toFile:gzippedFilePath error:&error]) { |
160 |
GHFail(@"Deflate failed because %@",error); |
161 |
} |
162 |
|
163 |
unsigned long long originalFileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error] fileSize]; |
164 |
[ASIHTTPRequest removeFileAtPath:filePath error:&error]; |
165 |
|
166 |
task = [[[NSTask alloc] init] autorelease]; |
167 |
[task setLaunchPath:@"/usr/bin/gzip"]; |
168 |
[task setArguments:[NSArray arrayWithObjects:@"-d",gzippedFilePath,nil]]; |
169 |
[task launch]; |
170 |
[task waitUntilExit]; |
171 |
|
172 |
unsigned long long inflatedFileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error] fileSize]; |
173 |
|
174 |
success = (originalFileSize == inflatedFileSize); |
175 |
GHAssertTrue(success,@"inflated data is not the same size as the original"); |
176 |
|
177 |
} |
178 |
|
179 |
@end |