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. 4 5 #include "net/tools/flip_server/mem_cache.h" 6 7 #include "net/tools/balsa/balsa_headers.h" 8 #include "testing/gtest/include/gtest/gtest.h" 9 10 namespace net { 11 12 namespace { 13 14 class MemoryCacheWithFakeReadToString : public MemoryCache { 15 public: 16 virtual ~MemoryCacheWithFakeReadToString() {} 17 18 virtual void ReadToString(const char* filename, 19 std::string* output) OVERRIDE { 20 *output = data_map_[filename]; 21 } 22 23 std::map<std::string, std::string> data_map_; 24 }; 25 26 class FlipMemoryCacheTest : public ::testing::Test { 27 public: 28 FlipMemoryCacheTest() : mem_cache_(new MemoryCacheWithFakeReadToString) {} 29 30 protected: 31 scoped_ptr<MemoryCacheWithFakeReadToString> mem_cache_; 32 }; 33 34 TEST_F(FlipMemoryCacheTest, EmptyCache) { 35 MemCacheIter mci; 36 mci.stream_id = 0; 37 ASSERT_EQ(NULL, mem_cache_->GetFileData("./foo")); 38 ASSERT_EQ(NULL, mem_cache_->GetFileData("./bar")); 39 ASSERT_FALSE(mem_cache_->AssignFileData("./hello", &mci)); 40 } 41 42 TEST_F(FlipMemoryCacheTest, ReadAndStoreFileContents) { 43 FileData* foo; 44 FileData* hello; 45 46 mem_cache_->data_map_["./foo"] = "bar"; 47 mem_cache_->data_map_["./hello"] = 48 "HTTP/1.0 200 OK\r\n" 49 "key1: value1\r\n" 50 "key2: value2\r\n\r\n" 51 "body: body\r\n"; 52 mem_cache_->ReadAndStoreFileContents("./foo"); 53 mem_cache_->ReadAndStoreFileContents("./hello"); 54 55 foo = mem_cache_->GetFileData("foo"); 56 hello = mem_cache_->GetFileData("hello"); 57 58 // "./foo" content is broken. 59 ASSERT_EQ(NULL, foo); 60 ASSERT_FALSE(NULL == hello); 61 ASSERT_EQ(hello, mem_cache_->GetFileData("hello")); 62 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()); 67 ASSERT_EQ(4, 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); 76 ASSERT_EQ("chunked", 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()); 82 } 83 84 TEST_F(FlipMemoryCacheTest, GetFileDataForHtmlFile) { 85 FileData* hello_html; 86 87 mem_cache_->data_map_["./hello.http"] = 88 "HTTP/1.0 200 OK\r\n" 89 "key1: value1\r\n" 90 "key2: value2\r\n\r\n" 91 "body: body\r\n"; 92 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")); 97 } 98 99 } // namespace 100 101 } // namespace net 102