| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/url_request/view_cache_helper.h" | 5 #include "net/url_request/view_cache_helper.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/pickle.h" | 8 #include "base/pickle.h" |
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 #include "net/base/test_completion_callback.h" | 10 #include "net/base/test_completion_callback.h" |
| 11 #include "net/disk_cache/disk_cache.h" | 11 #include "net/disk_cache/disk_cache.h" |
| 12 #include "net/http/http_cache.h" | 12 #include "net/http/http_cache.h" |
| 13 #include "net/http/http_transaction_test_util.h" | 13 #include "net/http/http_transaction_test_util.h" |
| 14 #include "net/test/gtest_util.h" |
| 14 #include "net/url_request/url_request_context.h" | 15 #include "net/url_request/url_request_context.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 18 |
| 19 using net::test::IsOk; |
| 20 |
| 17 namespace net { | 21 namespace net { |
| 18 | 22 |
| 19 namespace { | 23 namespace { |
| 20 | 24 |
| 21 class TestURLRequestContext : public URLRequestContext { | 25 class TestURLRequestContext : public URLRequestContext { |
| 22 public: | 26 public: |
| 23 TestURLRequestContext(); | 27 TestURLRequestContext(); |
| 24 | 28 |
| 25 ~TestURLRequestContext() override { AssertNoURLRequests(); } | 29 ~TestURLRequestContext() override { AssertNoURLRequests(); } |
| 26 | 30 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 | 77 |
| 74 void WriteToEntry(disk_cache::Backend* cache, const std::string& key, | 78 void WriteToEntry(disk_cache::Backend* cache, const std::string& key, |
| 75 const std::string& data0, const std::string& data1, | 79 const std::string& data0, const std::string& data1, |
| 76 const std::string& data2) { | 80 const std::string& data2) { |
| 77 TestCompletionCallback cb; | 81 TestCompletionCallback cb; |
| 78 disk_cache::Entry* entry; | 82 disk_cache::Entry* entry; |
| 79 int rv = cache->CreateEntry(key, &entry, cb.callback()); | 83 int rv = cache->CreateEntry(key, &entry, cb.callback()); |
| 80 rv = cb.GetResult(rv); | 84 rv = cb.GetResult(rv); |
| 81 if (rv != OK) { | 85 if (rv != OK) { |
| 82 rv = cache->OpenEntry(key, &entry, cb.callback()); | 86 rv = cache->OpenEntry(key, &entry, cb.callback()); |
| 83 ASSERT_EQ(OK, cb.GetResult(rv)); | 87 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
| 84 } | 88 } |
| 85 | 89 |
| 86 WriteHeaders(entry, 0, data0); | 90 WriteHeaders(entry, 0, data0); |
| 87 WriteData(entry, 1, data1); | 91 WriteData(entry, 1, data1); |
| 88 WriteData(entry, 2, data2); | 92 WriteData(entry, 2, data2); |
| 89 | 93 |
| 90 entry->Close(); | 94 entry->Close(); |
| 91 } | 95 } |
| 92 | 96 |
| 93 void FillCache(URLRequestContext* context) { | 97 void FillCache(URLRequestContext* context) { |
| 94 TestCompletionCallback cb; | 98 TestCompletionCallback cb; |
| 95 disk_cache::Backend* cache; | 99 disk_cache::Backend* cache; |
| 96 int rv = | 100 int rv = |
| 97 context->http_transaction_factory()->GetCache()->GetBackend( | 101 context->http_transaction_factory()->GetCache()->GetBackend( |
| 98 &cache, cb.callback()); | 102 &cache, cb.callback()); |
| 99 ASSERT_EQ(OK, cb.GetResult(rv)); | 103 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
| 100 | 104 |
| 101 std::string empty; | 105 std::string empty; |
| 102 WriteToEntry(cache, "first", "some", empty, empty); | 106 WriteToEntry(cache, "first", "some", empty, empty); |
| 103 WriteToEntry(cache, "second", "only hex_dumped", "same", "kind"); | 107 WriteToEntry(cache, "second", "only hex_dumped", "same", "kind"); |
| 104 WriteToEntry(cache, "third", empty, "another", "thing"); | 108 WriteToEntry(cache, "third", empty, "another", "thing"); |
| 105 } | 109 } |
| 106 | 110 |
| 107 } // namespace. | 111 } // namespace. |
| 108 | 112 |
| 109 TEST(ViewCacheHelper, EmptyCache) { | 113 TEST(ViewCacheHelper, EmptyCache) { |
| 110 TestURLRequestContext context; | 114 TestURLRequestContext context; |
| 111 ViewCacheHelper helper; | 115 ViewCacheHelper helper; |
| 112 | 116 |
| 113 TestCompletionCallback cb; | 117 TestCompletionCallback cb; |
| 114 std::string prefix, data; | 118 std::string prefix, data; |
| 115 int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback()); | 119 int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback()); |
| 116 EXPECT_EQ(OK, cb.GetResult(rv)); | 120 EXPECT_THAT(cb.GetResult(rv), IsOk()); |
| 117 EXPECT_FALSE(data.empty()); | 121 EXPECT_FALSE(data.empty()); |
| 118 } | 122 } |
| 119 | 123 |
| 120 TEST(ViewCacheHelper, ListContents) { | 124 TEST(ViewCacheHelper, ListContents) { |
| 121 TestURLRequestContext context; | 125 TestURLRequestContext context; |
| 122 ViewCacheHelper helper; | 126 ViewCacheHelper helper; |
| 123 | 127 |
| 124 FillCache(&context); | 128 FillCache(&context); |
| 125 | 129 |
| 126 std::string prefix, data; | 130 std::string prefix, data; |
| 127 TestCompletionCallback cb; | 131 TestCompletionCallback cb; |
| 128 int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback()); | 132 int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback()); |
| 129 EXPECT_EQ(OK, cb.GetResult(rv)); | 133 EXPECT_THAT(cb.GetResult(rv), IsOk()); |
| 130 | 134 |
| 131 EXPECT_EQ(0U, data.find("<html>")); | 135 EXPECT_EQ(0U, data.find("<html>")); |
| 132 EXPECT_NE(std::string::npos, data.find("</html>")); | 136 EXPECT_NE(std::string::npos, data.find("</html>")); |
| 133 EXPECT_NE(std::string::npos, data.find("first")); | 137 EXPECT_NE(std::string::npos, data.find("first")); |
| 134 EXPECT_NE(std::string::npos, data.find("second")); | 138 EXPECT_NE(std::string::npos, data.find("second")); |
| 135 EXPECT_NE(std::string::npos, data.find("third")); | 139 EXPECT_NE(std::string::npos, data.find("third")); |
| 136 | 140 |
| 137 EXPECT_EQ(std::string::npos, data.find("some")); | 141 EXPECT_EQ(std::string::npos, data.find("some")); |
| 138 EXPECT_EQ(std::string::npos, data.find("same")); | 142 EXPECT_EQ(std::string::npos, data.find("same")); |
| 139 EXPECT_EQ(std::string::npos, data.find("thing")); | 143 EXPECT_EQ(std::string::npos, data.find("thing")); |
| 140 } | 144 } |
| 141 | 145 |
| 142 TEST(ViewCacheHelper, DumpEntry) { | 146 TEST(ViewCacheHelper, DumpEntry) { |
| 143 TestURLRequestContext context; | 147 TestURLRequestContext context; |
| 144 ViewCacheHelper helper; | 148 ViewCacheHelper helper; |
| 145 | 149 |
| 146 FillCache(&context); | 150 FillCache(&context); |
| 147 | 151 |
| 148 std::string data; | 152 std::string data; |
| 149 TestCompletionCallback cb; | 153 TestCompletionCallback cb; |
| 150 int rv = helper.GetEntryInfoHTML("second", &context, &data, cb.callback()); | 154 int rv = helper.GetEntryInfoHTML("second", &context, &data, cb.callback()); |
| 151 EXPECT_EQ(OK, cb.GetResult(rv)); | 155 EXPECT_THAT(cb.GetResult(rv), IsOk()); |
| 152 | 156 |
| 153 EXPECT_EQ(0U, data.find("<html>")); | 157 EXPECT_EQ(0U, data.find("<html>")); |
| 154 EXPECT_NE(std::string::npos, data.find("</html>")); | 158 EXPECT_NE(std::string::npos, data.find("</html>")); |
| 155 | 159 |
| 156 EXPECT_NE(std::string::npos, data.find("hex_dumped")); | 160 EXPECT_NE(std::string::npos, data.find("hex_dumped")); |
| 157 EXPECT_NE(std::string::npos, data.find("same")); | 161 EXPECT_NE(std::string::npos, data.find("same")); |
| 158 EXPECT_NE(std::string::npos, data.find("kind")); | 162 EXPECT_NE(std::string::npos, data.find("kind")); |
| 159 | 163 |
| 160 EXPECT_EQ(std::string::npos, data.find("first")); | 164 EXPECT_EQ(std::string::npos, data.find("first")); |
| 161 EXPECT_EQ(std::string::npos, data.find("third")); | 165 EXPECT_EQ(std::string::npos, data.find("third")); |
| 162 EXPECT_EQ(std::string::npos, data.find("some")); | 166 EXPECT_EQ(std::string::npos, data.find("some")); |
| 163 EXPECT_EQ(std::string::npos, data.find("another")); | 167 EXPECT_EQ(std::string::npos, data.find("another")); |
| 164 } | 168 } |
| 165 | 169 |
| 166 // Makes sure the links are correct. | 170 // Makes sure the links are correct. |
| 167 TEST(ViewCacheHelper, Prefix) { | 171 TEST(ViewCacheHelper, Prefix) { |
| 168 TestURLRequestContext context; | 172 TestURLRequestContext context; |
| 169 ViewCacheHelper helper; | 173 ViewCacheHelper helper; |
| 170 | 174 |
| 171 FillCache(&context); | 175 FillCache(&context); |
| 172 | 176 |
| 173 std::string key, data; | 177 std::string key, data; |
| 174 std::string prefix("prefix:"); | 178 std::string prefix("prefix:"); |
| 175 TestCompletionCallback cb; | 179 TestCompletionCallback cb; |
| 176 int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback()); | 180 int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback()); |
| 177 EXPECT_EQ(OK, cb.GetResult(rv)); | 181 EXPECT_THAT(cb.GetResult(rv), IsOk()); |
| 178 | 182 |
| 179 EXPECT_EQ(0U, data.find("<html>")); | 183 EXPECT_EQ(0U, data.find("<html>")); |
| 180 EXPECT_NE(std::string::npos, data.find("</html>")); | 184 EXPECT_NE(std::string::npos, data.find("</html>")); |
| 181 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:first\">")); | 185 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:first\">")); |
| 182 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:second\">")); | 186 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:second\">")); |
| 183 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:third\">")); | 187 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:third\">")); |
| 184 } | 188 } |
| 185 | 189 |
| 186 TEST(ViewCacheHelper, TruncatedFlag) { | 190 TEST(ViewCacheHelper, TruncatedFlag) { |
| 187 TestURLRequestContext context; | 191 TestURLRequestContext context; |
| 188 ViewCacheHelper helper; | 192 ViewCacheHelper helper; |
| 189 | 193 |
| 190 TestCompletionCallback cb; | 194 TestCompletionCallback cb; |
| 191 disk_cache::Backend* cache; | 195 disk_cache::Backend* cache; |
| 192 int rv = | 196 int rv = |
| 193 context.http_transaction_factory()->GetCache()->GetBackend( | 197 context.http_transaction_factory()->GetCache()->GetBackend( |
| 194 &cache, cb.callback()); | 198 &cache, cb.callback()); |
| 195 ASSERT_EQ(OK, cb.GetResult(rv)); | 199 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
| 196 | 200 |
| 197 std::string key("the key"); | 201 std::string key("the key"); |
| 198 disk_cache::Entry* entry; | 202 disk_cache::Entry* entry; |
| 199 rv = cache->CreateEntry(key, &entry, cb.callback()); | 203 rv = cache->CreateEntry(key, &entry, cb.callback()); |
| 200 ASSERT_EQ(OK, cb.GetResult(rv)); | 204 ASSERT_THAT(cb.GetResult(rv), IsOk()); |
| 201 | 205 |
| 202 // RESPONSE_INFO_TRUNCATED defined on response_info.cc | 206 // RESPONSE_INFO_TRUNCATED defined on response_info.cc |
| 203 int flags = 1 << 12; | 207 int flags = 1 << 12; |
| 204 WriteHeaders(entry, flags, "something"); | 208 WriteHeaders(entry, flags, "something"); |
| 205 entry->Close(); | 209 entry->Close(); |
| 206 | 210 |
| 207 std::string data; | 211 std::string data; |
| 208 TestCompletionCallback cb1; | 212 TestCompletionCallback cb1; |
| 209 rv = helper.GetEntryInfoHTML(key, &context, &data, cb1.callback()); | 213 rv = helper.GetEntryInfoHTML(key, &context, &data, cb1.callback()); |
| 210 EXPECT_EQ(OK, cb1.GetResult(rv)); | 214 EXPECT_THAT(cb1.GetResult(rv), IsOk()); |
| 211 | 215 |
| 212 EXPECT_NE(std::string::npos, data.find("RESPONSE_INFO_TRUNCATED")); | 216 EXPECT_NE(std::string::npos, data.find("RESPONSE_INFO_TRUNCATED")); |
| 213 } | 217 } |
| 214 | 218 |
| 215 } // namespace net | 219 } // namespace net |
| OLD | NEW |