| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 "base/macros.h" |
| 6 #include "base/memory/ptr_util.h" |
| 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/threading/thread.h" |
| 9 #include "components/grpc_support/test/quic_test_server.h" |
| 10 #include "net/cert/mock_cert_verifier.h" |
| 11 #include "net/dns/mapped_host_resolver.h" |
| 12 #include "net/dns/mock_host_resolver.h" |
| 13 #include "net/http/http_server_properties_impl.h" |
| 14 #include "net/url_request/url_request_test_util.h" |
| 15 |
| 16 namespace grpc_support { |
| 17 namespace { |
| 18 |
| 19 // URLRequestContextGetter for BidirectionalStreamTest. This is used instead of |
| 20 // net::TestURLRequestContextGetter because the URLRequestContext needs to be |
| 21 // created on the test_io_thread_ for the test, and TestURLRequestContextGetter |
| 22 // does not allow for lazy instantiation of the URLRequestContext if additional |
| 23 // setup is required. |
| 24 class BidirectionalStreamTestURLRequestContextGetter |
| 25 : public net::URLRequestContextGetter { |
| 26 public: |
| 27 BidirectionalStreamTestURLRequestContextGetter( |
| 28 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
| 29 : task_runner_(task_runner) {} |
| 30 |
| 31 net::URLRequestContext* GetURLRequestContext() override { |
| 32 if (!request_context_.get()) { |
| 33 request_context_.reset(new net::TestURLRequestContext( |
| 34 true /* delay_initialization */)); |
| 35 host_resolver_.reset(new net::MappedHostResolver( |
| 36 net::HostResolver::CreateDefaultResolver(nullptr))); |
| 37 host_resolver_->SetRulesFromString("MAP test.example.com 127.0.0.1," |
| 38 "MAP notfound.example.com ~NOTFOUND"); |
| 39 mock_cert_verifier_.reset(new net::MockCertVerifier()); |
| 40 mock_cert_verifier_->set_default_result(net::OK); |
| 41 server_properties_.reset(new net::HttpServerPropertiesImpl()); |
| 42 |
| 43 // Need to enable QUIC for the test server. |
| 44 auto params = base::MakeUnique<net::HttpNetworkSession::Params>(); |
| 45 params->enable_quic = true; |
| 46 params->enable_http2 = true; |
| 47 net::AlternativeService alternative_service(net::AlternateProtocol::QUIC, |
| 48 "", kTestServerPort); |
| 49 url::SchemeHostPort quic_hint_server("https", kTestServerDomain, |
| 50 kTestServerPort); |
| 51 server_properties_->SetAlternativeService( |
| 52 quic_hint_server, alternative_service, base::Time::Max()); |
| 53 params->quic_host_whitelist.insert(kTestServerHost); |
| 54 |
| 55 request_context_->set_cert_verifier(mock_cert_verifier_.get()); |
| 56 request_context_->set_host_resolver(host_resolver_.get()); |
| 57 request_context_->set_http_server_properties(server_properties_.get()); |
| 58 request_context_->set_http_network_session_params(std::move(params)); |
| 59 |
| 60 request_context_->Init(); |
| 61 } |
| 62 return request_context_.get(); |
| 63 } |
| 64 |
| 65 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() |
| 66 const override { |
| 67 return task_runner_; |
| 68 } |
| 69 |
| 70 private: |
| 71 ~BidirectionalStreamTestURLRequestContextGetter() override {} |
| 72 |
| 73 std::unique_ptr<net::HttpServerProperties> server_properties_; |
| 74 std::unique_ptr<net::MockCertVerifier> mock_cert_verifier_; |
| 75 std::unique_ptr<net::MappedHostResolver> host_resolver_; |
| 76 std::unique_ptr<net::TestURLRequestContext> request_context_; |
| 77 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(BidirectionalStreamTestURLRequestContextGetter); |
| 80 }; |
| 81 |
| 82 } // namespace |
| 83 |
| 84 stream_engine* GetTestStreamEngine() { |
| 85 static scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 86 static stream_engine engine; |
| 87 if (request_context_getter_.get() == nullptr) { |
| 88 static base::Thread* test_io_thread_ = new base::Thread( |
| 89 "grpc_support_test_io_thread"); |
| 90 base::Thread::Options options; |
| 91 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 92 DCHECK(test_io_thread_->StartWithOptions(options)); |
| 93 |
| 94 request_context_getter_ = |
| 95 new BidirectionalStreamTestURLRequestContextGetter( |
| 96 test_io_thread_->task_runner()); |
| 97 engine.obj = request_context_getter_.get(); |
| 98 } |
| 99 return &engine; |
| 100 } |
| 101 |
| 102 } // namespace grpc_support |
| OLD | NEW |