Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mock_url_fetcher.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef NET_INSTAWEB_HTTP_PUBLIC_MOCK_URL_FETCHER_H_
20 #define NET_INSTAWEB_HTTP_PUBLIC_MOCK_URL_FETCHER_H_
21 
22 #include <map>
23 
31 
32 namespace net_instaweb {
33 
34 class AsyncFetch;
35 class MessageHandler;
36 class ThreadSystem;
37 class Timer;
38 
42  public:
44  virtual ~MockUrlFetcher();
45 
46  void SetResponse(const StringPiece& url,
47  const ResponseHeaders& response_header,
48  const StringPiece& response_body);
49 
52  void AddToResponse(const StringPiece& url,
53  const StringPiece& name,
54  const StringPiece& value);
55 
59  void SetConditionalResponse(const StringPiece& url,
60  int64 last_modified_date,
61  const GoogleString& etag,
62  const ResponseHeaders& response_header,
63  const StringPiece& response_body);
64 
66  virtual void Fetch(const GoogleString& url,
67  MessageHandler* message_handler,
68  AsyncFetch* fetch);
69 
70  virtual bool SupportsHttps() const {
71  ScopedMutex lock(mutex_.get());
72  return supports_https_;
73  }
74 
75  void set_fetcher_supports_https(bool supports_https) {
76  ScopedMutex lock(mutex_.get());
77  supports_https_ = supports_https;
78  }
79 
82  ScopedMutex lock(mutex_.get());
83  return last_referer_;
84  }
85 
92  void SetResponseFailure(const StringPiece& url);
93 
95  void Clear();
96 
98  void RemoveResponse(const StringPiece& url);
99 
102  void Disable() {
103  ScopedMutex lock(mutex_.get());
104  enabled_ = false;
105  }
106  void Enable() {
107  ScopedMutex lock(mutex_.get());
108  enabled_ = true;
109  }
110 
113  void set_fail_on_unexpected(bool x) {
114  ScopedMutex lock(mutex_.get());
115  fail_on_unexpected_ = x;
116  }
117 
120  void set_update_date_headers(bool x) {
121  ScopedMutex lock(mutex_.get());
122  update_date_headers_ = x;
123  }
124 
127  void set_omit_empty_writes(bool x) {
128  ScopedMutex lock(mutex_.get());
129  omit_empty_writes_ = x;
130  }
131 
135  void set_fail_after_headers(bool x) {
136  ScopedMutex lock(mutex_.get());
137  fail_after_headers_ = x;
138  }
139 
142  void set_verify_host_header(bool x) {
143  ScopedMutex lock(mutex_.get());
144  verify_host_header_ = x;
145  }
146 
147  void set_verify_pagespeed_header_off(bool x) {
148  ScopedMutex lock(mutex_.get());
149  verify_pagespeed_header_off_ = x;
150  }
151 
152  void set_timer(Timer* timer) {
153  ScopedMutex lock(mutex_.get());
154  timer_ = timer;
155  }
156 
160  void set_split_writes(bool val) {
161  ScopedMutex lock(mutex_.get());
162  split_writes_ = val;
163  }
164 
166  void set_error_message(const GoogleString& msg) {
167  ScopedMutex lock(mutex_.get());
168  error_message_ = msg;
169  }
170 
171  void set_strip_query_params(bool strip_query_params) {
172  ScopedMutex lock(mutex_.get());
173  strip_query_params_ = strip_query_params;
174  }
175 
176  private:
177  class HttpResponse {
178  public:
179  HttpResponse(int64 last_modified_time, const GoogleString& etag,
180  const ResponseHeaders& in_header, const StringPiece& in_body)
181  : last_modified_time_(last_modified_time),
182  etag_(etag),
183  body_(in_body.data(), in_body.size()),
184  success_(true) {
185  header_.CopyFrom(in_header);
186  }
187 
188  const int64 last_modified_time() const { return last_modified_time_; }
189  const GoogleString& etag() const { return etag_; }
190  const ResponseHeaders& header() const { return header_; }
191  ResponseHeaders* mutable_header() { return &header_; }
192  const GoogleString& body() const { return body_; }
193  void set_success(bool success) { success_ = success; }
194  bool success() const { return success_; }
195 
196  private:
197  int64 last_modified_time_;
198  GoogleString etag_;
199  ResponseHeaders header_;
200  GoogleString body_;
201  bool success_;
202 
203 
204  };
205  typedef std::map<const GoogleString, HttpResponse*> ResponseMap;
206 
209  ResponseMap response_map_;
210 
211  bool enabled_;
212  bool fail_on_unexpected_;
213  bool update_date_headers_;
214  bool omit_empty_writes_;
215  bool fail_after_headers_;
216  bool verify_host_header_;
217  bool verify_pagespeed_header_off_;
218  bool split_writes_;
219  bool supports_https_;
220  bool strip_query_params_;
221 
222  GoogleString error_message_;
223  Timer* timer_;
224  GoogleString last_referer_;
225  scoped_ptr<ThreadSystem> thread_system_;
226  scoped_ptr<AbstractMutex> mutex_;
227 
228 
229 };
230 
231 }
232 
233 #endif
void set_split_writes(bool val)
Definition: mock_url_fetcher.h:160
virtual bool SupportsHttps() const
Definition: mock_url_fetcher.h:70
void AddToResponse(const StringPiece &url, const StringPiece &name, const StringPiece &value)
Read/write API for HTTP response headers.
Definition: response_headers.h:37
void set_fail_on_unexpected(bool x)
Definition: mock_url_fetcher.h:113
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
#define ScopedMutex(x)
Definition: abstract_mutex.h:69
const GoogleString & last_referer()
Return the referer of this fetching request.
Definition: mock_url_fetcher.h:81
Helper class for lexically scoped mutexing.
Definition: abstract_mutex.h:46
Definition: async_fetch.h:53
virtual void Fetch(const GoogleString &url, MessageHandler *message_handler, AsyncFetch *fetch)
Fetching unset URLs will cause EXPECT failures as well as Done(false).
void set_omit_empty_writes(bool x)
Definition: mock_url_fetcher.h:127
void Disable()
Definition: mock_url_fetcher.h:102
Definition: mock_url_fetcher.h:41
void set_update_date_headers(bool x)
Definition: mock_url_fetcher.h:120
void Clear()
Clear all set responses.
void set_error_message(const GoogleString &msg)
If this is non-empty, we will write this out any time we report an error.
Definition: mock_url_fetcher.h:166
void SetConditionalResponse(const StringPiece &url, int64 last_modified_date, const GoogleString &etag, const ResponseHeaders &response_header, const StringPiece &response_body)
Definition: message_handler.h:39
void SetResponseFailure(const StringPiece &url)
void RemoveResponse(const StringPiece &url)
Remove a single response. Will be a no-op if no response was set for url.
void set_fail_after_headers(bool x)
Definition: mock_url_fetcher.h:135
Definition: url_async_fetcher.h:33
void set_verify_host_header(bool x)
Definition: mock_url_fetcher.h:142