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

Side by Side Diff: components/cronet/android/test/cronet_mock_job_interceptor.cc

Issue 558333007: Setup initial mock url request job tests for Cronet (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Misha's comments Created 6 years, 3 months 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
(Empty)
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
mmenke 2014/09/19 17:15:55 nit: Don't use (c) in new files.
xunjieli 2014/09/19 20:39:15 Done.
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 "cronet_mock_job_interceptor.h"
6
7 #include "base/files/file_path.h"
8 #include "base/macros.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/path_service.h"
11 #include "net/base/net_errors.h"
12 #include "net/test/url_request/url_request_failed_job.h"
13 #include "net/test/url_request/url_request_mock_http_job.h"
14 #include "net/url_request/url_request_filter.h"
15 #include "net/url_request/url_request_interceptor.h"
16
17 namespace cronet {
18
19 namespace {
20
21 // A mock URL that will produces a redirect.
22 const char kMockCronetTestRedirectUrl[] = "http://mock.cronet.redirect/";
23
24 // A mock URL that will return a 404 response.
25 const char kMockCronetTestSuccessUrl[] = "http://mock.cronet.success/";
26
27 // A mock URL that will result in a failed url request.
28 const char kMockCronetTestFailedUrl[] = "http://mock.cronet.failed/";
29
30 class CronetMockJobInterceptor : public net::URLRequestInterceptor {
31 public:
32 CronetMockJobInterceptor() {}
33 virtual ~CronetMockJobInterceptor() {}
34
35 // net::URLRequestInterceptor implementation
36 virtual net::URLRequestJob* MaybeInterceptRequest(
37 net::URLRequest* request,
38 net::NetworkDelegate* network_delegate) const OVERRIDE {
39 base::FilePath test_files_root;
40 PathService::Get(base::DIR_ANDROID_APP_DATA, &test_files_root);
41 if (request->url() == GURL(kMockCronetTestRedirectUrl)) {
42 return new net::URLRequestMockHTTPJob(
43 request,
44 network_delegate,
45 test_files_root.Append("redirect.html"),
46 base::MessageLoop::current()->message_loop_proxy());
47 }
48 if (request->url() == GURL(kMockCronetTestSuccessUrl)) {
49 return new net::URLRequestMockHTTPJob(
50 request,
51 network_delegate,
52 test_files_root.Append("success.html"),
53 base::MessageLoop::current()->message_loop_proxy());
54 }
55 return new net::URLRequestFailedJob(
56 request, network_delegate, net::ERR_FAILED);
57 }
58
59 private:
60 DISALLOW_COPY_AND_ASSIGN(CronetMockJobInterceptor);
61 };
62
63 } // namespace
64
65 // static
66 bool AddUrlInterceptors() {
67 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
68 // TODO(xunjieli): use a map to avoid code duplication.
69 return filter->AddUrlInterceptor(GURL(kMockCronetTestSuccessUrl),
70 scoped_ptr<net::URLRequestInterceptor>(
71 new CronetMockJobInterceptor())) &&
72 filter->AddUrlInterceptor(GURL(kMockCronetTestRedirectUrl),
73 scoped_ptr<net::URLRequestInterceptor>(
74 new CronetMockJobInterceptor())) &&
mmenke 2014/09/19 17:15:55 As Misha said, we really shouldn't be listing thes
xunjieli 2014/09/19 20:39:15 Done. I see! That's very smart! But I am having on
75 filter->AddUrlInterceptor(GURL(kMockCronetTestFailedUrl),
76 scoped_ptr<net::URLRequestInterceptor>(
77 new CronetMockJobInterceptor()));
78 }
79
80 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698