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