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" |
11 #include "net/http/http_cache.h" | 11 #include "net/http/http_cache.h" |
12 #include "net/url_request/url_request_context.h" | 12 #include "net/url_request/url_request_context.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace net { | 15 namespace net { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 class TestURLRequestContext : public URLRequestContext { | 19 class TestURLRequestContext : public URLRequestContext { |
20 public: | 20 public: |
21 TestURLRequestContext(); | 21 TestURLRequestContext(); |
| 22 virtual ~TestURLRequestContext() {} |
22 | 23 |
23 // Gets a pointer to the cache backend. | 24 // Gets a pointer to the cache backend. |
24 disk_cache::Backend* GetBackend(); | 25 disk_cache::Backend* GetBackend(); |
25 | 26 |
26 protected: | |
27 virtual ~TestURLRequestContext() {} | |
28 | |
29 private: | 27 private: |
30 HttpCache cache_; | 28 HttpCache cache_; |
31 }; | 29 }; |
32 | 30 |
33 TestURLRequestContext::TestURLRequestContext() | 31 TestURLRequestContext::TestURLRequestContext() |
34 : cache_(reinterpret_cast<HttpTransactionFactory*>(NULL), NULL, | 32 : cache_(reinterpret_cast<HttpTransactionFactory*>(NULL), NULL, |
35 HttpCache::DefaultBackend::InMemory(0)) { | 33 HttpCache::DefaultBackend::InMemory(0)) { |
36 set_http_transaction_factory(&cache_); | 34 set_http_transaction_factory(&cache_); |
37 } | 35 } |
38 | 36 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 | 95 |
98 std::string empty; | 96 std::string empty; |
99 WriteToEntry(cache, "first", "some", empty, empty); | 97 WriteToEntry(cache, "first", "some", empty, empty); |
100 WriteToEntry(cache, "second", "only hex_dumped", "same", "kind"); | 98 WriteToEntry(cache, "second", "only hex_dumped", "same", "kind"); |
101 WriteToEntry(cache, "third", empty, "another", "thing"); | 99 WriteToEntry(cache, "third", empty, "another", "thing"); |
102 } | 100 } |
103 | 101 |
104 } // namespace. | 102 } // namespace. |
105 | 103 |
106 TEST(ViewCacheHelper, EmptyCache) { | 104 TEST(ViewCacheHelper, EmptyCache) { |
107 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); | 105 TestURLRequestContext context; |
108 ViewCacheHelper helper; | 106 ViewCacheHelper helper; |
109 | 107 |
110 TestCompletionCallback cb; | 108 TestCompletionCallback cb; |
111 std::string prefix, data; | 109 std::string prefix, data; |
112 int rv = helper.GetContentsHTML(context, prefix, &data, cb.callback()); | 110 int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback()); |
113 EXPECT_EQ(OK, cb.GetResult(rv)); | 111 EXPECT_EQ(OK, cb.GetResult(rv)); |
114 EXPECT_FALSE(data.empty()); | 112 EXPECT_FALSE(data.empty()); |
115 } | 113 } |
116 | 114 |
117 TEST(ViewCacheHelper, ListContents) { | 115 TEST(ViewCacheHelper, ListContents) { |
118 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); | 116 TestURLRequestContext context; |
119 ViewCacheHelper helper; | 117 ViewCacheHelper helper; |
120 | 118 |
121 FillCache(context); | 119 FillCache(&context); |
122 | 120 |
123 std::string prefix, data; | 121 std::string prefix, data; |
124 TestCompletionCallback cb; | 122 TestCompletionCallback cb; |
125 int rv = helper.GetContentsHTML(context, prefix, &data, cb.callback()); | 123 int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback()); |
126 EXPECT_EQ(OK, cb.GetResult(rv)); | 124 EXPECT_EQ(OK, cb.GetResult(rv)); |
127 | 125 |
128 EXPECT_EQ(0U, data.find("<html>")); | 126 EXPECT_EQ(0U, data.find("<html>")); |
129 EXPECT_NE(std::string::npos, data.find("</html>")); | 127 EXPECT_NE(std::string::npos, data.find("</html>")); |
130 EXPECT_NE(std::string::npos, data.find("first")); | 128 EXPECT_NE(std::string::npos, data.find("first")); |
131 EXPECT_NE(std::string::npos, data.find("second")); | 129 EXPECT_NE(std::string::npos, data.find("second")); |
132 EXPECT_NE(std::string::npos, data.find("third")); | 130 EXPECT_NE(std::string::npos, data.find("third")); |
133 | 131 |
134 EXPECT_EQ(std::string::npos, data.find("some")); | 132 EXPECT_EQ(std::string::npos, data.find("some")); |
135 EXPECT_EQ(std::string::npos, data.find("same")); | 133 EXPECT_EQ(std::string::npos, data.find("same")); |
136 EXPECT_EQ(std::string::npos, data.find("thing")); | 134 EXPECT_EQ(std::string::npos, data.find("thing")); |
137 } | 135 } |
138 | 136 |
139 TEST(ViewCacheHelper, DumpEntry) { | 137 TEST(ViewCacheHelper, DumpEntry) { |
140 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); | 138 TestURLRequestContext context; |
141 ViewCacheHelper helper; | 139 ViewCacheHelper helper; |
142 | 140 |
143 FillCache(context); | 141 FillCache(&context); |
144 | 142 |
145 std::string data; | 143 std::string data; |
146 TestCompletionCallback cb; | 144 TestCompletionCallback cb; |
147 int rv = helper.GetEntryInfoHTML("second", context, &data, cb.callback()); | 145 int rv = helper.GetEntryInfoHTML("second", &context, &data, cb.callback()); |
148 EXPECT_EQ(OK, cb.GetResult(rv)); | 146 EXPECT_EQ(OK, cb.GetResult(rv)); |
149 | 147 |
150 EXPECT_EQ(0U, data.find("<html>")); | 148 EXPECT_EQ(0U, data.find("<html>")); |
151 EXPECT_NE(std::string::npos, data.find("</html>")); | 149 EXPECT_NE(std::string::npos, data.find("</html>")); |
152 | 150 |
153 EXPECT_NE(std::string::npos, data.find("hex_dumped")); | 151 EXPECT_NE(std::string::npos, data.find("hex_dumped")); |
154 EXPECT_NE(std::string::npos, data.find("same")); | 152 EXPECT_NE(std::string::npos, data.find("same")); |
155 EXPECT_NE(std::string::npos, data.find("kind")); | 153 EXPECT_NE(std::string::npos, data.find("kind")); |
156 | 154 |
157 EXPECT_EQ(std::string::npos, data.find("first")); | 155 EXPECT_EQ(std::string::npos, data.find("first")); |
158 EXPECT_EQ(std::string::npos, data.find("third")); | 156 EXPECT_EQ(std::string::npos, data.find("third")); |
159 EXPECT_EQ(std::string::npos, data.find("some")); | 157 EXPECT_EQ(std::string::npos, data.find("some")); |
160 EXPECT_EQ(std::string::npos, data.find("another")); | 158 EXPECT_EQ(std::string::npos, data.find("another")); |
161 } | 159 } |
162 | 160 |
163 // Makes sure the links are correct. | 161 // Makes sure the links are correct. |
164 TEST(ViewCacheHelper, Prefix) { | 162 TEST(ViewCacheHelper, Prefix) { |
165 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); | 163 TestURLRequestContext context; |
166 ViewCacheHelper helper; | 164 ViewCacheHelper helper; |
167 | 165 |
168 FillCache(context); | 166 FillCache(&context); |
169 | 167 |
170 std::string key, data; | 168 std::string key, data; |
171 std::string prefix("prefix:"); | 169 std::string prefix("prefix:"); |
172 TestCompletionCallback cb; | 170 TestCompletionCallback cb; |
173 int rv = helper.GetContentsHTML(context, prefix, &data, cb.callback()); | 171 int rv = helper.GetContentsHTML(&context, prefix, &data, cb.callback()); |
174 EXPECT_EQ(OK, cb.GetResult(rv)); | 172 EXPECT_EQ(OK, cb.GetResult(rv)); |
175 | 173 |
176 EXPECT_EQ(0U, data.find("<html>")); | 174 EXPECT_EQ(0U, data.find("<html>")); |
177 EXPECT_NE(std::string::npos, data.find("</html>")); | 175 EXPECT_NE(std::string::npos, data.find("</html>")); |
178 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:first\">")); | 176 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:first\">")); |
179 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:second\">")); | 177 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:second\">")); |
180 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:third\">")); | 178 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:third\">")); |
181 } | 179 } |
182 | 180 |
183 TEST(ViewCacheHelper, TruncatedFlag) { | 181 TEST(ViewCacheHelper, TruncatedFlag) { |
184 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); | 182 TestURLRequestContext context; |
185 ViewCacheHelper helper; | 183 ViewCacheHelper helper; |
186 | 184 |
187 net::TestCompletionCallback cb; | 185 net::TestCompletionCallback cb; |
188 disk_cache::Backend* cache; | 186 disk_cache::Backend* cache; |
189 int rv = | 187 int rv = |
190 context->http_transaction_factory()->GetCache()->GetBackend( | 188 context.http_transaction_factory()->GetCache()->GetBackend( |
191 &cache, cb.callback()); | 189 &cache, cb.callback()); |
192 ASSERT_EQ(OK, cb.GetResult(rv)); | 190 ASSERT_EQ(OK, cb.GetResult(rv)); |
193 | 191 |
194 std::string key("the key"); | 192 std::string key("the key"); |
195 disk_cache::Entry* entry; | 193 disk_cache::Entry* entry; |
196 rv = cache->CreateEntry(key, &entry, cb.callback()); | 194 rv = cache->CreateEntry(key, &entry, cb.callback()); |
197 ASSERT_EQ(OK, cb.GetResult(rv)); | 195 ASSERT_EQ(OK, cb.GetResult(rv)); |
198 | 196 |
199 // RESPONSE_INFO_TRUNCATED defined on response_info.cc | 197 // RESPONSE_INFO_TRUNCATED defined on response_info.cc |
200 int flags = 1 << 12; | 198 int flags = 1 << 12; |
201 WriteHeaders(entry, flags, "something"); | 199 WriteHeaders(entry, flags, "something"); |
202 entry->Close(); | 200 entry->Close(); |
203 | 201 |
204 std::string data; | 202 std::string data; |
205 TestCompletionCallback cb1; | 203 TestCompletionCallback cb1; |
206 rv = helper.GetEntryInfoHTML(key, context, &data, cb1.callback()); | 204 rv = helper.GetEntryInfoHTML(key, &context, &data, cb1.callback()); |
207 EXPECT_EQ(OK, cb1.GetResult(rv)); | 205 EXPECT_EQ(OK, cb1.GetResult(rv)); |
208 | 206 |
209 EXPECT_NE(std::string::npos, data.find("RESPONSE_INFO_TRUNCATED")); | 207 EXPECT_NE(std::string::npos, data.find("RESPONSE_INFO_TRUNCATED")); |
210 } | 208 } |
211 | 209 |
212 } // namespace net | 210 } // namespace net |
OLD | NEW |