1 // Copyright 2013 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.
5 #include "net/tools/flip_server/mem_cache.h"
7 #include "net/tools/balsa/balsa_headers.h"
8 #include "testing/gtest/include/gtest/gtest.h"
14 class MemoryCacheWithFakeReadToString
: public MemoryCache
{
16 virtual ~MemoryCacheWithFakeReadToString() {}
18 virtual void ReadToString(const char* filename
,
19 std::string
* output
) override
{
20 *output
= data_map_
[filename
];
23 std::map
<std::string
, std::string
> data_map_
;
26 class FlipMemoryCacheTest
: public ::testing::Test
{
28 FlipMemoryCacheTest() : mem_cache_(new MemoryCacheWithFakeReadToString
) {}
31 scoped_ptr
<MemoryCacheWithFakeReadToString
> mem_cache_
;
34 TEST_F(FlipMemoryCacheTest
, EmptyCache
) {
37 ASSERT_EQ(NULL
, mem_cache_
->GetFileData("./foo"));
38 ASSERT_EQ(NULL
, mem_cache_
->GetFileData("./bar"));
39 ASSERT_FALSE(mem_cache_
->AssignFileData("./hello", &mci
));
42 TEST_F(FlipMemoryCacheTest
, ReadAndStoreFileContents
) {
46 mem_cache_
->data_map_
["./foo"] = "bar";
47 mem_cache_
->data_map_
["./hello"] =
50 "key2: value2\r\n\r\n"
52 mem_cache_
->ReadAndStoreFileContents("./foo");
53 mem_cache_
->ReadAndStoreFileContents("./hello");
55 foo
= mem_cache_
->GetFileData("foo");
56 hello
= mem_cache_
->GetFileData("hello");
58 // "./foo" content is broken.
60 ASSERT_FALSE(NULL
== hello
);
61 ASSERT_EQ(hello
, mem_cache_
->GetFileData("hello"));
63 // "HTTP/1.0" is rewritten to "HTTP/1.1".
64 ASSERT_EQ("HTTP/1.1", hello
->headers()->response_version());
65 ASSERT_EQ("200", hello
->headers()->response_code());
66 ASSERT_EQ("OK", hello
->headers()->response_reason_phrase());
68 std::distance(hello
->headers()->header_lines_begin(),
69 hello
->headers()->header_lines_end()));
70 ASSERT_TRUE(hello
->headers()->HasHeader("key1"));
71 ASSERT_TRUE(hello
->headers()->HasHeader("key2"));
72 ASSERT_TRUE(hello
->headers()->HasHeader("transfer-encoding"));
73 ASSERT_TRUE(hello
->headers()->HasHeader("connection"));
74 ASSERT_EQ("value1", hello
->headers()->GetHeaderPosition("key1")->second
);
75 ASSERT_EQ("value2", hello
->headers()->GetHeaderPosition("key2")->second
);
77 hello
->headers()->GetHeaderPosition("transfer-encoding")->second
);
78 ASSERT_EQ("keep-alive",
79 hello
->headers()->GetHeaderPosition("connection")->second
);
80 ASSERT_EQ("body: body\r\n", hello
->body());
81 ASSERT_EQ("hello", hello
->filename());
84 TEST_F(FlipMemoryCacheTest
, GetFileDataForHtmlFile
) {
87 mem_cache_
->data_map_
["./hello.http"] =
90 "key2: value2\r\n\r\n"
93 mem_cache_
->ReadAndStoreFileContents("./hello.http");
94 hello_html
= mem_cache_
->GetFileData("hello.html");
95 ASSERT_FALSE(NULL
== hello_html
);
96 ASSERT_EQ(hello_html
, mem_cache_
->GetFileData("hello.http"));