OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 #include "net/url_request/view_cache_helper.h" |
| 6 |
| 7 #include "base/pickle.h" |
| 8 #include "net/base/test_completion_callback.h" |
| 9 #include "net/disk_cache/disk_cache.h" |
| 10 #include "net/http/http_cache.h" |
| 11 #include "net/url_request/url_request_context.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 class TestURLRequestContext : public URLRequestContext { |
| 17 public: |
| 18 TestURLRequestContext(); |
| 19 |
| 20 // Gets a pointer to the cache backend. |
| 21 disk_cache::Backend* GetBackend(); |
| 22 |
| 23 private: |
| 24 net::HttpCache cache_; |
| 25 }; |
| 26 |
| 27 TestURLRequestContext::TestURLRequestContext() |
| 28 : cache_(reinterpret_cast<net::HttpTransactionFactory*>(NULL), |
| 29 net::HttpCache::DefaultBackend::InMemory(0)) { |
| 30 http_transaction_factory_ = &cache_; |
| 31 } |
| 32 |
| 33 void WriteHeaders(disk_cache::Entry* entry, int flags, const std::string data) { |
| 34 if (data.empty()) |
| 35 return; |
| 36 |
| 37 Pickle pickle; |
| 38 pickle.WriteInt(flags | 1); // Version 1. |
| 39 pickle.WriteInt64(0); |
| 40 pickle.WriteInt64(0); |
| 41 pickle.WriteString(data); |
| 42 |
| 43 scoped_refptr<net::WrappedIOBuffer> buf = new net::WrappedIOBuffer( |
| 44 reinterpret_cast<const char*>(pickle.data())); |
| 45 int len = static_cast<int>(pickle.size()); |
| 46 |
| 47 TestCompletionCallback cb; |
| 48 int rv = entry->WriteData(0, 0, buf, len, &cb, true); |
| 49 ASSERT_EQ(len, cb.GetResult(rv)); |
| 50 } |
| 51 |
| 52 void WriteData(disk_cache::Entry* entry, int index, const std::string data) { |
| 53 if (data.empty()) |
| 54 return; |
| 55 |
| 56 int len = data.length(); |
| 57 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(len)); |
| 58 memcpy(buf->data(), data.data(), data.length()); |
| 59 |
| 60 TestCompletionCallback cb; |
| 61 int rv = entry->WriteData(index, 0, buf, len, &cb, true); |
| 62 ASSERT_EQ(len, cb.GetResult(rv)); |
| 63 } |
| 64 |
| 65 void WriteToEntry(disk_cache::Backend* cache, const std::string key, |
| 66 const std::string data0, const std::string data1, |
| 67 const std::string data2) { |
| 68 TestCompletionCallback cb; |
| 69 disk_cache::Entry* entry; |
| 70 int rv = cache->CreateEntry(key, &entry, &cb); |
| 71 rv = cb.GetResult(rv); |
| 72 if (rv != net::OK) { |
| 73 rv = cache->OpenEntry(key, &entry, &cb); |
| 74 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 75 } |
| 76 |
| 77 WriteHeaders(entry, 0, data0); |
| 78 WriteData(entry, 1, data1); |
| 79 WriteData(entry, 2, data2); |
| 80 |
| 81 entry->Close(); |
| 82 } |
| 83 |
| 84 void FillCache(URLRequestContext* context) { |
| 85 TestCompletionCallback cb; |
| 86 disk_cache::Backend* cache; |
| 87 int rv = |
| 88 context->http_transaction_factory()->GetCache()->GetBackend(&cache, &cb); |
| 89 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 90 |
| 91 std::string empty; |
| 92 WriteToEntry(cache, "first", "some", empty, empty); |
| 93 WriteToEntry(cache, "second", "only hex_dumped", "same", "kind"); |
| 94 WriteToEntry(cache, "third", empty, "another", "thing"); |
| 95 } |
| 96 |
| 97 } // namespace. |
| 98 |
| 99 TEST(ViewCacheHelper, EmptyCache) { |
| 100 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
| 101 net::ViewCacheHelper helper; |
| 102 |
| 103 TestCompletionCallback cb; |
| 104 std::string prefix, data; |
| 105 int rv = helper.GetContentsHTML(context, prefix, &data, &cb); |
| 106 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| 107 EXPECT_FALSE(data.empty()); |
| 108 } |
| 109 |
| 110 TEST(ViewCacheHelper, ListContents) { |
| 111 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
| 112 net::ViewCacheHelper helper; |
| 113 |
| 114 FillCache(context); |
| 115 |
| 116 std::string prefix, data; |
| 117 TestCompletionCallback cb; |
| 118 int rv = helper.GetContentsHTML(context, prefix, &data, &cb); |
| 119 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| 120 |
| 121 EXPECT_EQ(0U, data.find("<html>")); |
| 122 EXPECT_NE(std::string::npos, data.find("</html>")); |
| 123 EXPECT_NE(std::string::npos, data.find("first")); |
| 124 EXPECT_NE(std::string::npos, data.find("second")); |
| 125 EXPECT_NE(std::string::npos, data.find("third")); |
| 126 |
| 127 EXPECT_EQ(std::string::npos, data.find("some")); |
| 128 EXPECT_EQ(std::string::npos, data.find("same")); |
| 129 EXPECT_EQ(std::string::npos, data.find("thing")); |
| 130 } |
| 131 |
| 132 TEST(ViewCacheHelper, DumpEntry) { |
| 133 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
| 134 net::ViewCacheHelper helper; |
| 135 |
| 136 FillCache(context); |
| 137 |
| 138 std::string data; |
| 139 TestCompletionCallback cb; |
| 140 int rv = helper.GetEntryInfoHTML("second", context, &data, &cb); |
| 141 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| 142 |
| 143 EXPECT_EQ(0U, data.find("<html>")); |
| 144 EXPECT_NE(std::string::npos, data.find("</html>")); |
| 145 |
| 146 EXPECT_NE(std::string::npos, data.find("hex_dumped")); |
| 147 EXPECT_NE(std::string::npos, data.find("same")); |
| 148 EXPECT_NE(std::string::npos, data.find("kind")); |
| 149 |
| 150 EXPECT_EQ(std::string::npos, data.find("first")); |
| 151 EXPECT_EQ(std::string::npos, data.find("third")); |
| 152 EXPECT_EQ(std::string::npos, data.find("some")); |
| 153 EXPECT_EQ(std::string::npos, data.find("another")); |
| 154 } |
| 155 |
| 156 // Makes sure the links are correct. |
| 157 TEST(ViewCacheHelper, Prefix) { |
| 158 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
| 159 net::ViewCacheHelper helper; |
| 160 |
| 161 FillCache(context); |
| 162 |
| 163 std::string key, data; |
| 164 std::string prefix("prefix:"); |
| 165 TestCompletionCallback cb; |
| 166 int rv = helper.GetContentsHTML(context, prefix, &data, &cb); |
| 167 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| 168 |
| 169 EXPECT_EQ(0U, data.find("<html>")); |
| 170 EXPECT_NE(std::string::npos, data.find("</html>")); |
| 171 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:first\">")); |
| 172 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:second\">")); |
| 173 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:third\">")); |
| 174 } |
| 175 |
| 176 TEST(ViewCacheHelper, TruncatedFlag) { |
| 177 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
| 178 net::ViewCacheHelper helper; |
| 179 |
| 180 TestCompletionCallback cb; |
| 181 disk_cache::Backend* cache; |
| 182 int rv = |
| 183 context->http_transaction_factory()->GetCache()->GetBackend(&cache, &cb); |
| 184 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 185 |
| 186 std::string key("the key"); |
| 187 disk_cache::Entry* entry; |
| 188 rv = cache->CreateEntry(key, &entry, &cb); |
| 189 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 190 |
| 191 // RESPONSE_INFO_TRUNCATED defined on response_info.cc |
| 192 int flags = 1 << 12; |
| 193 WriteHeaders(entry, flags, "something"); |
| 194 entry->Close(); |
| 195 |
| 196 std::string data; |
| 197 rv = helper.GetEntryInfoHTML(key, context, &data, &cb); |
| 198 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| 199 |
| 200 EXPECT_NE(std::string::npos, data.find("RESPONSE_INFO_TRUNCATED")); |
| 201 } |
OLD | NEW |