1 // Copyright 2015 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.
5 #include "net/test/url_request/ssl_certificate_error_job.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "net/ssl/ssl_info.h"
13 #include "net/url_request/url_request.h"
14 #include "net/url_request/url_request_filter.h"
15 #include "net/url_request/url_request_interceptor.h"
21 const char kMockHostname
[] = "mock.ssl.cert.error.request";
23 class MockJobInterceptor
: public URLRequestInterceptor
{
25 MockJobInterceptor() {}
26 ~MockJobInterceptor() override
{}
28 // URLRequestJobFactory::ProtocolHandler implementation:
29 URLRequestJob
* MaybeInterceptRequest(
31 NetworkDelegate
* network_delegate
) const override
{
32 return new SSLCertificateErrorJob(request
, network_delegate
);
36 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor
);
41 SSLCertificateErrorJob::SSLCertificateErrorJob(
43 NetworkDelegate
* network_delegate
)
44 : URLRequestJob(request
, network_delegate
), weak_factory_(this) {
47 void SSLCertificateErrorJob::Start() {
48 base::ThreadTaskRunnerHandle::Get()->PostTask(
49 FROM_HERE
, base::Bind(&SSLCertificateErrorJob::NotifyError
,
50 weak_factory_
.GetWeakPtr()));
53 void SSLCertificateErrorJob::AddUrlHandler() {
54 URLRequestFilter
* filter
= URLRequestFilter::GetInstance();
55 filter
->AddHostnameInterceptor(
56 "https", kMockHostname
,
57 scoped_ptr
<URLRequestInterceptor
>(new MockJobInterceptor()));
60 GURL
SSLCertificateErrorJob::GetMockUrl() {
61 return GURL(base::StringPrintf("https://%s", kMockHostname
));
64 SSLCertificateErrorJob::~SSLCertificateErrorJob() {
67 void SSLCertificateErrorJob::NotifyError() {
69 info
.cert_status
= CERT_STATUS_DATE_INVALID
;
70 NotifySSLCertificateError(info
, true);