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
, const std::string data
) {
42 pickle
.WriteInt(flags
| 1); // Version 1.
45 pickle
.WriteString(data
);
47 scoped_refptr
<WrappedIOBuffer
> buf(new WrappedIOBuffer(
48 reinterpret_cast<const char*>(pickle
.data())));
49 int len
= static_cast<int>(pickle
.size());
51 net::TestCompletionCallback cb
;
52 int rv
= entry
->WriteData(0, 0, buf
, len
, cb
.callback(), true);
53 ASSERT_EQ(len
, cb
.GetResult(rv
));
56 void WriteData(disk_cache::Entry
* entry
, int index
, const std::string data
) {
60 int len
= data
.length();
61 scoped_refptr
<IOBuffer
> buf(new IOBuffer(len
));
62 memcpy(buf
->data(), data
.data(), data
.length());
64 net::TestCompletionCallback cb
;
65 int rv
= entry
->WriteData(index
, 0, buf
, len
, cb
.callback(), true);
66 ASSERT_EQ(len
, cb
.GetResult(rv
));
69 void WriteToEntry(disk_cache::Backend
* cache
, const std::string key
,
70 const std::string data0
, const std::string data1
,
71 const std::string data2
) {
72 net::TestCompletionCallback cb
;
73 disk_cache::Entry
* entry
;
74 int rv
= cache
->CreateEntry(key
, &entry
, cb
.callback());
75 rv
= cb
.GetResult(rv
);
77 rv
= cache
->OpenEntry(key
, &entry
, cb
.callback());
78 ASSERT_EQ(OK
, cb
.GetResult(rv
));
81 WriteHeaders(entry
, 0, data0
);
82 WriteData(entry
, 1, data1
);
83 WriteData(entry
, 2, data2
);
88 void FillCache(URLRequestContext
* context
) {
89 net::TestCompletionCallback cb
;
90 disk_cache::Backend
* cache
;
92 context
->http_transaction_factory()->GetCache()->GetBackend(
93 &cache
, cb
.callback());
94 ASSERT_EQ(OK
, cb
.GetResult(rv
));
97 WriteToEntry(cache
, "first", "some", empty
, empty
);
98 WriteToEntry(cache
, "second", "only hex_dumped", "same", "kind");
99 WriteToEntry(cache
, "third", empty
, "another", "thing");
104 TEST(ViewCacheHelper
, EmptyCache
) {
105 TestURLRequestContext context
;
106 ViewCacheHelper helper
;
108 TestCompletionCallback cb
;
109 std::string prefix
, data
;
110 int rv
= helper
.GetContentsHTML(&context
, prefix
, &data
, cb
.callback());
111 EXPECT_EQ(OK
, cb
.GetResult(rv
));
112 EXPECT_FALSE(data
.empty());
115 TEST(ViewCacheHelper
, ListContents
) {
116 TestURLRequestContext context
;
117 ViewCacheHelper helper
;
121 std::string prefix
, data
;
122 TestCompletionCallback cb
;
123 int rv
= helper
.GetContentsHTML(&context
, prefix
, &data
, cb
.callback());
124 EXPECT_EQ(OK
, cb
.GetResult(rv
));
126 EXPECT_EQ(0U, data
.find("<html>"));
127 EXPECT_NE(std::string::npos
, data
.find("</html>"));
128 EXPECT_NE(std::string::npos
, data
.find("first"));
129 EXPECT_NE(std::string::npos
, data
.find("second"));
130 EXPECT_NE(std::string::npos
, data
.find("third"));
132 EXPECT_EQ(std::string::npos
, data
.find("some"));
133 EXPECT_EQ(std::string::npos
, data
.find("same"));
134 EXPECT_EQ(std::string::npos
, data
.find("thing"));
137 TEST(ViewCacheHelper
, DumpEntry
) {
138 TestURLRequestContext context
;
139 ViewCacheHelper helper
;
144 TestCompletionCallback cb
;
145 int rv
= helper
.GetEntryInfoHTML("second", &context
, &data
, cb
.callback());
146 EXPECT_EQ(OK
, cb
.GetResult(rv
));
148 EXPECT_EQ(0U, data
.find("<html>"));
149 EXPECT_NE(std::string::npos
, data
.find("</html>"));
151 EXPECT_NE(std::string::npos
, data
.find("hex_dumped"));
152 EXPECT_NE(std::string::npos
, data
.find("same"));
153 EXPECT_NE(std::string::npos
, data
.find("kind"));
155 EXPECT_EQ(std::string::npos
, data
.find("first"));
156 EXPECT_EQ(std::string::npos
, data
.find("third"));
157 EXPECT_EQ(std::string::npos
, data
.find("some"));
158 EXPECT_EQ(std::string::npos
, data
.find("another"));
161 // Makes sure the links are correct.
162 TEST(ViewCacheHelper
, Prefix
) {
163 TestURLRequestContext context
;
164 ViewCacheHelper helper
;
168 std::string key
, data
;
169 std::string
prefix("prefix:");
170 TestCompletionCallback cb
;
171 int rv
= helper
.GetContentsHTML(&context
, prefix
, &data
, cb
.callback());
172 EXPECT_EQ(OK
, cb
.GetResult(rv
));
174 EXPECT_EQ(0U, data
.find("<html>"));
175 EXPECT_NE(std::string::npos
, data
.find("</html>"));
176 EXPECT_NE(std::string::npos
, data
.find("<a href=\"prefix:first\">"));
177 EXPECT_NE(std::string::npos
, data
.find("<a href=\"prefix:second\">"));
178 EXPECT_NE(std::string::npos
, data
.find("<a href=\"prefix:third\">"));
181 TEST(ViewCacheHelper
, TruncatedFlag
) {
182 TestURLRequestContext context
;
183 ViewCacheHelper helper
;
185 net::TestCompletionCallback cb
;
186 disk_cache::Backend
* cache
;
188 context
.http_transaction_factory()->GetCache()->GetBackend(
189 &cache
, cb
.callback());
190 ASSERT_EQ(OK
, cb
.GetResult(rv
));
192 std::string
key("the key");
193 disk_cache::Entry
* entry
;
194 rv
= cache
->CreateEntry(key
, &entry
, cb
.callback());
195 ASSERT_EQ(OK
, cb
.GetResult(rv
));
197 // RESPONSE_INFO_TRUNCATED defined on response_info.cc
199 WriteHeaders(entry
, flags
, "something");
203 TestCompletionCallback cb1
;
204 rv
= helper
.GetEntryInfoHTML(key
, &context
, &data
, cb1
.callback());
205 EXPECT_EQ(OK
, cb1
.GetResult(rv
));
207 EXPECT_NE(std::string::npos
, data
.find("RESPONSE_INFO_TRUNCATED"));