Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / url_request / view_cache_helper_unittest.cc
blob70bf201a507ee502dc60266055c2d2139ac693a1
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"
16 namespace net {
18 namespace {
20 class TestURLRequestContext : public URLRequestContext {
21 public:
22 TestURLRequestContext();
23 virtual ~TestURLRequestContext() {}
25 // Gets a pointer to the cache backend.
26 disk_cache::Backend* GetBackend();
28 private:
29 HttpCache cache_;
32 TestURLRequestContext::TestURLRequestContext()
33 : cache_(new MockNetworkLayer(), NULL,
34 HttpCache::DefaultBackend::InMemory(0)) {
35 set_http_transaction_factory(&cache_);
38 void WriteHeaders(disk_cache::Entry* entry, int flags,
39 const std::string& data) {
40 if (data.empty())
41 return;
43 Pickle pickle;
44 pickle.WriteInt(flags | 1); // Version 1.
45 pickle.WriteInt64(0);
46 pickle.WriteInt64(0);
47 pickle.WriteString(data);
49 scoped_refptr<WrappedIOBuffer> buf(new WrappedIOBuffer(
50 reinterpret_cast<const char*>(pickle.data())));
51 int len = static_cast<int>(pickle.size());
53 net::TestCompletionCallback cb;
54 int rv = entry->WriteData(0, 0, buf.get(), len, cb.callback(), true);
55 ASSERT_EQ(len, cb.GetResult(rv));
58 void WriteData(disk_cache::Entry* entry, int index, const std::string& data) {
59 if (data.empty())
60 return;
62 int len = data.length();
63 scoped_refptr<IOBuffer> buf(new IOBuffer(len));
64 memcpy(buf->data(), data.data(), data.length());
66 net::TestCompletionCallback cb;
67 int rv = entry->WriteData(index, 0, buf.get(), len, cb.callback(), true);
68 ASSERT_EQ(len, cb.GetResult(rv));
71 void WriteToEntry(disk_cache::Backend* cache, const std::string& key,
72 const std::string& data0, const std::string& data1,
73 const std::string& data2) {
74 net::TestCompletionCallback cb;
75 disk_cache::Entry* entry;
76 int rv = cache->CreateEntry(key, &entry, cb.callback());
77 rv = cb.GetResult(rv);
78 if (rv != OK) {
79 rv = cache->OpenEntry(key, &entry, cb.callback());
80 ASSERT_EQ(OK, cb.GetResult(rv));
83 WriteHeaders(entry, 0, data0);
84 WriteData(entry, 1, data1);
85 WriteData(entry, 2, data2);
87 entry->Close();
90 void FillCache(URLRequestContext* context) {
91 net::TestCompletionCallback cb;
92 disk_cache::Backend* cache;
93 int rv =
94 context->http_transaction_factory()->GetCache()->GetBackend(
95 &cache, cb.callback());
96 ASSERT_EQ(OK, cb.GetResult(rv));
98 std::string empty;
99 WriteToEntry(cache, "first", "some", empty, empty);
100 WriteToEntry(cache, "second", "only hex_dumped", "same", "kind");
101 WriteToEntry(cache, "third", empty, "another", "thing");
104 } // namespace.
106 TEST(ViewCacheHelper, EmptyCache) {
107 TestURLRequestContext context;
108 ViewCacheHelper helper;
110 TestCompletionCallback cb;
111 std::string prefix, data;
112 int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback());
113 EXPECT_EQ(OK, cb.GetResult(rv));
114 EXPECT_FALSE(data.empty());
117 TEST(ViewCacheHelper, ListContents) {
118 TestURLRequestContext context;
119 ViewCacheHelper helper;
121 FillCache(&context);
123 std::string prefix, data;
124 TestCompletionCallback cb;
125 int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback());
126 EXPECT_EQ(OK, cb.GetResult(rv));
128 EXPECT_EQ(0U, data.find("<html>"));
129 EXPECT_NE(std::string::npos, data.find("</html>"));
130 EXPECT_NE(std::string::npos, data.find("first"));
131 EXPECT_NE(std::string::npos, data.find("second"));
132 EXPECT_NE(std::string::npos, data.find("third"));
134 EXPECT_EQ(std::string::npos, data.find("some"));
135 EXPECT_EQ(std::string::npos, data.find("same"));
136 EXPECT_EQ(std::string::npos, data.find("thing"));
139 TEST(ViewCacheHelper, DumpEntry) {
140 TestURLRequestContext context;
141 ViewCacheHelper helper;
143 FillCache(&context);
145 std::string data;
146 TestCompletionCallback cb;
147 int rv = helper.GetEntryInfoHTML("second", &context, &data, cb.callback());
148 EXPECT_EQ(OK, cb.GetResult(rv));
150 EXPECT_EQ(0U, data.find("<html>"));
151 EXPECT_NE(std::string::npos, data.find("</html>"));
153 EXPECT_NE(std::string::npos, data.find("hex_dumped"));
154 EXPECT_NE(std::string::npos, data.find("same"));
155 EXPECT_NE(std::string::npos, data.find("kind"));
157 EXPECT_EQ(std::string::npos, data.find("first"));
158 EXPECT_EQ(std::string::npos, data.find("third"));
159 EXPECT_EQ(std::string::npos, data.find("some"));
160 EXPECT_EQ(std::string::npos, data.find("another"));
163 // Makes sure the links are correct.
164 TEST(ViewCacheHelper, Prefix) {
165 TestURLRequestContext context;
166 ViewCacheHelper helper;
168 FillCache(&context);
170 std::string key, data;
171 std::string prefix("prefix:");
172 TestCompletionCallback cb;
173 int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback());
174 EXPECT_EQ(OK, cb.GetResult(rv));
176 EXPECT_EQ(0U, data.find("<html>"));
177 EXPECT_NE(std::string::npos, data.find("</html>"));
178 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:first\">"));
179 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:second\">"));
180 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:third\">"));
183 TEST(ViewCacheHelper, TruncatedFlag) {
184 TestURLRequestContext context;
185 ViewCacheHelper helper;
187 net::TestCompletionCallback cb;
188 disk_cache::Backend* cache;
189 int rv =
190 context.http_transaction_factory()->GetCache()->GetBackend(
191 &cache, cb.callback());
192 ASSERT_EQ(OK, cb.GetResult(rv));
194 std::string key("the key");
195 disk_cache::Entry* entry;
196 rv = cache->CreateEntry(key, &entry, cb.callback());
197 ASSERT_EQ(OK, cb.GetResult(rv));
199 // RESPONSE_INFO_TRUNCATED defined on response_info.cc
200 int flags = 1 << 12;
201 WriteHeaders(entry, flags, "something");
202 entry->Close();
204 std::string data;
205 TestCompletionCallback cb1;
206 rv = helper.GetEntryInfoHTML(key, &context, &data, cb1.callback());
207 EXPECT_EQ(OK, cb1.GetResult(rv));
209 EXPECT_NE(std::string::npos, data.find("RESPONSE_INFO_TRUNCATED"));
212 } // namespace net