Add a RKRequest attribute to control redirect handling

The default behavior is the same as before, to follow redirects. If RKRequest.followRedirect is set to NO, then a redirect (301, 302, 307) response will not be followed, and the request processing will proceed using the current request only.
This commit is contained in:
Marcus Brito
2012-04-04 09:20:00 -03:00
committed by Blake Watters
parent e0b248da46
commit ea100b6aac
7 changed files with 64 additions and 1 deletions

View File

@@ -268,4 +268,31 @@
assertThatInteger([loader.response bodyEncoding], is(equalToInteger(NSASCIIStringEncoding)));
}
- (void)testFollowRedirect {
RKClient* client = [RKTestFactory client];
RKTestResponseLoader* loader = [RKTestResponseLoader responseLoader];
[client get:@"/redirection" delegate:loader];
[loader waitForResponse];
assertThatInteger(loader.response.statusCode, is(equalToInteger(200)));
id body = [loader.response parsedBody:NULL];
assertThat([body objectForKey:@"redirected"], is(equalTo([NSNumber numberWithBool:YES])));
}
- (void)testNoFollowRedirect {
RKClient* client = [RKTestFactory client];
RKTestResponseLoader* loader = [RKTestResponseLoader responseLoader];
RKRequest* request = [client requestWithResourcePath:@"/redirection"];
request.method = RKRequestMethodGET;
request.followRedirect = NO;
request.delegate = loader;
[request send];
[loader waitForResponse];
assertThatInteger(loader.response.statusCode, is(equalToInteger(302)));
assertThat([loader.response.allHeaderFields objectForKey:@"Location"], is(equalTo(@"/redirection/target")));
}
@end

View File

@@ -0,0 +1,13 @@
module RestKit
module Network
class Redirection < Sinatra::Base
get '/redirection' do
[302, {"Location" => '/redirection/target'}, ""]
end
get '/redirection/target' do
[200, {"Content-Type" => "application/json"}, {"redirected" => true}.to_json]
end
end
end
end

View File

@@ -18,6 +18,7 @@ require 'restkit/network/authentication'
require 'restkit/network/etags'
require 'restkit/network/timeout'
require 'restkit/network/oauth2'
require 'restkit/network/redirection'
class Person < Struct.new(:name, :age)
def to_json(*args)
@@ -38,6 +39,7 @@ class RestKitTestServer < Sinatra::Base
use RestKit::Network::ETags
use RestKit::Network::Timeout
use RestKit::Network::OAuth2
use RestKit::Network::Redirection
def render_fixture(path, options = {})
send_file File.join(settings.public_folder, path), options