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 ~MemoryCacheWithFakeReadToString() override
{}
18 void ReadToString(const char* filename
, std::string
* output
) override
{
19 *output
= data_map_
[filename
];
22 std::map
<std::string
, std::string
> data_map_
;
25 class FlipMemoryCacheTest
: public ::testing::Test
{
27 FlipMemoryCacheTest() : mem_cache_(new MemoryCacheWithFakeReadToString
) {}
30 scoped_ptr
<MemoryCacheWithFakeReadToString
> mem_cache_
;
33 TEST_F(FlipMemoryCacheTest
, EmptyCache
) {
36 ASSERT_EQ(NULL
, mem_cache_
->GetFileData("./foo"));
37 ASSERT_EQ(NULL
, mem_cache_
->GetFileData("./bar"));
38 ASSERT_FALSE(mem_cache_
->AssignFileData("./hello", &mci
));
41 TEST_F(FlipMemoryCacheTest
, ReadAndStoreFileContents
) {
45 mem_cache_
->data_map_
["./foo"] = "bar";
46 mem_cache_
->data_map_
["./hello"] =
49 "key2: value2\r\n\r\n"
51 mem_cache_
->ReadAndStoreFileContents("./foo");
52 mem_cache_
->ReadAndStoreFileContents("./hello");
54 foo
= mem_cache_
->GetFileData("foo");
55 hello
= mem_cache_
->GetFileData("hello");
57 // "./foo" content is broken.
59 ASSERT_FALSE(NULL
== hello
);
60 ASSERT_EQ(hello
, mem_cache_
->GetFileData("hello"));
62 // "HTTP/1.0" is rewritten to "HTTP/1.1".
63 ASSERT_EQ("HTTP/1.1", hello
->headers()->response_version());
64 ASSERT_EQ("200", hello
->headers()->response_code());
65 ASSERT_EQ("OK", hello
->headers()->response_reason_phrase());
67 std::distance(hello
->headers()->header_lines_begin(),
68 hello
->headers()->header_lines_end()));
69 ASSERT_TRUE(hello
->headers()->HasHeader("key1"));
70 ASSERT_TRUE(hello
->headers()->HasHeader("key2"));
71 ASSERT_TRUE(hello
->headers()->HasHeader("transfer-encoding"));
72 ASSERT_TRUE(hello
->headers()->HasHeader("connection"));
73 ASSERT_EQ("value1", hello
->headers()->GetHeaderPosition("key1")->second
);
74 ASSERT_EQ("value2", hello
->headers()->GetHeaderPosition("key2")->second
);
76 hello
->headers()->GetHeaderPosition("transfer-encoding")->second
);
77 ASSERT_EQ("keep-alive",
78 hello
->headers()->GetHeaderPosition("connection")->second
);
79 ASSERT_EQ("body: body\r\n", hello
->body());
80 ASSERT_EQ("hello", hello
->filename());
83 TEST_F(FlipMemoryCacheTest
, GetFileDataForHtmlFile
) {
86 mem_cache_
->data_map_
["./hello.http"] =
89 "key2: value2\r\n\r\n"
92 mem_cache_
->ReadAndStoreFileContents("./hello.http");
93 hello_html
= mem_cache_
->GetFileData("hello.html");
94 ASSERT_FALSE(NULL
== hello_html
);
95 ASSERT_EQ(hello_html
, mem_cache_
->GetFileData("hello.http"));