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

Side by Side Diff: net/test/url_request/url_request_mock_http_job.h

Issue 586143002: Initial implementation of Cronet Async API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Helen's comments, add CronetUrlRequestContextTest. Created 6 years, 1 month 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // A URLRequestJob class that pulls the net and http headers from disk. 5 // A URLRequestJob class that pulls the net and http headers from disk.
6 6
7 #ifndef NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_ 7 #ifndef NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_
8 #define NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_ 8 #define NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_
9 9
10 #include <string> 10 #include <string>
(...skipping 10 matching lines...) Expand all
21 } 21 }
22 22
23 namespace net { 23 namespace net {
24 class URLRequestInterceptor; 24 class URLRequestInterceptor;
25 } 25 }
26 26
27 namespace net { 27 namespace net {
28 28
29 class URLRequestMockHTTPJob : public URLRequestFileJob { 29 class URLRequestMockHTTPJob : public URLRequestFileJob {
30 public: 30 public:
31 enum FailurePhase {
32 START = 0,
33 READ_ASYNC = 1,
34 READ_SYNC = 2,
35 };
36
31 // Note that all file IO is done using |worker_pool|. 37 // Note that all file IO is done using |worker_pool|.
32 URLRequestMockHTTPJob(URLRequest* request, 38 URLRequestMockHTTPJob(URLRequest* request,
33 NetworkDelegate* network_delegate, 39 NetworkDelegate* network_delegate,
34 const base::FilePath& file_path, 40 const base::FilePath& file_path,
35 const scoped_refptr<base::TaskRunner>& task_runner); 41 const scoped_refptr<base::TaskRunner>& task_runner);
36 42
37 void Start() override; 43 void Start() override;
44 bool ReadRawData(IOBuffer* buf, int buf_size, int* bytes_read) override;
38 bool GetMimeType(std::string* mime_type) const override; 45 bool GetMimeType(std::string* mime_type) const override;
39 int GetResponseCode() const override; 46 int GetResponseCode() const override;
40 bool GetCharset(std::string* charset) override; 47 bool GetCharset(std::string* charset) override;
41 void GetResponseInfo(HttpResponseInfo* info) override; 48 void GetResponseInfo(HttpResponseInfo* info) override;
42 bool IsRedirectResponse(GURL* location, int* http_status_code) override; 49 bool IsRedirectResponse(GURL* location, int* http_status_code) override;
43 50
44 // Adds the testing URLs to the URLRequestFilter. 51 // Adds the testing URLs to the URLRequestFilter.
45 static void AddUrlHandler( 52 static void AddUrlHandler(
46 const base::FilePath& base_path, 53 const base::FilePath& base_path,
47 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); 54 const scoped_refptr<base::SequencedWorkerPool>& worker_pool);
48 55
49 // Respond to all HTTP requests of |hostname| with contents of the file 56 // Respond to all HTTP requests of |hostname| with contents of the file
50 // located at |file_path|. 57 // located at |file_path|.
51 static void AddHostnameToFileHandler( 58 static void AddHostnameToFileHandler(
52 const std::string& hostname, 59 const std::string& hostname,
53 const base::FilePath& file, 60 const base::FilePath& file,
54 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); 61 const scoped_refptr<base::SequencedWorkerPool>& worker_pool);
55 62
56 // Given the path to a file relative to the path passed to AddUrlHandler(), 63 // Given the path to a file relative to the path passed to AddUrlHandler(),
57 // construct a mock URL. 64 // construct a mock URL.
58 static GURL GetMockUrl(const base::FilePath& path); 65 static GURL GetMockUrl(const base::FilePath& path);
59 66
67 // Given the path to a file relative to the path passed to AddUrlHandler(),
68 // construct a mock URL that reports |net_error| at given |phase| of the
69 // request. Reporting |net_error| ERR_IO_PENDING results in a hung request.
70 static GURL GetMockUrlWithFailure(const base::FilePath& path,
71 FailurePhase phase,
72 int net_error);
73
60 // Returns a URLRequestJobFactory::ProtocolHandler that serves 74 // Returns a URLRequestJobFactory::ProtocolHandler that serves
61 // URLRequestMockHTTPJob's responding like an HTTP server. |base_path| is the 75 // URLRequestMockHTTPJob's responding like an HTTP server. |base_path| is the
62 // file path leading to the root of the directory to use as the root of the 76 // file path leading to the root of the directory to use as the root of the
63 // HTTP server. 77 // HTTP server.
64 static scoped_ptr<URLRequestInterceptor> CreateInterceptor( 78 static scoped_ptr<URLRequestInterceptor> CreateInterceptor(
65 const base::FilePath& base_path, 79 const base::FilePath& base_path,
66 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); 80 const scoped_refptr<base::SequencedWorkerPool>& worker_pool);
67 81
68 // Returns a URLRequestJobFactory::ProtocolHandler that serves 82 // Returns a URLRequestJobFactory::ProtocolHandler that serves
69 // URLRequestMockHTTPJob's responding like an HTTP server. It responds to all 83 // URLRequestMockHTTPJob's responding like an HTTP server. It responds to all
70 // requests with the contents of |file|. 84 // requests with the contents of |file|.
71 static scoped_ptr<URLRequestInterceptor> CreateInterceptorForSingleFile( 85 static scoped_ptr<URLRequestInterceptor> CreateInterceptorForSingleFile(
72 const base::FilePath& file, 86 const base::FilePath& file,
73 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); 87 const scoped_refptr<base::SequencedWorkerPool>& worker_pool);
74 88
75 protected: 89 protected:
76 ~URLRequestMockHTTPJob() override; 90 ~URLRequestMockHTTPJob() override;
77 91
78 private: 92 private:
79 void GetResponseInfoConst(HttpResponseInfo* info) const; 93 void GetResponseInfoConst(HttpResponseInfo* info) const;
80 void GetRawHeaders(std::string raw_headers); 94 void SetHeadersAndStart(const std::string& raw_headers);
95 // Checks query part of request url, and reports an error if it matches.
96 // Error is parsed out from the query and is reported synchronously.
97 // Reporting ERR_IO_PENDING results in a hung request.
98 // The "readasync" error is posted asynchronously.
99 bool MaybeReportErrorOnPhase(FailurePhase phase);
100
81 std::string raw_headers_; 101 std::string raw_headers_;
82 const scoped_refptr<base::TaskRunner> task_runner_; 102 const scoped_refptr<base::TaskRunner> task_runner_;
83 103
84 base::WeakPtrFactory<URLRequestMockHTTPJob> weak_ptr_factory_; 104 base::WeakPtrFactory<URLRequestMockHTTPJob> weak_ptr_factory_;
85 105
86 DISALLOW_COPY_AND_ASSIGN(URLRequestMockHTTPJob); 106 DISALLOW_COPY_AND_ASSIGN(URLRequestMockHTTPJob);
87 }; 107 };
88 108
89 } // namespace net 109 } // namespace net
90 110
91 #endif // NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_ 111 #endif // NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698