| OLD | NEW | 
|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/tools/flip_server/mem_cache.h" | 5 #include "net/tools/flip_server/mem_cache.h" | 
| 6 | 6 | 
| 7 #include "net/tools/balsa/balsa_headers.h" | 7 #include "net/tools/balsa/balsa_headers.h" | 
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" | 
| 9 | 9 | 
| 10 namespace net { | 10 namespace net { | 
| 11 | 11 | 
| 12 namespace { | 12 namespace { | 
| 13 | 13 | 
| 14 class MemoryCacheWithFakeReadToString : public MemoryCache { | 14 class MemoryCacheWithFakeReadToString : public MemoryCache { | 
| 15  public: | 15  public: | 
| 16   virtual ~MemoryCacheWithFakeReadToString() {} | 16   virtual ~MemoryCacheWithFakeReadToString() {} | 
| 17 | 17 | 
| 18   virtual void ReadToString(const char* filename, std::string* output) | 18   virtual void ReadToString(const char* filename, | 
| 19       OVERRIDE { | 19                             std::string* output) OVERRIDE { | 
| 20     *output = data_map_[filename]; | 20     *output = data_map_[filename]; | 
| 21   } | 21   } | 
| 22 | 22 | 
| 23   std::map<std::string, std::string> data_map_; | 23   std::map<std::string, std::string> data_map_; | 
| 24 }; | 24 }; | 
| 25 | 25 | 
| 26 class FlipMemoryCacheTest : public ::testing::Test { | 26 class FlipMemoryCacheTest : public ::testing::Test { | 
| 27  public: | 27  public: | 
| 28   FlipMemoryCacheTest(): mem_cache_(new MemoryCacheWithFakeReadToString) {} | 28   FlipMemoryCacheTest() : mem_cache_(new MemoryCacheWithFakeReadToString) {} | 
| 29 | 29 | 
| 30  protected: | 30  protected: | 
| 31   scoped_ptr<MemoryCacheWithFakeReadToString> mem_cache_; | 31   scoped_ptr<MemoryCacheWithFakeReadToString> mem_cache_; | 
| 32 }; | 32 }; | 
| 33 | 33 | 
| 34 TEST_F(FlipMemoryCacheTest, EmptyCache) { | 34 TEST_F(FlipMemoryCacheTest, EmptyCache) { | 
| 35   MemCacheIter mci; | 35   MemCacheIter mci; | 
| 36   mci.stream_id = 0; | 36   mci.stream_id = 0; | 
| 37   ASSERT_EQ(NULL, mem_cache_->GetFileData("./foo")); | 37   ASSERT_EQ(NULL, mem_cache_->GetFileData("./foo")); | 
| 38   ASSERT_EQ(NULL, mem_cache_->GetFileData("./bar")); | 38   ASSERT_EQ(NULL, mem_cache_->GetFileData("./bar")); | 
| 39   ASSERT_FALSE(mem_cache_->AssignFileData("./hello", &mci)); | 39   ASSERT_FALSE(mem_cache_->AssignFileData("./hello", &mci)); | 
| 40 } | 40 } | 
| 41 | 41 | 
| 42 TEST_F(FlipMemoryCacheTest, ReadAndStoreFileContents) { | 42 TEST_F(FlipMemoryCacheTest, ReadAndStoreFileContents) { | 
| 43   FileData* foo; | 43   FileData* foo; | 
| 44   FileData* hello; | 44   FileData* hello; | 
| 45 | 45 | 
| 46   mem_cache_->data_map_["./foo"] = "bar"; | 46   mem_cache_->data_map_["./foo"] = "bar"; | 
| 47   mem_cache_->data_map_["./hello"] = "HTTP/1.0 200 OK\r\n" | 47   mem_cache_->data_map_["./hello"] = | 
|  | 48       "HTTP/1.0 200 OK\r\n" | 
| 48       "key1: value1\r\n" | 49       "key1: value1\r\n" | 
| 49       "key2: value2\r\n\r\n" | 50       "key2: value2\r\n\r\n" | 
| 50       "body: body\r\n"; | 51       "body: body\r\n"; | 
| 51   mem_cache_->ReadAndStoreFileContents("./foo"); | 52   mem_cache_->ReadAndStoreFileContents("./foo"); | 
| 52   mem_cache_->ReadAndStoreFileContents("./hello"); | 53   mem_cache_->ReadAndStoreFileContents("./hello"); | 
| 53 | 54 | 
| 54   foo = mem_cache_->GetFileData("foo"); | 55   foo = mem_cache_->GetFileData("foo"); | 
| 55   hello = mem_cache_->GetFileData("hello"); | 56   hello = mem_cache_->GetFileData("hello"); | 
| 56 | 57 | 
| 57   // "./foo" content is broken. | 58   // "./foo" content is broken. | 
| 58   ASSERT_EQ(NULL, foo); | 59   ASSERT_EQ(NULL, foo); | 
| 59   ASSERT_FALSE(NULL == hello); | 60   ASSERT_FALSE(NULL == hello); | 
| 60   ASSERT_EQ(hello, mem_cache_->GetFileData("hello")); | 61   ASSERT_EQ(hello, mem_cache_->GetFileData("hello")); | 
| 61 | 62 | 
| 62   // "HTTP/1.0" is rewritten to "HTTP/1.1". | 63   // "HTTP/1.0" is rewritten to "HTTP/1.1". | 
| 63   ASSERT_EQ("HTTP/1.1", hello->headers()->response_version()); | 64   ASSERT_EQ("HTTP/1.1", hello->headers()->response_version()); | 
| 64   ASSERT_EQ("200", hello->headers()->response_code()); | 65   ASSERT_EQ("200", hello->headers()->response_code()); | 
| 65   ASSERT_EQ("OK", hello->headers()->response_reason_phrase()); | 66   ASSERT_EQ("OK", hello->headers()->response_reason_phrase()); | 
| 66   ASSERT_EQ(4, std::distance(hello->headers()->header_lines_begin(), | 67   ASSERT_EQ(4, | 
| 67                              hello->headers()->header_lines_end())); | 68             std::distance(hello->headers()->header_lines_begin(), | 
|  | 69                           hello->headers()->header_lines_end())); | 
| 68   ASSERT_TRUE(hello->headers()->HasHeader("key1")); | 70   ASSERT_TRUE(hello->headers()->HasHeader("key1")); | 
| 69   ASSERT_TRUE(hello->headers()->HasHeader("key2")); | 71   ASSERT_TRUE(hello->headers()->HasHeader("key2")); | 
| 70   ASSERT_TRUE(hello->headers()->HasHeader("transfer-encoding")); | 72   ASSERT_TRUE(hello->headers()->HasHeader("transfer-encoding")); | 
| 71   ASSERT_TRUE(hello->headers()->HasHeader("connection")); | 73   ASSERT_TRUE(hello->headers()->HasHeader("connection")); | 
| 72   ASSERT_EQ("value1", hello->headers()->GetHeaderPosition("key1")->second); | 74   ASSERT_EQ("value1", hello->headers()->GetHeaderPosition("key1")->second); | 
| 73   ASSERT_EQ("value2", hello->headers()->GetHeaderPosition("key2")->second); | 75   ASSERT_EQ("value2", hello->headers()->GetHeaderPosition("key2")->second); | 
| 74   ASSERT_EQ("chunked", | 76   ASSERT_EQ("chunked", | 
| 75             hello->headers()->GetHeaderPosition("transfer-encoding")->second); | 77             hello->headers()->GetHeaderPosition("transfer-encoding")->second); | 
| 76   ASSERT_EQ("keep-alive", | 78   ASSERT_EQ("keep-alive", | 
| 77             hello->headers()->GetHeaderPosition("connection")->second); | 79             hello->headers()->GetHeaderPosition("connection")->second); | 
| 78   ASSERT_EQ("body: body\r\n", hello->body()); | 80   ASSERT_EQ("body: body\r\n", hello->body()); | 
| 79   ASSERT_EQ("hello", hello->filename()); | 81   ASSERT_EQ("hello", hello->filename()); | 
| 80 } | 82 } | 
| 81 | 83 | 
| 82 TEST_F(FlipMemoryCacheTest, GetFileDataForHtmlFile) { | 84 TEST_F(FlipMemoryCacheTest, GetFileDataForHtmlFile) { | 
| 83   FileData* hello_html; | 85   FileData* hello_html; | 
| 84 | 86 | 
| 85   mem_cache_->data_map_["./hello.http"] = "HTTP/1.0 200 OK\r\n" | 87   mem_cache_->data_map_["./hello.http"] = | 
|  | 88       "HTTP/1.0 200 OK\r\n" | 
| 86       "key1: value1\r\n" | 89       "key1: value1\r\n" | 
| 87       "key2: value2\r\n\r\n" | 90       "key2: value2\r\n\r\n" | 
| 88       "body: body\r\n"; | 91       "body: body\r\n"; | 
| 89 | 92 | 
| 90   mem_cache_->ReadAndStoreFileContents("./hello.http"); | 93   mem_cache_->ReadAndStoreFileContents("./hello.http"); | 
| 91   hello_html = mem_cache_->GetFileData("hello.html"); | 94   hello_html = mem_cache_->GetFileData("hello.html"); | 
| 92   ASSERT_FALSE(NULL == hello_html); | 95   ASSERT_FALSE(NULL == hello_html); | 
| 93   ASSERT_EQ(hello_html, mem_cache_->GetFileData("hello.http")); | 96   ASSERT_EQ(hello_html, mem_cache_->GetFileData("hello.http")); | 
| 94 } | 97 } | 
| 95 | 98 | 
| 96 }  // namespace | 99 }  // namespace | 
| 97 | 100 | 
| 98 }  // namespace net | 101 }  // namespace net | 
| OLD | NEW | 
|---|