1 // Copyright 2014 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/url_request/url_fetcher_response_writer.h"
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/run_loop.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h"
13 #include "net/base/test_completion_callback.h"
14 #include "testing/platform_test.h"
20 const char kData
[] = "Hello!";
24 class URLFetcherStringWriterTest
: public PlatformTest
{
26 void SetUp() override
{
27 writer_
.reset(new URLFetcherStringWriter
);
28 buf_
= new StringIOBuffer(kData
);
31 scoped_ptr
<URLFetcherStringWriter
> writer_
;
32 scoped_refptr
<StringIOBuffer
> buf_
;
35 TEST_F(URLFetcherStringWriterTest
, Basic
) {
37 // Initialize(), Write() and Finish().
38 TestCompletionCallback callback
;
39 rv
= writer_
->Initialize(callback
.callback());
40 EXPECT_EQ(OK
, callback
.GetResult(rv
));
41 rv
= writer_
->Write(buf_
.get(), buf_
->size(), callback
.callback());
42 EXPECT_EQ(buf_
->size(), callback
.GetResult(rv
));
43 rv
= writer_
->Finish(callback
.callback());
44 EXPECT_EQ(OK
, callback
.GetResult(rv
));
47 EXPECT_EQ(kData
, writer_
->data());
49 // Initialize() again to reset.
50 rv
= writer_
->Initialize(callback
.callback());
51 EXPECT_EQ(OK
, callback
.GetResult(rv
));
52 EXPECT_TRUE(writer_
->data().empty());
55 class URLFetcherFileWriterTest
: public PlatformTest
{
57 void SetUp() override
{
58 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
59 file_path_
= temp_dir_
.path().AppendASCII("test.txt");
60 writer_
.reset(new URLFetcherFileWriter(base::ThreadTaskRunnerHandle::Get(),
62 buf_
= new StringIOBuffer(kData
);
65 base::ScopedTempDir temp_dir_
;
66 base::FilePath file_path_
;
67 scoped_ptr
<URLFetcherFileWriter
> writer_
;
68 scoped_refptr
<StringIOBuffer
> buf_
;
71 TEST_F(URLFetcherFileWriterTest
, WriteToFile
) {
73 // Initialize(), Write() and Finish().
74 TestCompletionCallback callback
;
75 rv
= writer_
->Initialize(callback
.callback());
76 EXPECT_EQ(OK
, callback
.GetResult(rv
));
77 rv
= writer_
->Write(buf_
.get(), buf_
->size(), callback
.callback());
78 EXPECT_EQ(buf_
->size(), callback
.GetResult(rv
));
79 rv
= writer_
->Finish(callback
.callback());
80 EXPECT_EQ(OK
, callback
.GetResult(rv
));
83 EXPECT_EQ(file_path_
.value(), writer_
->file_path().value());
84 std::string file_contents
;
85 EXPECT_TRUE(base::ReadFileToString(writer_
->file_path(), &file_contents
));
86 EXPECT_EQ(kData
, file_contents
);
88 // Destroy the writer. File should be deleted.
90 base::RunLoop().RunUntilIdle();
91 EXPECT_FALSE(base::PathExists(file_path_
));
94 TEST_F(URLFetcherFileWriterTest
, InitializeAgain
) {
96 // Initialize(), Write() and Finish().
97 TestCompletionCallback callback
;
98 rv
= writer_
->Initialize(callback
.callback());
99 EXPECT_EQ(OK
, callback
.GetResult(rv
));
100 rv
= writer_
->Write(buf_
.get(), buf_
->size(), callback
.callback());
101 EXPECT_EQ(buf_
->size(), callback
.GetResult(rv
));
102 rv
= writer_
->Finish(callback
.callback());
103 EXPECT_EQ(OK
, callback
.GetResult(rv
));
105 // Verify the result.
106 std::string file_contents
;
107 EXPECT_TRUE(base::ReadFileToString(writer_
->file_path(), &file_contents
));
108 EXPECT_EQ(kData
, file_contents
);
110 // Initialize() again to reset. Write different data.
111 const std::string data2
= "Bye!";
112 scoped_refptr
<StringIOBuffer
> buf2(new StringIOBuffer(data2
));
114 rv
= writer_
->Initialize(callback
.callback());
115 EXPECT_EQ(OK
, callback
.GetResult(rv
));
116 rv
= writer_
->Write(buf2
.get(), buf2
->size(), callback
.callback());
117 EXPECT_EQ(buf2
->size(), callback
.GetResult(rv
));
118 rv
= writer_
->Finish(callback
.callback());
119 EXPECT_EQ(OK
, callback
.GetResult(rv
));
121 // Verify the result.
122 file_contents
.clear();
123 EXPECT_TRUE(base::ReadFileToString(writer_
->file_path(), &file_contents
));
124 EXPECT_EQ(data2
, file_contents
);
127 TEST_F(URLFetcherFileWriterTest
, DisownFile
) {
129 // Initialize() and Finish() to create a file.
130 TestCompletionCallback callback
;
131 rv
= writer_
->Initialize(callback
.callback());
132 EXPECT_EQ(OK
, callback
.GetResult(rv
));
133 rv
= writer_
->Finish(callback
.callback());
134 EXPECT_EQ(OK
, callback
.GetResult(rv
));
137 writer_
->DisownFile();
139 // File is not deleted even after the writer gets destroyed.
141 base::RunLoop().RunUntilIdle();
142 EXPECT_TRUE(base::PathExists(file_path_
));
145 class URLFetcherFileWriterTemporaryFileTest
: public PlatformTest
{
147 void SetUp() override
{
148 writer_
.reset(new URLFetcherFileWriter(base::ThreadTaskRunnerHandle::Get(),
150 buf_
= new StringIOBuffer(kData
);
153 scoped_ptr
<URLFetcherFileWriter
> writer_
;
154 scoped_refptr
<StringIOBuffer
> buf_
;
157 TEST_F(URLFetcherFileWriterTemporaryFileTest
, WriteToTemporaryFile
) {
159 // Initialize(), Write() and Finish().
160 TestCompletionCallback callback
;
161 rv
= writer_
->Initialize(callback
.callback());
162 EXPECT_EQ(OK
, callback
.GetResult(rv
));
163 rv
= writer_
->Write(buf_
.get(), buf_
->size(), callback
.callback());
164 EXPECT_EQ(buf_
->size(), callback
.GetResult(rv
));
165 rv
= writer_
->Finish(callback
.callback());
166 EXPECT_EQ(OK
, callback
.GetResult(rv
));
168 // Verify the result.
169 std::string file_contents
;
170 EXPECT_TRUE(base::ReadFileToString(writer_
->file_path(), &file_contents
));
171 EXPECT_EQ(kData
, file_contents
);
173 // Destroy the writer. File should be deleted.
174 const base::FilePath file_path
= writer_
->file_path();
176 base::RunLoop().RunUntilIdle();
177 EXPECT_FALSE(base::PathExists(file_path
));