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/http/http_transaction_test_util.h"
13 #include "net/url_request/url_request_context.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 class TestURLRequestContext
: public URLRequestContext
{
22 TestURLRequestContext();
24 virtual ~TestURLRequestContext() {
25 AssertNoURLRequests();
28 // Gets a pointer to the cache backend.
29 disk_cache::Backend
* GetBackend();
35 TestURLRequestContext::TestURLRequestContext()
36 : cache_(new MockNetworkLayer(), NULL
,
37 HttpCache::DefaultBackend::InMemory(0)) {
38 set_http_transaction_factory(&cache_
);
41 void WriteHeaders(disk_cache::Entry
* entry
, int flags
,
42 const std::string
& data
) {
47 pickle
.WriteInt(flags
| 1); // Version 1.
50 pickle
.WriteString(data
);
52 scoped_refptr
<WrappedIOBuffer
> buf(new WrappedIOBuffer(
53 reinterpret_cast<const char*>(pickle
.data())));
54 int len
= static_cast<int>(pickle
.size());
56 net::TestCompletionCallback cb
;
57 int rv
= entry
->WriteData(0, 0, buf
.get(), len
, cb
.callback(), true);
58 ASSERT_EQ(len
, cb
.GetResult(rv
));
61 void WriteData(disk_cache::Entry
* entry
, int index
, const std::string
& data
) {
65 int len
= data
.length();
66 scoped_refptr
<IOBuffer
> buf(new IOBuffer(len
));
67 memcpy(buf
->data(), data
.data(), data
.length());
69 net::TestCompletionCallback cb
;
70 int rv
= entry
->WriteData(index
, 0, buf
.get(), len
, cb
.callback(), true);
71 ASSERT_EQ(len
, cb
.GetResult(rv
));
74 void WriteToEntry(disk_cache::Backend
* cache
, const std::string
& key
,
75 const std::string
& data0
, const std::string
& data1
,
76 const std::string
& data2
) {
77 net::TestCompletionCallback cb
;
78 disk_cache::Entry
* entry
;
79 int rv
= cache
->CreateEntry(key
, &entry
, cb
.callback());
80 rv
= cb
.GetResult(rv
);
82 rv
= cache
->OpenEntry(key
, &entry
, cb
.callback());
83 ASSERT_EQ(OK
, cb
.GetResult(rv
));
86 WriteHeaders(entry
, 0, data0
);
87 WriteData(entry
, 1, data1
);
88 WriteData(entry
, 2, data2
);
93 void FillCache(URLRequestContext
* context
) {
94 net::TestCompletionCallback cb
;
95 disk_cache::Backend
* cache
;
97 context
->http_transaction_factory()->GetCache()->GetBackend(
98 &cache
, cb
.callback());
99 ASSERT_EQ(OK
, cb
.GetResult(rv
));
102 WriteToEntry(cache
, "first", "some", empty
, empty
);
103 WriteToEntry(cache
, "second", "only hex_dumped", "same", "kind");
104 WriteToEntry(cache
, "third", empty
, "another", "thing");
109 TEST(ViewCacheHelper
, EmptyCache
) {
110 TestURLRequestContext context
;
111 ViewCacheHelper helper
;
113 TestCompletionCallback cb
;
114 std::string prefix
, data
;
115 int rv
= helper
.GetContentsHTML(&context
, prefix
, &data
, cb
.callback());
116 EXPECT_EQ(OK
, cb
.GetResult(rv
));
117 EXPECT_FALSE(data
.empty());
120 TEST(ViewCacheHelper
, ListContents
) {
121 TestURLRequestContext context
;
122 ViewCacheHelper helper
;
126 std::string prefix
, data
;
127 TestCompletionCallback cb
;
128 int rv
= helper
.GetContentsHTML(&context
, prefix
, &data
, cb
.callback());
129 EXPECT_EQ(OK
, cb
.GetResult(rv
));
131 EXPECT_EQ(0U, data
.find("<html>"));
132 EXPECT_NE(std::string::npos
, data
.find("</html>"));
133 EXPECT_NE(std::string::npos
, data
.find("first"));
134 EXPECT_NE(std::string::npos
, data
.find("second"));
135 EXPECT_NE(std::string::npos
, data
.find("third"));
137 EXPECT_EQ(std::string::npos
, data
.find("some"));
138 EXPECT_EQ(std::string::npos
, data
.find("same"));
139 EXPECT_EQ(std::string::npos
, data
.find("thing"));
142 TEST(ViewCacheHelper
, DumpEntry
) {
143 TestURLRequestContext context
;
144 ViewCacheHelper helper
;
149 TestCompletionCallback cb
;
150 int rv
= helper
.GetEntryInfoHTML("second", &context
, &data
, cb
.callback());
151 EXPECT_EQ(OK
, cb
.GetResult(rv
));
153 EXPECT_EQ(0U, data
.find("<html>"));
154 EXPECT_NE(std::string::npos
, data
.find("</html>"));
156 EXPECT_NE(std::string::npos
, data
.find("hex_dumped"));
157 EXPECT_NE(std::string::npos
, data
.find("same"));
158 EXPECT_NE(std::string::npos
, data
.find("kind"));
160 EXPECT_EQ(std::string::npos
, data
.find("first"));
161 EXPECT_EQ(std::string::npos
, data
.find("third"));
162 EXPECT_EQ(std::string::npos
, data
.find("some"));
163 EXPECT_EQ(std::string::npos
, data
.find("another"));
166 // Makes sure the links are correct.
167 TEST(ViewCacheHelper
, Prefix
) {
168 TestURLRequestContext context
;
169 ViewCacheHelper helper
;
173 std::string key
, data
;
174 std::string
prefix("prefix:");
175 TestCompletionCallback cb
;
176 int rv
= helper
.GetContentsHTML(&context
, prefix
, &data
, cb
.callback());
177 EXPECT_EQ(OK
, cb
.GetResult(rv
));
179 EXPECT_EQ(0U, data
.find("<html>"));
180 EXPECT_NE(std::string::npos
, data
.find("</html>"));
181 EXPECT_NE(std::string::npos
, data
.find("<a href=\"prefix:first\">"));
182 EXPECT_NE(std::string::npos
, data
.find("<a href=\"prefix:second\">"));
183 EXPECT_NE(std::string::npos
, data
.find("<a href=\"prefix:third\">"));
186 TEST(ViewCacheHelper
, TruncatedFlag
) {
187 TestURLRequestContext context
;
188 ViewCacheHelper helper
;
190 net::TestCompletionCallback cb
;
191 disk_cache::Backend
* cache
;
193 context
.http_transaction_factory()->GetCache()->GetBackend(
194 &cache
, cb
.callback());
195 ASSERT_EQ(OK
, cb
.GetResult(rv
));
197 std::string
key("the key");
198 disk_cache::Entry
* entry
;
199 rv
= cache
->CreateEntry(key
, &entry
, cb
.callback());
200 ASSERT_EQ(OK
, cb
.GetResult(rv
));
202 // RESPONSE_INFO_TRUNCATED defined on response_info.cc
204 WriteHeaders(entry
, flags
, "something");
208 TestCompletionCallback cb1
;
209 rv
= helper
.GetEntryInfoHTML(key
, &context
, &data
, cb1
.callback());
210 EXPECT_EQ(OK
, cb1
.GetResult(rv
));
212 EXPECT_NE(std::string::npos
, data
.find("RESPONSE_INFO_TRUNCATED"));