Permalink
Browse files

ASIWebPageRequest: xmlSaveDoc() will preserve the response encoding, …

…so stop setting it back to UTF-8

Add tests for ASIWebPageRequest encoding
Also, strip any gzip content-encoding header, since the parsed response will not be deflated (cache does this anyway, but we can't guarantee we'll have one)
  • Loading branch information...
1 parent 324bac0 commit a73bed768cd693c8ffb70eb543fe373f4ca8253e @pokeb committed Jan 6, 2011
View
@@ -24,7 +24,7 @@
#import "ASIDataCompressor.h"
// Automatically set on build
-NSString *ASIHTTPRequestVersion = @"v1.8-29 2011-01-06";
+NSString *ASIHTTPRequestVersion = @"v1.8-32 2011-01-06";
NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";
@@ -367,8 +367,12 @@ - (void)finishedFetchingExternalResources:(ASINetworkQueue *)queue
#endif
}
- // libxml will generate UTF-8
- [self setResponseEncoding:NSUTF8StringEncoding];
+ // Strip the content encoding if the original response was gzipped
+ if ([self isResponseCompressed]) {
+ NSMutableDictionary *headers = [[self responseHeaders] mutableCopy];
+ [headers removeObjectForKey:@"Content-Encoding"];
+ [self setResponseHeaders:headers];
+ }
}
xmlFreeDoc(doc);
@@ -0,0 +1,16 @@
+//
+// ASIWebPageRequestTests.h
+// Mac
+//
+// Created by Ben Copsey on 06/01/2011.
+// Copyright 2011 All-Seeing Interactive. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "ASITestCase.h"
+
+@interface ASIWebPageRequestTests : ASITestCase {
+
+}
+
+@end
@@ -0,0 +1,41 @@
+//
+// ASIWebPageRequestTests.m
+// Mac
+//
+// Created by Ben Copsey on 06/01/2011.
+// Copyright 2011 All-Seeing Interactive. All rights reserved.
+//
+
+#import "ASIWebPageRequestTests.h"
+#import "ASIWebPageRequest.h"
+
+@implementation ASIWebPageRequestTests
+
+- (void)testEncoding
+{
+ NSArray *encodings = [NSArray arrayWithObjects:@"us-ascii",@"iso-8859-1",@"utf-16",@"utf-8",nil];
+ NSArray *expectedResponses = [NSArray arrayWithObjects:@"Hi there",@"Olá",@"你好",@"今日は",nil];
+ NSUInteger i;
+ for (i=0; i<[encodings count]; i++) {
+ ASIWebPageRequest *request = [ASIWebPageRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://allseeing-i.com/ASIHTTPRequest/tests/asiwebpagerequest/character-encoding/%@",[encodings objectAtIndex:i]]]];
+ [request setUserInfo:[NSDictionary dictionaryWithObject:[expectedResponses objectAtIndex:i] forKey:@"expected-response"]];
+ [request setDelegate:self];
+ [request setUrlReplacementMode:ASIReplaceExternalResourcesWithLocalURLs];
+ [request startAsynchronous];
+ }
+}
+
+- (void)requestFinished:(ASIHTTPRequest *)request
+{
+ if ([[request userInfo] objectForKey:@"expected-response"]) {
+ BOOL success = ([[request responseString] rangeOfString:[[request userInfo] objectForKey:@"expected-response"]].location != NSNotFound);
+ GHAssertTrue(success,@"Response HTML used wrong encoding");
+ }
+}
+
+- (void)requestFailed:(ASIHTTPRequest *)request
+{
+ GHAssertNil([request error],@"Request failed, cannot proceed with test");
+}
+
+@end
@@ -49,6 +49,7 @@
B55B60140F7659A30064029C /* libz.1.2.3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = B55B5EB70F7658200064029C /* libz.1.2.3.dylib */; };
B565297E101C8D01000499CF /* ASITestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = B565297C101C8D01000499CF /* ASITestCase.m */; };
B5728CA010AEC2F4004F4CE6 /* StressTests.m in Sources */ = {isa = PBXBuildFile; fileRef = B5728C9F10AEC2F4004F4CE6 /* StressTests.m */; };
+ B57A610812D5FBEB00887E97 /* ASIWebPageRequestTests.m in Sources */ = {isa = PBXBuildFile; fileRef = B57A610712D5FBEB00887E97 /* ASIWebPageRequestTests.m */; };
B5873F8C10FF2884001E145F /* ASIS3BucketObject.m in Sources */ = {isa = PBXBuildFile; fileRef = B5873F8710FF2884001E145F /* ASIS3BucketObject.m */; };
B5873F8E10FF2884001E145F /* ASIS3Request.m in Sources */ = {isa = PBXBuildFile; fileRef = B5873F8B10FF2884001E145F /* ASIS3Request.m */; };
B5873F9E10FF2890001E145F /* ASICloudFilesCDNRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = B5873F9110FF2890001E145F /* ASICloudFilesCDNRequest.m */; };
@@ -170,6 +171,8 @@
B5728C9F10AEC2F4004F4CE6 /* StressTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StressTests.m; sourceTree = "<group>"; };
B5747B3A11746CF500C9414E /* ASIProgressDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASIProgressDelegate.h; sourceTree = "<group>"; };
B5747EA91174B1C100C9414E /* ASIHTTPRequestDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASIHTTPRequestDelegate.h; sourceTree = "<group>"; };
+ B57A610612D5FBEB00887E97 /* ASIWebPageRequestTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASIWebPageRequestTests.h; sourceTree = "<group>"; };
+ B57A610712D5FBEB00887E97 /* ASIWebPageRequestTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ASIWebPageRequestTests.m; sourceTree = "<group>"; };
B5873F8610FF2884001E145F /* ASIS3BucketObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ASIS3BucketObject.h; path = S3/ASIS3BucketObject.h; sourceTree = "<group>"; };
B5873F8710FF2884001E145F /* ASIS3BucketObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ASIS3BucketObject.m; path = S3/ASIS3BucketObject.m; sourceTree = "<group>"; };
B5873F8A10FF2884001E145F /* ASIS3Request.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ASIS3Request.h; path = S3/ASIS3Request.h; sourceTree = "<group>"; };
@@ -381,6 +384,8 @@
B587400610FF2913001E145F /* ASICloudFilesRequestTests.m */,
B51A1A9311DDF84700ED75CF /* ASIDownloadCacheTests.h */,
B51A1A9411DDF84700ED75CF /* ASIDownloadCacheTests.m */,
+ B57A610612D5FBEB00887E97 /* ASIWebPageRequestTests.h */,
+ B57A610712D5FBEB00887E97 /* ASIWebPageRequestTests.m */,
B53E62ED1255F73300C1E79A /* ASIDataCompressorTests.h */,
B53E62EE1255F73300C1E79A /* ASIDataCompressorTests.m */,
B5728C9E10AEC2F4004F4CE6 /* StressTests.h */,
@@ -616,6 +621,7 @@
B53E628F1255EE4B00C1E79A /* ASIDataDecompressor.m in Sources */,
B53E62EF1255F73300C1E79A /* ASIDataCompressorTests.m in Sources */,
B54B23FA12AA7D9100A7F940 /* ASIWebPageRequest.m in Sources */,
+ B57A610812D5FBEB00887E97 /* ASIWebPageRequestTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

0 comments on commit a73bed7

Please sign in to comment.