1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/url_request/url_request_file_dir_job.h"
#include <string>
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "net/base/filename_util.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
const int kBufferSize = 4096;
class TestJobFactory : public URLRequestJobFactory {
public:
explicit TestJobFactory(const base::FilePath& path) : path_(path) {}
~TestJobFactory() override {}
URLRequestJob* MaybeCreateJobWithProtocolHandler(
const std::string& scheme,
URLRequest* request,
NetworkDelegate* network_delegate) const override {
URLRequestJob* job = new URLRequestFileDirJob(
request, network_delegate, path_, base::ThreadTaskRunnerHandle::Get());
return job;
}
URLRequestJob* MaybeInterceptRedirect(URLRequest* request,
NetworkDelegate* network_delegate,
const GURL& location) const override {
return nullptr;
}
URLRequestJob* MaybeInterceptResponse(
URLRequest* request,
NetworkDelegate* network_delegate) const override {
return nullptr;
}
bool IsHandledProtocol(const std::string& scheme) const override {
return scheme == "file";
}
bool IsHandledURL(const GURL& url) const override {
return IsHandledProtocol(url.scheme());
}
bool IsSafeRedirectTarget(const GURL& location) const override {
return false;
}
private:
const base::FilePath path_;
DISALLOW_COPY_AND_ASSIGN(TestJobFactory);
};
class TestDirectoryURLRequestDelegate : public TestDelegate {
public:
TestDirectoryURLRequestDelegate() {}
~TestDirectoryURLRequestDelegate() override {}
void OnResponseStarted(URLRequest* request) override {
got_response_started_ = true;
}
bool got_response_started() const { return got_response_started_; }
private:
bool got_response_started_ = false;
DISALLOW_COPY_AND_ASSIGN(TestDirectoryURLRequestDelegate);
};
class URLRequestFileDirTest : public testing::Test {
public:
URLRequestFileDirTest() : buffer_(new IOBuffer(kBufferSize)) {}
protected:
TestURLRequestContext context_;
TestDirectoryURLRequestDelegate delegate_;
scoped_refptr<IOBuffer> buffer_;
};
TEST_F(URLRequestFileDirTest, ListCompletionOnNoPending) {
base::ScopedTempDir directory;
// It is necessary to pass an existing directory to UrlRequest object,
// but it will be deleted for testing purpose after request is started.
ASSERT_TRUE(directory.CreateUniqueTempDir());
TestJobFactory factory(directory.path());
context_.set_job_factory(&factory);
scoped_ptr<URLRequest> request(context_.CreateRequest(
FilePathToFileURL(
directory.path().AppendASCII("this_path_does_not_exist")),
DEFAULT_PRIORITY, &delegate_));
request->Start();
ASSERT_TRUE(directory.Delete());
// Since the DirectoryLister is running on the network thread, this
// will spin the message loop until the read error is returned to the
// URLRequestFileDirJob.
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(delegate_.got_response_started());
int bytes_read = 0;
EXPECT_FALSE(request->Read(buffer_.get(), kBufferSize, &bytes_read));
// The URLRequestFileDirJobShould return the cached read error synchronously.
// If it's not returned synchronously, the code path this is intended to test
// was not executed.
EXPECT_EQ(ERR_FILE_NOT_FOUND, request->status().ToNetError());
}
} // namespace
} // namespace net
|