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

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

Powered by Google App Engine
This is Rietveld 408576698