1 // Copyright (c) 2012 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/view_cache_helper.h"
7 #include "base/pickle.h"
8 #include "net/base/net_errors.h"
9 #include "net/base/test_completion_callback.h"
10 #include "net/disk_cache/disk_cache.h"
11 #include "net/http/http_cache.h"
12 #include "net/url_request/url_request_context.h"
13 #include "testing/gtest/include/gtest/gtest.h"
19 class TestURLRequestContext
: public URLRequestContext
{
21 TestURLRequestContext();
22 virtual ~TestURLRequestContext() {}
24 // Gets a pointer to the cache backend.
25 disk_cache::Backend
* GetBackend();
31 TestURLRequestContext::TestURLRequestContext()
32 : cache_(reinterpret_cast<HttpTransactionFactory
*>(NULL
), NULL
,
33 HttpCache::DefaultBackend::InMemory(0)) {
34 set_http_transaction_factory(&cache_
);
37 void WriteHeaders(disk_cache::Entry
* entry
, int flags
,
38 const std::string
& data
) {
43 pickle
.WriteInt(flags
| 1); // Version 1.
46 pickle
.WriteString(data
);
48 scoped_refptr
<WrappedIOBuffer
> buf(new WrappedIOBuffer(
49 reinterpret_cast<const char*>(pickle
.data())));
50 int len
= static_cast<int>(pickle
.size());
52 net::TestCompletionCallback cb
;
53 int rv
= entry
->WriteData(0, 0, buf
.get(), len
, cb
.callback(), true);
54 ASSERT_EQ(len
, cb
.GetResult(rv
));
57 void WriteData(disk_cache::Entry
* entry
, int index
, const std::string
& data
) {
61 int len
= data
.length();
62 scoped_refptr
<IOBuffer
> buf(new IOBuffer(len
));
63 memcpy(buf
->data(), data
.data(), data
.length());
65 net::TestCompletionCallback cb
;
66 int rv
= entry
->WriteData(index
, 0, buf
.get(), len
, cb
.callback(), true);
67 ASSERT_EQ(len
, cb
.GetResult(rv
));
70 void WriteToEntry(disk_cache::Backend
* cache
, const std::string
& key
,
71 const std::string
& data0
, const std::string
& data1
,
72 const std::string
& data2
) {
73 net::TestCompletionCallback cb
;
74 disk_cache::Entry
* entry
;
75 int rv
= cache
->CreateEntry(key
, &entry
, cb
.callback());
76 rv
= cb
.GetResult(rv
);
78 rv
= cache
->OpenEntry(key
, &entry
, cb
.callback());
79 ASSERT_EQ(OK
, cb
.GetResult(rv
));
82 WriteHeaders(entry
, 0, data0
);
83 WriteData(entry
, 1, data1
);
84 WriteData(entry
, 2, data2
);
89 void FillCache(URLRequestContext
* context
) {
90 net::TestCompletionCallback cb
;
91 disk_cache::Backend
* cache
;
93 context
->http_transaction_factory()->GetCache()->GetBackend(
94 &cache
, cb
.callback());
95 ASSERT_EQ(OK
, cb
.GetResult(rv
));
98 WriteToEntry(cache
, "first", "some", empty
, empty
);
99 WriteToEntry(cache
, "second", "only hex_dumped", "same", "kind");
100 WriteToEntry(cache
, "third", empty
, "another", "thing");
105 TEST(ViewCacheHelper
, EmptyCache
) {
106 TestURLRequestContext context
;
107 ViewCacheHelper helper
;
109 TestCompletionCallback cb
;
110 std::string prefix
, data
;
111 int rv
= helper
.GetContentsHTML(&context
, prefix
, &data
, cb
.callback());
112 EXPECT_EQ(OK
, cb
.GetResult(rv
));
113 EXPECT_FALSE(data
.empty());
116 TEST(ViewCacheHelper
, ListContents
) {
117 TestURLRequestContext context
;
118 ViewCacheHelper helper
;
122 std::string prefix
, data
;
123 TestCompletionCallback cb
;
124 int rv
= helper
.GetContentsHTML(&context
, prefix
, &data
, cb
.callback());
125 EXPECT_EQ(OK
, cb
.GetResult(rv
));
127 EXPECT_EQ(0U, data
.find("<html>"));
128 EXPECT_NE(std::string::npos
, data
.find("</html>"));
129 EXPECT_NE(std::string::npos
, data
.find("first"));
130 EXPECT_NE(std::string::npos
, data
.find("second"));
131 EXPECT_NE(std::string::npos
, data
.find("third"));
133 EXPECT_EQ(std::string::npos
, data
.find("some"));
134 EXPECT_EQ(std::string::npos
, data
.find("same"));
135 EXPECT_EQ(std::string::npos
, data
.find("thing"));
138 TEST(ViewCacheHelper
, DumpEntry
) {
139 TestURLRequestContext context
;
140 ViewCacheHelper helper
;
145 TestCompletionCallback cb
;
146 int rv
= helper
.GetEntryInfoHTML("second", &context
, &data
, cb
.callback());
147 EXPECT_EQ(OK
, cb
.GetResult(rv
));
149 EXPECT_EQ(0U, data
.find("<html>"));
150 EXPECT_NE(std::string::npos
, data
.find("</html>"));
152 EXPECT_NE(std::string::npos
, data
.find("hex_dumped"));
153 EXPECT_NE(std::string::npos
, data
.find("same"));
154 EXPECT_NE(std::string::npos
, data
.find("kind"));
156 EXPECT_EQ(std::string::npos
, data
.find("first"));
157 EXPECT_EQ(std::string::npos
, data
.find("third"));
158 EXPECT_EQ(std::string::npos
, data
.find("some"));
159 EXPECT_EQ(std::string::npos
, data
.find("another"));
162 // Makes sure the links are correct.
163 TEST(ViewCacheHelper
, Prefix
) {
164 TestURLRequestContext context
;
165 ViewCacheHelper helper
;
169 std::string key
, data
;
170 std::string
prefix("prefix:");
171 TestCompletionCallback cb
;
172 int rv
= helper
.GetContentsHTML(&context
, prefix
, &data
, cb
.callback());
173 EXPECT_EQ(OK
, cb
.GetResult(rv
));
175 EXPECT_EQ(0U, data
.find("<html>"));
176 EXPECT_NE(std::string::npos
, data
.find("</html>"));
177 EXPECT_NE(std::string::npos
, data
.find("<a href=\"prefix:first\">"));
178 EXPECT_NE(std::string::npos
, data
.find("<a href=\"prefix:second\">"));
179 EXPECT_NE(std::string::npos
, data
.find("<a href=\"prefix:third\">"));
182 TEST(ViewCacheHelper
, TruncatedFlag
) {
183 TestURLRequestContext context
;
184 ViewCacheHelper helper
;
186 net::TestCompletionCallback cb
;
187 disk_cache::Backend
* cache
;
189 context
.http_transaction_factory()->GetCache()->GetBackend(
190 &cache
, cb
.callback());
191 ASSERT_EQ(OK
, cb
.GetResult(rv
));
193 std::string
key("the key");
194 disk_cache::Entry
* entry
;
195 rv
= cache
->CreateEntry(key
, &entry
, cb
.callback());
196 ASSERT_EQ(OK
, cb
.GetResult(rv
));
198 // RESPONSE_INFO_TRUNCATED defined on response_info.cc
200 WriteHeaders(entry
, flags
, "something");
204 TestCompletionCallback cb1
;
205 rv
= helper
.GetEntryInfoHTML(key
, &context
, &data
, cb1
.callback());
206 EXPECT_EQ(OK
, cb1
.GetResult(rv
));
208 EXPECT_NE(std::string::npos
, data
.find("RESPONSE_INFO_TRUNCATED"));