1 // Copyright (c) 2011 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();
23 // Gets a pointer to the cache backend.
24 disk_cache::Backend
* GetBackend();
30 TestURLRequestContext::TestURLRequestContext()
31 : cache_(reinterpret_cast<HttpTransactionFactory
*>(NULL
), NULL
,
32 HttpCache::DefaultBackend::InMemory(0)) {
33 set_http_transaction_factory(&cache_
);
36 void WriteHeaders(disk_cache::Entry
* entry
, int flags
, const std::string data
) {
41 pickle
.WriteInt(flags
| 1); // Version 1.
44 pickle
.WriteString(data
);
46 scoped_refptr
<WrappedIOBuffer
> buf(new WrappedIOBuffer(
47 reinterpret_cast<const char*>(pickle
.data())));
48 int len
= static_cast<int>(pickle
.size());
50 TestCompletionCallback cb
;
51 int rv
= entry
->WriteData(0, 0, buf
, len
, &cb
, true);
52 ASSERT_EQ(len
, cb
.GetResult(rv
));
55 void WriteData(disk_cache::Entry
* entry
, int index
, const std::string data
) {
59 int len
= data
.length();
60 scoped_refptr
<IOBuffer
> buf(new IOBuffer(len
));
61 memcpy(buf
->data(), data
.data(), data
.length());
63 TestCompletionCallback cb
;
64 int rv
= entry
->WriteData(index
, 0, buf
, len
, &cb
, true);
65 ASSERT_EQ(len
, cb
.GetResult(rv
));
68 void WriteToEntry(disk_cache::Backend
* cache
, const std::string key
,
69 const std::string data0
, const std::string data1
,
70 const std::string data2
) {
71 TestCompletionCallback cb
;
72 disk_cache::Entry
* entry
;
73 int rv
= cache
->CreateEntry(key
, &entry
, &cb
);
74 rv
= cb
.GetResult(rv
);
76 rv
= cache
->OpenEntry(key
, &entry
, &cb
);
77 ASSERT_EQ(OK
, cb
.GetResult(rv
));
80 WriteHeaders(entry
, 0, data0
);
81 WriteData(entry
, 1, data1
);
82 WriteData(entry
, 2, data2
);
87 void FillCache(URLRequestContext
* context
) {
88 TestCompletionCallback cb
;
89 disk_cache::Backend
* cache
;
91 context
->http_transaction_factory()->GetCache()->GetBackend(&cache
, &cb
);
92 ASSERT_EQ(OK
, cb
.GetResult(rv
));
95 WriteToEntry(cache
, "first", "some", empty
, empty
);
96 WriteToEntry(cache
, "second", "only hex_dumped", "same", "kind");
97 WriteToEntry(cache
, "third", empty
, "another", "thing");
102 TEST(ViewCacheHelper
, EmptyCache
) {
103 scoped_refptr
<TestURLRequestContext
> context(new TestURLRequestContext());
104 ViewCacheHelper helper
;
106 TestCompletionCallback cb
;
107 std::string prefix
, data
;
108 int rv
= helper
.GetContentsHTML(context
, prefix
, &data
, &cb
);
109 EXPECT_EQ(OK
, cb
.GetResult(rv
));
110 EXPECT_FALSE(data
.empty());
113 TEST(ViewCacheHelper
, ListContents
) {
114 scoped_refptr
<TestURLRequestContext
> context(new TestURLRequestContext());
115 ViewCacheHelper helper
;
119 std::string prefix
, data
;
120 TestCompletionCallback cb
;
121 int rv
= helper
.GetContentsHTML(context
, prefix
, &data
, &cb
);
122 EXPECT_EQ(OK
, cb
.GetResult(rv
));
124 EXPECT_EQ(0U, data
.find("<html>"));
125 EXPECT_NE(std::string::npos
, data
.find("</html>"));
126 EXPECT_NE(std::string::npos
, data
.find("first"));
127 EXPECT_NE(std::string::npos
, data
.find("second"));
128 EXPECT_NE(std::string::npos
, data
.find("third"));
130 EXPECT_EQ(std::string::npos
, data
.find("some"));
131 EXPECT_EQ(std::string::npos
, data
.find("same"));
132 EXPECT_EQ(std::string::npos
, data
.find("thing"));
135 TEST(ViewCacheHelper
, DumpEntry
) {
136 scoped_refptr
<TestURLRequestContext
> context(new TestURLRequestContext());
137 ViewCacheHelper helper
;
142 TestCompletionCallback cb
;
143 int rv
= helper
.GetEntryInfoHTML("second", context
, &data
, &cb
);
144 EXPECT_EQ(OK
, cb
.GetResult(rv
));
146 EXPECT_EQ(0U, data
.find("<html>"));
147 EXPECT_NE(std::string::npos
, data
.find("</html>"));
149 EXPECT_NE(std::string::npos
, data
.find("hex_dumped"));
150 EXPECT_NE(std::string::npos
, data
.find("same"));
151 EXPECT_NE(std::string::npos
, data
.find("kind"));
153 EXPECT_EQ(std::string::npos
, data
.find("first"));
154 EXPECT_EQ(std::string::npos
, data
.find("third"));
155 EXPECT_EQ(std::string::npos
, data
.find("some"));
156 EXPECT_EQ(std::string::npos
, data
.find("another"));
159 // Makes sure the links are correct.
160 TEST(ViewCacheHelper
, Prefix
) {
161 scoped_refptr
<TestURLRequestContext
> context(new TestURLRequestContext());
162 ViewCacheHelper helper
;
166 std::string key
, data
;
167 std::string
prefix("prefix:");
168 TestCompletionCallback cb
;
169 int rv
= helper
.GetContentsHTML(context
, prefix
, &data
, &cb
);
170 EXPECT_EQ(OK
, cb
.GetResult(rv
));
172 EXPECT_EQ(0U, data
.find("<html>"));
173 EXPECT_NE(std::string::npos
, data
.find("</html>"));
174 EXPECT_NE(std::string::npos
, data
.find("<a href=\"prefix:first\">"));
175 EXPECT_NE(std::string::npos
, data
.find("<a href=\"prefix:second\">"));
176 EXPECT_NE(std::string::npos
, data
.find("<a href=\"prefix:third\">"));
179 TEST(ViewCacheHelper
, TruncatedFlag
) {
180 scoped_refptr
<TestURLRequestContext
> context(new TestURLRequestContext());
181 ViewCacheHelper helper
;
183 TestCompletionCallback cb
;
184 disk_cache::Backend
* cache
;
186 context
->http_transaction_factory()->GetCache()->GetBackend(&cache
, &cb
);
187 ASSERT_EQ(OK
, cb
.GetResult(rv
));
189 std::string
key("the key");
190 disk_cache::Entry
* entry
;
191 rv
= cache
->CreateEntry(key
, &entry
, &cb
);
192 ASSERT_EQ(OK
, cb
.GetResult(rv
));
194 // RESPONSE_INFO_TRUNCATED defined on response_info.cc
196 WriteHeaders(entry
, flags
, "something");
200 rv
= helper
.GetEntryInfoHTML(key
, context
, &data
, &cb
);
201 EXPECT_EQ(OK
, cb
.GetResult(rv
));
203 EXPECT_NE(std::string::npos
, data
.find("RESPONSE_INFO_TRUNCATED"));