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