Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(476)

Side by Side Diff: content/test/net/url_request_prepackaged_interceptor.cc

Issue 11293252: Change Interceptors into URLRequestJobFactory::ProtocolHandlers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address erikwright's third round of comments Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 ignore_query) {
mmenke 2012/12/11 17:22:38 BrowserThread::CurrentlyOn(BrowserThread::IO)?
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 (ignore_query) {
45 ignore_query_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));
mmenke 2012/12/11 17:22:38 May actually want to use CHECK here. If we're on
63 if (request->url().scheme() != "http" ||
64 request->url().host() != "localhost") {
65 return NULL;
mmenke 2012/12/11 17:22:38 nit: Fix indent.
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;
mmenke 2012/12/11 17:22:38 Suggest moving this to just before ReadFileToStrin
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 = ignore_query_responses_.find(url);
82 if (it == ignore_query_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));
mmenke 2012/12/11 17:22:38 Can we just use a "URLRequestMockHTTPJob" here?
pauljensen 2012/12/17 16:26:49 I tried for a while to use URLRequestMockHTTPJob b
mmenke 2012/12/18 20:32:15 Could just fix URLRequestMockHTTPJob to use a nice
mmenke 2012/12/19 16:21:21 BTW - no need to worry about that now. Your solut
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 ignore_query_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) {
mmenke 2012/12/11 17:22:38 Suggest a DCHECK_EQ on url.hostname
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::SetResponseIgnoreQuery(
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698