diff options
author | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-27 22:06:31 +0000 |
---|---|---|
committer | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-27 22:06:31 +0000 |
commit | cec6f9b08dd24625cd40df548810561b5a55e7d6 (patch) | |
tree | b3eb73f30533f31a794bf887d7da0eaaf3f13850 /net/url_request/view_cache_helper_unittest.cc | |
parent | 28191891f2389382e4e53d004e04a74bcb1b152e (diff) | |
download | chromium_src-cec6f9b08dd24625cd40df548810561b5a55e7d6.zip chromium_src-cec6f9b08dd24625cd40df548810561b5a55e7d6.tar.gz chromium_src-cec6f9b08dd24625cd40df548810561b5a55e7d6.tar.bz2 |
view-cache: Refactor ViewCacheHelper and ViewHttpCacheJobFactory
to use asynchronous interfaces.
BUG=26730
TEST=unittest
Review URL: http://codereview.chromium.org/2168004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@48438 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/url_request/view_cache_helper_unittest.cc')
-rw-r--r-- | net/url_request/view_cache_helper_unittest.cc | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/net/url_request/view_cache_helper_unittest.cc b/net/url_request/view_cache_helper_unittest.cc new file mode 100644 index 0000000..e82ff15 --- /dev/null +++ b/net/url_request/view_cache_helper_unittest.cc @@ -0,0 +1,201 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/url_request/view_cache_helper.h" + +#include "base/pickle.h" +#include "net/base/test_completion_callback.h" +#include "net/disk_cache/disk_cache.h" +#include "net/http/http_cache.h" +#include "net/url_request/url_request_context.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +class TestURLRequestContext : public URLRequestContext { + public: + TestURLRequestContext(); + + // Gets a pointer to the cache backend. + disk_cache::Backend* GetBackend(); + + private: + net::HttpCache cache_; +}; + +TestURLRequestContext::TestURLRequestContext() + : cache_(reinterpret_cast<net::HttpTransactionFactory*>(NULL), + net::HttpCache::DefaultBackend::InMemory(0)) { + http_transaction_factory_ = &cache_; +} + +void WriteHeaders(disk_cache::Entry* entry, int flags, const std::string data) { + if (data.empty()) + return; + + Pickle pickle; + pickle.WriteInt(flags | 1); // Version 1. + pickle.WriteInt64(0); + pickle.WriteInt64(0); + pickle.WriteString(data); + + scoped_refptr<net::WrappedIOBuffer> buf = new net::WrappedIOBuffer( + reinterpret_cast<const char*>(pickle.data())); + int len = static_cast<int>(pickle.size()); + + TestCompletionCallback cb; + int rv = entry->WriteData(0, 0, buf, len, &cb, true); + ASSERT_EQ(len, cb.GetResult(rv)); +} + +void WriteData(disk_cache::Entry* entry, int index, const std::string data) { + if (data.empty()) + return; + + int len = data.length(); + scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(len)); + memcpy(buf->data(), data.data(), data.length()); + + TestCompletionCallback cb; + int rv = entry->WriteData(index, 0, buf, len, &cb, true); + ASSERT_EQ(len, cb.GetResult(rv)); +} + +void WriteToEntry(disk_cache::Backend* cache, const std::string key, + const std::string data0, const std::string data1, + const std::string data2) { + TestCompletionCallback cb; + disk_cache::Entry* entry; + int rv = cache->CreateEntry(key, &entry, &cb); + rv = cb.GetResult(rv); + if (rv != net::OK) { + rv = cache->OpenEntry(key, &entry, &cb); + ASSERT_EQ(net::OK, cb.GetResult(rv)); + } + + WriteHeaders(entry, 0, data0); + WriteData(entry, 1, data1); + WriteData(entry, 2, data2); + + entry->Close(); +} + +void FillCache(URLRequestContext* context) { + TestCompletionCallback cb; + disk_cache::Backend* cache; + int rv = + context->http_transaction_factory()->GetCache()->GetBackend(&cache, &cb); + ASSERT_EQ(net::OK, cb.GetResult(rv)); + + std::string empty; + WriteToEntry(cache, "first", "some", empty, empty); + WriteToEntry(cache, "second", "only hex_dumped", "same", "kind"); + WriteToEntry(cache, "third", empty, "another", "thing"); +} + +} // namespace. + +TEST(ViewCacheHelper, EmptyCache) { + scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); + net::ViewCacheHelper helper; + + TestCompletionCallback cb; + std::string prefix, data; + int rv = helper.GetContentsHTML(context, prefix, &data, &cb); + EXPECT_EQ(net::OK, cb.GetResult(rv)); + EXPECT_FALSE(data.empty()); +} + +TEST(ViewCacheHelper, ListContents) { + scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); + net::ViewCacheHelper helper; + + FillCache(context); + + std::string prefix, data; + TestCompletionCallback cb; + int rv = helper.GetContentsHTML(context, prefix, &data, &cb); + EXPECT_EQ(net::OK, cb.GetResult(rv)); + + EXPECT_EQ(0U, data.find("<html>")); + EXPECT_NE(std::string::npos, data.find("</html>")); + EXPECT_NE(std::string::npos, data.find("first")); + EXPECT_NE(std::string::npos, data.find("second")); + EXPECT_NE(std::string::npos, data.find("third")); + + EXPECT_EQ(std::string::npos, data.find("some")); + EXPECT_EQ(std::string::npos, data.find("same")); + EXPECT_EQ(std::string::npos, data.find("thing")); +} + +TEST(ViewCacheHelper, DumpEntry) { + scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); + net::ViewCacheHelper helper; + + FillCache(context); + + std::string data; + TestCompletionCallback cb; + int rv = helper.GetEntryInfoHTML("second", context, &data, &cb); + EXPECT_EQ(net::OK, cb.GetResult(rv)); + + EXPECT_EQ(0U, data.find("<html>")); + EXPECT_NE(std::string::npos, data.find("</html>")); + + EXPECT_NE(std::string::npos, data.find("hex_dumped")); + EXPECT_NE(std::string::npos, data.find("same")); + EXPECT_NE(std::string::npos, data.find("kind")); + + EXPECT_EQ(std::string::npos, data.find("first")); + EXPECT_EQ(std::string::npos, data.find("third")); + EXPECT_EQ(std::string::npos, data.find("some")); + EXPECT_EQ(std::string::npos, data.find("another")); +} + +// Makes sure the links are correct. +TEST(ViewCacheHelper, Prefix) { + scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); + net::ViewCacheHelper helper; + + FillCache(context); + + std::string key, data; + std::string prefix("prefix:"); + TestCompletionCallback cb; + int rv = helper.GetContentsHTML(context, prefix, &data, &cb); + EXPECT_EQ(net::OK, cb.GetResult(rv)); + + EXPECT_EQ(0U, data.find("<html>")); + EXPECT_NE(std::string::npos, data.find("</html>")); + EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:first\">")); + EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:second\">")); + EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:third\">")); +} + +TEST(ViewCacheHelper, TruncatedFlag) { + scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); + net::ViewCacheHelper helper; + + TestCompletionCallback cb; + disk_cache::Backend* cache; + int rv = + context->http_transaction_factory()->GetCache()->GetBackend(&cache, &cb); + ASSERT_EQ(net::OK, cb.GetResult(rv)); + + std::string key("the key"); + disk_cache::Entry* entry; + rv = cache->CreateEntry(key, &entry, &cb); + ASSERT_EQ(net::OK, cb.GetResult(rv)); + + // RESPONSE_INFO_TRUNCATED defined on response_info.cc + int flags = 1 << 12; + WriteHeaders(entry, flags, "something"); + entry->Close(); + + std::string data; + rv = helper.GetEntryInfoHTML(key, context, &data, &cb); + EXPECT_EQ(net::OK, cb.GetResult(rv)); + + EXPECT_NE(std::string::npos, data.find("RESPONSE_INFO_TRUNCATED")); +} |