1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "ios/chrome/browser/net/retryable_url_fetcher.h"
7 #import "base/mac/scoped_nsobject.h"
8 #include "ios/web/public/test/test_web_thread.h"
9 #include "net/url_request/test_url_fetcher_factory.h"
10 #include "net/url_request/url_fetcher_delegate.h"
11 #include "net/url_request/url_request_test_util.h"
12 #import "testing/gtest_mac.h"
13 #include "testing/platform_test.h"
16 // An arbitrary text string for a fake response.
17 NSString* const kFakeResponseString = @"Something interesting here.";
20 // Delegate object to provide data for RetryableURLFetcher and
21 // handles the callback when URL is fetched.
22 @interface TestRetryableURLFetcherDelegate
23 : NSObject<RetryableURLFetcherDelegate>
24 // Counts the number of times that a successful response has been processed.
25 @property(nonatomic, assign) NSUInteger responsesProcessed;
28 @implementation TestRetryableURLFetcherDelegate
29 @synthesize responsesProcessed;
31 - (NSString*)urlToFetch {
32 return @"http://www.google.com";
35 - (void)processSuccessResponse:(NSString*)response {
37 EXPECT_NSEQ(kFakeResponseString, response);
46 class RetryableURLFetcherTest : public PlatformTest {
48 void SetUp() override {
49 PlatformTest::SetUp();
50 test_delegate_.reset([[TestRetryableURLFetcherDelegate alloc] init]);
52 new web::TestWebThread(web::WebThread::IO, &message_loop_));
55 net::TestURLFetcherFactory factory_;
56 scoped_ptr<web::TestWebThread> io_thread_;
57 base::MessageLoop message_loop_;
58 base::scoped_nsobject<TestRetryableURLFetcherDelegate> test_delegate_;
61 TEST_F(RetryableURLFetcherTest, TestResponse200) {
62 scoped_refptr<net::URLRequestContextGetter> request_context_getter =
63 new net::TestURLRequestContextGetter(message_loop_.task_runner());
64 base::scoped_nsobject<RetryableURLFetcher> retryableFetcher(
65 [[RetryableURLFetcher alloc]
66 initWithRequestContextGetter:request_context_getter.get()
67 delegate:test_delegate_.get()
69 [retryableFetcher startFetch];
71 // Manually calls the delegate.
72 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
74 DCHECK(fetcher->delegate());
75 [test_delegate_ setResponsesProcessed:0U];
76 fetcher->set_response_code(200);
77 fetcher->SetResponseString([kFakeResponseString UTF8String]);
78 fetcher->delegate()->OnURLFetchComplete(fetcher);
79 EXPECT_EQ(1U, [test_delegate_ responsesProcessed]);
82 TEST_F(RetryableURLFetcherTest, TestResponse404) {
83 scoped_refptr<net::URLRequestContextGetter> request_context_getter =
84 new net::TestURLRequestContextGetter(message_loop_.task_runner());
85 base::scoped_nsobject<RetryableURLFetcher> retryableFetcher(
86 [[RetryableURLFetcher alloc]
87 initWithRequestContextGetter:request_context_getter.get()
88 delegate:test_delegate_.get()
90 [retryableFetcher startFetch];
92 // Manually calls the delegate.
93 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
95 DCHECK(fetcher->delegate());
96 [test_delegate_ setResponsesProcessed:0U];
97 fetcher->set_response_code(404);
98 fetcher->SetResponseString("");
99 fetcher->delegate()->OnURLFetchComplete(fetcher);
100 EXPECT_EQ(0U, [test_delegate_ responsesProcessed]);