| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 #include "content/test/net/url_request_prepackaged_interceptor.h" |
| 5 |
| 6 #include "base/file_util.h" |
| 7 #include "base/threading/thread_restrictions.h" |
| 8 #include "content/public/browser/browser_thread.h" |
| 9 #include "net/url_request/url_request.h" |
| 10 #include "net/url_request/url_request_filter.h" |
| 11 #include "net/url_request/url_request_test_job.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 using content::BrowserThread; |
| 15 |
| 16 namespace content { |
| 17 |
| 18 class URLRequestPrepackagedInterceptor::Delegate |
| 19 : public net::URLRequestJobFactory::ProtocolHandler { |
| 20 public: |
| 21 Delegate() : hit_count_(0) {} |
| 22 virtual ~Delegate() { |
| 23 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", |
| 24 "localhost"); |
| 25 } |
| 26 void Register() { |
| 27 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler( |
| 28 "http", "localhost", this); |
| 29 } |
| 30 |
| 31 // When requests for |url| arrive, respond with the contents of |path|. The |
| 32 // hostname of |url| must be "localhost" to avoid DNS lookups, and the scheme |
| 33 // must be "http". |
| 34 void SetResponse(const std::string& url, |
| 35 const FilePath& path, |
| 36 bool queryless) { |
| 37 // It's ok to do a blocking disk access on this thread; this class |
| 38 // is just used for tests. |
| 39 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 40 GURL gurl(url); |
| 41 EXPECT_EQ("http", gurl.scheme()); |
| 42 EXPECT_EQ("localhost", gurl.host()); |
| 43 EXPECT_TRUE(file_util::PathExists(path)); |
| 44 if (queryless) { |
| 45 queryless_responses_[gurl] = path; |
| 46 } else { |
| 47 responses_[gurl] = path; |
| 48 } |
| 49 } |
| 50 |
| 51 // Returns how many requests have been issued that have a stored reply. |
| 52 int GetHitCount() const { |
| 53 base::AutoLock auto_lock(hit_count_lock_); |
| 54 return hit_count_; |
| 55 } |
| 56 |
| 57 private: |
| 58 // When computing matches, this ignores the query parameters of the url. |
| 59 virtual net::URLRequestJob* MaybeCreateJob( |
| 60 net::URLRequest* request, |
| 61 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 62 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 63 if (request->url().scheme() != "http" || |
| 64 request->url().host() != "localhost") { |
| 65 return NULL; |
| 66 } |
| 67 |
| 68 // It's ok to do a blocking disk access on this thread; this class |
| 69 // is just used for tests. |
| 70 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 71 |
| 72 ResponseMap::const_iterator it = responses_.find(request->url()); |
| 73 if (it == responses_.end()) { |
| 74 // Search for this request's url, ignoring any query parameters. |
| 75 GURL url = request->url(); |
| 76 if (url.has_query()) { |
| 77 GURL::Replacements replacements; |
| 78 replacements.ClearQuery(); |
| 79 url = url.ReplaceComponents(replacements); |
| 80 } |
| 81 it = queryless_responses_.find(url); |
| 82 if (it == queryless_responses_.end()) |
| 83 return NULL; |
| 84 } |
| 85 const FilePath& response = it->second; |
| 86 { |
| 87 base::AutoLock auto_lock(hit_count_lock_); |
| 88 ++hit_count_; |
| 89 } |
| 90 |
| 91 std::string contents; |
| 92 EXPECT_TRUE(file_util::ReadFileToString(response, &contents)); |
| 93 |
| 94 return new net::URLRequestTestJob(request, |
| 95 network_delegate, |
| 96 net::URLRequestTestJob::test_headers(), |
| 97 contents, |
| 98 true); |
| 99 } |
| 100 |
| 101 typedef std::map<GURL, FilePath> ResponseMap; |
| 102 ResponseMap responses_; |
| 103 ResponseMap queryless_responses_; |
| 104 |
| 105 mutable base::Lock hit_count_lock_; |
| 106 mutable int hit_count_; |
| 107 |
| 108 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 109 }; |
| 110 |
| 111 |
| 112 URLRequestPrepackagedInterceptor::URLRequestPrepackagedInterceptor() |
| 113 : delegate_(new Delegate) { |
| 114 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 115 base::Bind(&Delegate::Register, |
| 116 base::Unretained(delegate_))); |
| 117 } |
| 118 |
| 119 URLRequestPrepackagedInterceptor::~URLRequestPrepackagedInterceptor() { |
| 120 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, delegate_); |
| 121 } |
| 122 |
| 123 void URLRequestPrepackagedInterceptor::SetResponse(const std::string& url, |
| 124 const FilePath& path) { |
| 125 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 126 base::Bind(&Delegate::SetResponse, |
| 127 base::Unretained(delegate_), url, path, |
| 128 false)); |
| 129 } |
| 130 |
| 131 void URLRequestPrepackagedInterceptor::SetQuerylessResponse( |
| 132 const std::string& url, |
| 133 const FilePath& path) { |
| 134 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 135 base::Bind(&Delegate::SetResponse, |
| 136 base::Unretained(delegate_), url, path, |
| 137 true)); |
| 138 } |
| 139 |
| 140 int URLRequestPrepackagedInterceptor::GetHitCount() { |
| 141 return delegate_->GetHitCount(); |
| 142 } |
| 143 |
| 144 } // namespace content |
| OLD | NEW |