summaryrefslogtreecommitdiffstats
path: root/net/tools/flip_server/mem_cache_test.cc
blob: 59bc485bc67707e8e946aa9bac1fc057dcdf2deb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/tools/flip_server/mem_cache.h"

#include "net/tools/balsa/balsa_headers.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

namespace {

class MemoryCacheWithFakeReadToString : public MemoryCache {
 public:
  virtual ~MemoryCacheWithFakeReadToString() {}

  virtual void ReadToString(const char* filename, std::string* output)
      OVERRIDE {
    *output = data_map_[filename];
  }

  std::map<std::string, std::string> data_map_;
};

class FlipMemoryCacheTest : public ::testing::Test {
 public:
  FlipMemoryCacheTest(): mem_cache_(new MemoryCacheWithFakeReadToString) {}

 protected:
  scoped_ptr<MemoryCacheWithFakeReadToString> mem_cache_;
};

TEST_F(FlipMemoryCacheTest, EmptyCache) {
  MemCacheIter mci;
  mci.stream_id = 0;
  ASSERT_EQ(NULL, mem_cache_->GetFileData("./foo"));
  ASSERT_EQ(NULL, mem_cache_->GetFileData("./bar"));
  ASSERT_FALSE(mem_cache_->AssignFileData("./hello", &mci));
}

TEST_F(FlipMemoryCacheTest, ReadAndStoreFileContents) {
  FileData* foo;
  FileData* hello;

  mem_cache_->data_map_["./foo"] = "bar";
  mem_cache_->data_map_["./hello"] = "HTTP/1.0 200 OK\r\n"
      "key1: value1\r\n"
      "key2: value2\r\n\r\n"
      "body: body\r\n";
  mem_cache_->ReadAndStoreFileContents("./foo");
  mem_cache_->ReadAndStoreFileContents("./hello");

  foo = mem_cache_->GetFileData("foo");
  hello = mem_cache_->GetFileData("hello");

  // "./foo" content is broken.
  ASSERT_EQ(NULL, foo);
  ASSERT_FALSE(NULL == hello);
  ASSERT_EQ(hello, mem_cache_->GetFileData("hello"));

  // "HTTP/1.0" is rewritten to "HTTP/1.1".
  ASSERT_EQ("HTTP/1.1", hello->headers()->response_version());
  ASSERT_EQ("200", hello->headers()->response_code());
  ASSERT_EQ("OK", hello->headers()->response_reason_phrase());
  ASSERT_EQ(4, std::distance(hello->headers()->header_lines_begin(),
                             hello->headers()->header_lines_end()));
  ASSERT_TRUE(hello->headers()->HasHeader("key1"));
  ASSERT_TRUE(hello->headers()->HasHeader("key2"));
  ASSERT_TRUE(hello->headers()->HasHeader("transfer-encoding"));
  ASSERT_TRUE(hello->headers()->HasHeader("connection"));
  ASSERT_EQ("value1", hello->headers()->GetHeaderPosition("key1")->second);
  ASSERT_EQ("value2", hello->headers()->GetHeaderPosition("key2")->second);
  ASSERT_EQ("chunked",
            hello->headers()->GetHeaderPosition("transfer-encoding")->second);
  ASSERT_EQ("keep-alive",
            hello->headers()->GetHeaderPosition("connection")->second);
  ASSERT_EQ("body: body\r\n", hello->body());
  ASSERT_EQ("hello", hello->filename());
}

TEST_F(FlipMemoryCacheTest, GetFileDataForHtmlFile) {
  FileData* hello_html;

  mem_cache_->data_map_["./hello.http"] = "HTTP/1.0 200 OK\r\n"
      "key1: value1\r\n"
      "key2: value2\r\n\r\n"
      "body: body\r\n";

  mem_cache_->ReadAndStoreFileContents("./hello.http");
  hello_html = mem_cache_->GetFileData("hello.html");
  ASSERT_FALSE(NULL == hello_html);
  ASSERT_EQ(hello_html, mem_cache_->GetFileData("hello.http"));
}

}  // namespace

}  // namespace net