Chromium Code Reviews| 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 "content/test/net/url_request_mock_http_job.h" | |
| 10 #include "net/url_request/url_request.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 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 } | |
|
mmenke
2012/12/13 16:12:04
nit: Suggest a line break here.
| |
| 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 ignore_query) { | |
| 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 38 // It's ok to do a blocking disk access on this thread; this class | |
| 39 // is just used for tests. | |
| 40 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
|
mmenke
2012/12/13 16:12:04
nit: Suggest doing this just before PathExists, t
| |
| 41 GURL gurl(url); | |
| 42 EXPECT_EQ("http", gurl.scheme()); | |
| 43 EXPECT_EQ("localhost", gurl.host()); | |
| 44 EXPECT_TRUE(file_util::PathExists(path)); | |
| 45 if (ignore_query) { | |
| 46 ignore_query_responses_[gurl] = path; | |
| 47 } else { | |
| 48 responses_[gurl] = path; | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 // Returns how many requests have been issued that have a stored reply. | |
| 53 int GetHitCount() const { | |
| 54 base::AutoLock auto_lock(hit_count_lock_); | |
| 55 return hit_count_; | |
| 56 } | |
| 57 | |
| 58 private: | |
| 59 // When computing matches, this ignores the query parameters of the url. | |
| 60 virtual net::URLRequestJob* MaybeCreateJob( | |
| 61 net::URLRequest* request, | |
| 62 net::NetworkDelegate* network_delegate) const OVERRIDE { | |
| 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 64 if (request->url().scheme() != "http" || | |
| 65 request->url().host() != "localhost") { | |
| 66 return NULL; | |
| 67 } | |
| 68 | |
| 69 ResponseMap::const_iterator it = responses_.find(request->url()); | |
| 70 if (it == responses_.end()) { | |
| 71 // Search for this request's url, ignoring any query parameters. | |
| 72 GURL url = request->url(); | |
| 73 if (url.has_query()) { | |
| 74 GURL::Replacements replacements; | |
| 75 replacements.ClearQuery(); | |
| 76 url = url.ReplaceComponents(replacements); | |
| 77 } | |
| 78 it = ignore_query_responses_.find(url); | |
| 79 if (it == ignore_query_responses_.end()) | |
| 80 return NULL; | |
| 81 } | |
| 82 { | |
| 83 base::AutoLock auto_lock(hit_count_lock_); | |
| 84 ++hit_count_; | |
| 85 } | |
| 86 | |
| 87 return new URLRequestMockHTTPJob(request, | |
| 88 network_delegate, | |
| 89 it->second); | |
| 90 } | |
| 91 | |
| 92 typedef std::map<GURL, FilePath> ResponseMap; | |
|
mmenke
2012/12/13 16:12:04
nit: Private typedefs should go before private me
| |
| 93 ResponseMap responses_; | |
| 94 ResponseMap ignore_query_responses_; | |
| 95 | |
| 96 mutable base::Lock hit_count_lock_; | |
| 97 mutable int hit_count_; | |
| 98 | |
| 99 DISALLOW_COPY_AND_ASSIGN(Delegate); | |
| 100 }; | |
| 101 | |
| 102 | |
| 103 URLRequestPrepackagedInterceptor::URLRequestPrepackagedInterceptor() | |
| 104 : delegate_(new Delegate) { | |
| 105 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 106 base::Bind(&Delegate::Register, | |
| 107 base::Unretained(delegate_))); | |
| 108 } | |
| 109 | |
| 110 URLRequestPrepackagedInterceptor::~URLRequestPrepackagedInterceptor() { | |
| 111 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, delegate_); | |
| 112 } | |
| 113 | |
| 114 void URLRequestPrepackagedInterceptor::SetResponse(const std::string& url, | |
| 115 const FilePath& path) { | |
| 116 GURL gurl(url); | |
| 117 EXPECT_EQ("http", gurl.scheme()); | |
| 118 EXPECT_EQ("localhost", gurl.host()); | |
|
mmenke
2012/12/13 16:12:04
Think doing this both here and in Delegate::SetRes
mmenke
2012/12/13 16:12:04
optional: May want to do a CHECK instead of an EX
| |
| 119 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 120 base::Bind(&Delegate::SetResponse, | |
| 121 base::Unretained(delegate_), url, path, | |
| 122 false)); | |
| 123 } | |
| 124 | |
| 125 void URLRequestPrepackagedInterceptor::SetResponseIgnoreQuery( | |
| 126 const std::string& url, | |
| 127 const FilePath& path) { | |
| 128 GURL gurl(url); | |
| 129 EXPECT_EQ("http", gurl.scheme()); | |
| 130 EXPECT_EQ("localhost", gurl.host()); | |
|
mmenke
2012/12/13 16:12:04
Same comment as above.
| |
| 131 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 132 base::Bind(&Delegate::SetResponse, | |
| 133 base::Unretained(delegate_), url, path, | |
| 134 true)); | |
| 135 } | |
| 136 | |
| 137 int URLRequestPrepackagedInterceptor::GetHitCount() { | |
| 138 return delegate_->GetHitCount(); | |
| 139 } | |
| 140 | |
| 141 } // namespace content | |
| OLD | NEW |