Roll src/third_party/skia 5cc0f6c:01d3319
[chromium-blink-merge.git] / net / url_request / view_cache_helper_unittest.cc
blob097eade98dc9176f279b8aa1b625d7dfc83401fb
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();
24 ~TestURLRequestContext() override { AssertNoURLRequests(); }
26 // Gets a pointer to the cache backend.
27 disk_cache::Backend* GetBackend();
29 private:
30 HttpCache cache_;
33 TestURLRequestContext::TestURLRequestContext()
34 : cache_(new MockNetworkLayer(), NULL,
35 HttpCache::DefaultBackend::InMemory(0)) {
36 set_http_transaction_factory(&cache_);
39 void WriteHeaders(disk_cache::Entry* entry, int flags,
40 const std::string& data) {
41 if (data.empty())
42 return;
44 Pickle pickle;
45 pickle.WriteInt(flags | 1); // Version 1.
46 pickle.WriteInt64(0);
47 pickle.WriteInt64(0);
48 pickle.WriteString(data);
50 scoped_refptr<WrappedIOBuffer> buf(new WrappedIOBuffer(
51 reinterpret_cast<const char*>(pickle.data())));
52 int len = static_cast<int>(pickle.size());
54 net::TestCompletionCallback cb;
55 int rv = entry->WriteData(0, 0, buf.get(), len, cb.callback(), true);
56 ASSERT_EQ(len, cb.GetResult(rv));
59 void WriteData(disk_cache::Entry* entry, int index, const std::string& data) {
60 if (data.empty())
61 return;
63 int len = data.length();
64 scoped_refptr<IOBuffer> buf(new IOBuffer(len));
65 memcpy(buf->data(), data.data(), data.length());
67 net::TestCompletionCallback cb;
68 int rv = entry->WriteData(index, 0, buf.get(), len, cb.callback(), true);
69 ASSERT_EQ(len, cb.GetResult(rv));
72 void WriteToEntry(disk_cache::Backend* cache, const std::string& key,
73 const std::string& data0, const std::string& data1,
74 const std::string& data2) {
75 net::TestCompletionCallback cb;
76 disk_cache::Entry* entry;
77 int rv = cache->CreateEntry(key, &entry, cb.callback());
78 rv = cb.GetResult(rv);
79 if (rv != OK) {
80 rv = cache->OpenEntry(key, &entry, cb.callback());
81 ASSERT_EQ(OK, cb.GetResult(rv));
84 WriteHeaders(entry, 0, data0);
85 WriteData(entry, 1, data1);
86 WriteData(entry, 2, data2);
88 entry->Close();
91 void FillCache(URLRequestContext* context) {
92 net::TestCompletionCallback cb;
93 disk_cache::Backend* cache;
94 int rv =
95 context->http_transaction_factory()->GetCache()->GetBackend(
96 &cache, cb.callback());
97 ASSERT_EQ(OK, cb.GetResult(rv));
99 std::string empty;
100 WriteToEntry(cache, "first", "some", empty, empty);
101 WriteToEntry(cache, "second", "only hex_dumped", "same", "kind");
102 WriteToEntry(cache, "third", empty, "another", "thing");
105 } // namespace.
107 TEST(ViewCacheHelper, EmptyCache) {
108 TestURLRequestContext context;
109 ViewCacheHelper helper;
111 TestCompletionCallback cb;
112 std::string prefix, data;
113 int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback());
114 EXPECT_EQ(OK, cb.GetResult(rv));
115 EXPECT_FALSE(data.empty());
118 TEST(ViewCacheHelper, ListContents) {
119 TestURLRequestContext context;
120 ViewCacheHelper helper;
122 FillCache(&context);
124 std::string prefix, data;
125 TestCompletionCallback cb;
126 int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback());
127 EXPECT_EQ(OK, cb.GetResult(rv));
129 EXPECT_EQ(0U, data.find("<html>"));
130 EXPECT_NE(std::string::npos, data.find("</html>"));
131 EXPECT_NE(std::string::npos, data.find("first"));
132 EXPECT_NE(std::string::npos, data.find("second"));
133 EXPECT_NE(std::string::npos, data.find("third"));
135 EXPECT_EQ(std::string::npos, data.find("some"));
136 EXPECT_EQ(std::string::npos, data.find("same"));
137 EXPECT_EQ(std::string::npos, data.find("thing"));
140 TEST(ViewCacheHelper, DumpEntry) {
141 TestURLRequestContext context;
142 ViewCacheHelper helper;
144 FillCache(&context);
146 std::string data;
147 TestCompletionCallback cb;
148 int rv = helper.GetEntryInfoHTML("second", &context, &data, cb.callback());
149 EXPECT_EQ(OK, cb.GetResult(rv));
151 EXPECT_EQ(0U, data.find("<html>"));
152 EXPECT_NE(std::string::npos, data.find("</html>"));
154 EXPECT_NE(std::string::npos, data.find("hex_dumped"));
155 EXPECT_NE(std::string::npos, data.find("same"));
156 EXPECT_NE(std::string::npos, data.find("kind"));
158 EXPECT_EQ(std::string::npos, data.find("first"));
159 EXPECT_EQ(std::string::npos, data.find("third"));
160 EXPECT_EQ(std::string::npos, data.find("some"));
161 EXPECT_EQ(std::string::npos, data.find("another"));
164 // Makes sure the links are correct.
165 TEST(ViewCacheHelper, Prefix) {
166 TestURLRequestContext context;
167 ViewCacheHelper helper;
169 FillCache(&context);
171 std::string key, data;
172 std::string prefix("prefix:");
173 TestCompletionCallback cb;
174 int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback());
175 EXPECT_EQ(OK, cb.GetResult(rv));
177 EXPECT_EQ(0U, data.find("<html>"));
178 EXPECT_NE(std::string::npos, data.find("</html>"));
179 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:first\">"));
180 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:second\">"));
181 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:third\">"));
184 TEST(ViewCacheHelper, TruncatedFlag) {
185 TestURLRequestContext context;
186 ViewCacheHelper helper;
188 net::TestCompletionCallback cb;
189 disk_cache::Backend* cache;
190 int rv =
191 context.http_transaction_factory()->GetCache()->GetBackend(
192 &cache, cb.callback());
193 ASSERT_EQ(OK, cb.GetResult(rv));
195 std::string key("the key");
196 disk_cache::Entry* entry;
197 rv = cache->CreateEntry(key, &entry, cb.callback());
198 ASSERT_EQ(OK, cb.GetResult(rv));
200 // RESPONSE_INFO_TRUNCATED defined on response_info.cc
201 int flags = 1 << 12;
202 WriteHeaders(entry, flags, "something");
203 entry->Close();
205 std::string data;
206 TestCompletionCallback cb1;
207 rv = helper.GetEntryInfoHTML(key, &context, &data, cb1.callback());
208 EXPECT_EQ(OK, cb1.GetResult(rv));
210 EXPECT_NE(std::string::npos, data.find("RESPONSE_INFO_TRUNCATED"));
213 } // namespace net