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/pickle.h" | 7 #include "base/pickle.h" |
8 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
9 #include "net/base/test_completion_callback.h" | 9 #include "net/base/test_completion_callback.h" |
10 #include "net/disk_cache/disk_cache.h" | 10 #include "net/disk_cache/disk_cache.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 virtual ~TestURLRequestContext() {} | 22 virtual ~TestURLRequestContext() {} |
23 | 23 |
24 // Gets a pointer to the cache backend. | 24 // Gets a pointer to the cache backend. |
25 disk_cache::Backend* GetBackend(); | 25 disk_cache::Backend* GetBackend(); |
26 | 26 |
27 private: | 27 private: |
28 HttpCache cache_; | 28 HttpCache cache_; |
29 }; | 29 }; |
30 | 30 |
31 TestURLRequestContext::TestURLRequestContext() | 31 TestURLRequestContext::TestURLRequestContext() |
32 : cache_(reinterpret_cast<HttpTransactionFactory*>(NULL), NULL, | 32 : cache_(reinterpret_cast<HttpTransactionFactory*>(NULL), |
| 33 NULL, |
33 HttpCache::DefaultBackend::InMemory(0)) { | 34 HttpCache::DefaultBackend::InMemory(0)) { |
34 set_http_transaction_factory(&cache_); | 35 set_http_transaction_factory(&cache_); |
35 } | 36 } |
36 | 37 |
37 void WriteHeaders(disk_cache::Entry* entry, int flags, | 38 void WriteHeaders(disk_cache::Entry* entry, |
| 39 int flags, |
38 const std::string& data) { | 40 const std::string& data) { |
39 if (data.empty()) | 41 if (data.empty()) |
40 return; | 42 return; |
41 | 43 |
42 Pickle pickle; | 44 Pickle pickle; |
43 pickle.WriteInt(flags | 1); // Version 1. | 45 pickle.WriteInt(flags | 1); // Version 1. |
44 pickle.WriteInt64(0); | 46 pickle.WriteInt64(0); |
45 pickle.WriteInt64(0); | 47 pickle.WriteInt64(0); |
46 pickle.WriteString(data); | 48 pickle.WriteString(data); |
47 | 49 |
48 scoped_refptr<WrappedIOBuffer> buf(new WrappedIOBuffer( | 50 scoped_refptr<WrappedIOBuffer> buf( |
49 reinterpret_cast<const char*>(pickle.data()))); | 51 new WrappedIOBuffer(reinterpret_cast<const char*>(pickle.data()))); |
50 int len = static_cast<int>(pickle.size()); | 52 int len = static_cast<int>(pickle.size()); |
51 | 53 |
52 net::TestCompletionCallback cb; | 54 net::TestCompletionCallback cb; |
53 int rv = entry->WriteData(0, 0, buf.get(), len, cb.callback(), true); | 55 int rv = entry->WriteData(0, 0, buf.get(), len, cb.callback(), true); |
54 ASSERT_EQ(len, cb.GetResult(rv)); | 56 ASSERT_EQ(len, cb.GetResult(rv)); |
55 } | 57 } |
56 | 58 |
57 void WriteData(disk_cache::Entry* entry, int index, const std::string& data) { | 59 void WriteData(disk_cache::Entry* entry, int index, const std::string& data) { |
58 if (data.empty()) | 60 if (data.empty()) |
59 return; | 61 return; |
60 | 62 |
61 int len = data.length(); | 63 int len = data.length(); |
62 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); | 64 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); |
63 memcpy(buf->data(), data.data(), data.length()); | 65 memcpy(buf->data(), data.data(), data.length()); |
64 | 66 |
65 net::TestCompletionCallback cb; | 67 net::TestCompletionCallback cb; |
66 int rv = entry->WriteData(index, 0, buf.get(), len, cb.callback(), true); | 68 int rv = entry->WriteData(index, 0, buf.get(), len, cb.callback(), true); |
67 ASSERT_EQ(len, cb.GetResult(rv)); | 69 ASSERT_EQ(len, cb.GetResult(rv)); |
68 } | 70 } |
69 | 71 |
70 void WriteToEntry(disk_cache::Backend* cache, const std::string& key, | 72 void WriteToEntry(disk_cache::Backend* cache, |
71 const std::string& data0, const std::string& data1, | 73 const std::string& key, |
| 74 const std::string& data0, |
| 75 const std::string& data1, |
72 const std::string& data2) { | 76 const std::string& data2) { |
73 net::TestCompletionCallback cb; | 77 net::TestCompletionCallback cb; |
74 disk_cache::Entry* entry; | 78 disk_cache::Entry* entry; |
75 int rv = cache->CreateEntry(key, &entry, cb.callback()); | 79 int rv = cache->CreateEntry(key, &entry, cb.callback()); |
76 rv = cb.GetResult(rv); | 80 rv = cb.GetResult(rv); |
77 if (rv != OK) { | 81 if (rv != OK) { |
78 rv = cache->OpenEntry(key, &entry, cb.callback()); | 82 rv = cache->OpenEntry(key, &entry, cb.callback()); |
79 ASSERT_EQ(OK, cb.GetResult(rv)); | 83 ASSERT_EQ(OK, cb.GetResult(rv)); |
80 } | 84 } |
81 | 85 |
82 WriteHeaders(entry, 0, data0); | 86 WriteHeaders(entry, 0, data0); |
83 WriteData(entry, 1, data1); | 87 WriteData(entry, 1, data1); |
84 WriteData(entry, 2, data2); | 88 WriteData(entry, 2, data2); |
85 | 89 |
86 entry->Close(); | 90 entry->Close(); |
87 } | 91 } |
88 | 92 |
89 void FillCache(URLRequestContext* context) { | 93 void FillCache(URLRequestContext* context) { |
90 net::TestCompletionCallback cb; | 94 net::TestCompletionCallback cb; |
91 disk_cache::Backend* cache; | 95 disk_cache::Backend* cache; |
92 int rv = | 96 int rv = context->http_transaction_factory()->GetCache()->GetBackend( |
93 context->http_transaction_factory()->GetCache()->GetBackend( | 97 &cache, cb.callback()); |
94 &cache, cb.callback()); | |
95 ASSERT_EQ(OK, cb.GetResult(rv)); | 98 ASSERT_EQ(OK, cb.GetResult(rv)); |
96 | 99 |
97 std::string empty; | 100 std::string empty; |
98 WriteToEntry(cache, "first", "some", empty, empty); | 101 WriteToEntry(cache, "first", "some", empty, empty); |
99 WriteToEntry(cache, "second", "only hex_dumped", "same", "kind"); | 102 WriteToEntry(cache, "second", "only hex_dumped", "same", "kind"); |
100 WriteToEntry(cache, "third", empty, "another", "thing"); | 103 WriteToEntry(cache, "third", empty, "another", "thing"); |
101 } | 104 } |
102 | 105 |
103 } // namespace. | 106 } // namespace. |
104 | 107 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:second\">")); | 181 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:second\">")); |
179 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:third\">")); | 182 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:third\">")); |
180 } | 183 } |
181 | 184 |
182 TEST(ViewCacheHelper, TruncatedFlag) { | 185 TEST(ViewCacheHelper, TruncatedFlag) { |
183 TestURLRequestContext context; | 186 TestURLRequestContext context; |
184 ViewCacheHelper helper; | 187 ViewCacheHelper helper; |
185 | 188 |
186 net::TestCompletionCallback cb; | 189 net::TestCompletionCallback cb; |
187 disk_cache::Backend* cache; | 190 disk_cache::Backend* cache; |
188 int rv = | 191 int rv = context.http_transaction_factory()->GetCache()->GetBackend( |
189 context.http_transaction_factory()->GetCache()->GetBackend( | 192 &cache, cb.callback()); |
190 &cache, cb.callback()); | |
191 ASSERT_EQ(OK, cb.GetResult(rv)); | 193 ASSERT_EQ(OK, cb.GetResult(rv)); |
192 | 194 |
193 std::string key("the key"); | 195 std::string key("the key"); |
194 disk_cache::Entry* entry; | 196 disk_cache::Entry* entry; |
195 rv = cache->CreateEntry(key, &entry, cb.callback()); | 197 rv = cache->CreateEntry(key, &entry, cb.callback()); |
196 ASSERT_EQ(OK, cb.GetResult(rv)); | 198 ASSERT_EQ(OK, cb.GetResult(rv)); |
197 | 199 |
198 // RESPONSE_INFO_TRUNCATED defined on response_info.cc | 200 // RESPONSE_INFO_TRUNCATED defined on response_info.cc |
199 int flags = 1 << 12; | 201 int flags = 1 << 12; |
200 WriteHeaders(entry, flags, "something"); | 202 WriteHeaders(entry, flags, "something"); |
201 entry->Close(); | 203 entry->Close(); |
202 | 204 |
203 std::string data; | 205 std::string data; |
204 TestCompletionCallback cb1; | 206 TestCompletionCallback cb1; |
205 rv = helper.GetEntryInfoHTML(key, &context, &data, cb1.callback()); | 207 rv = helper.GetEntryInfoHTML(key, &context, &data, cb1.callback()); |
206 EXPECT_EQ(OK, cb1.GetResult(rv)); | 208 EXPECT_EQ(OK, cb1.GetResult(rv)); |
207 | 209 |
208 EXPECT_NE(std::string::npos, data.find("RESPONSE_INFO_TRUNCATED")); | 210 EXPECT_NE(std::string::npos, data.find("RESPONSE_INFO_TRUNCATED")); |
209 } | 211 } |
210 | 212 |
211 } // namespace net | 213 } // namespace net |
OLD | NEW |