proxygen
HTTPDefaultSessionCodecFactoryTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
17 
18 using namespace proxygen;
19 using namespace testing;
20 
21 TEST(HTTPDefaultSessionCodecFactoryTest, GetCodecSPDY) {
23  // If set directly on the acceptor, we should always return the SPDY version.
24  conf.plaintextProtocol = "spdy/3.1";
25 
26  HTTPDefaultSessionCodecFactory factory(conf);
27  auto codec = factory.getCodec(
28  "http/1.1", TransportDirection::UPSTREAM, false /* isTLS */);
29  SPDYCodec* spdyCodec = dynamic_cast<SPDYCodec*>(codec.get());
30  EXPECT_NE(spdyCodec, nullptr);
32 
33  codec = factory.getCodec(
34  "spdy/3", TransportDirection::UPSTREAM, false /* isTLS */);
35  spdyCodec = dynamic_cast<SPDYCodec*>(codec.get());
36  EXPECT_NE(spdyCodec, nullptr);
38 
39  conf.plaintextProtocol = "spdy/3";
40 
41  HTTPDefaultSessionCodecFactory secondFactory(conf);
42  codec = secondFactory.getCodec(
43  "http/1.1", TransportDirection::UPSTREAM, false /* isTLS */);
44  spdyCodec = dynamic_cast<SPDYCodec*>(codec.get());
45  EXPECT_NE(spdyCodec, nullptr);
47 
48  codec = secondFactory.getCodec(
49  "spdy/3.1", TransportDirection::UPSTREAM, false /* isTLS */);
50  spdyCodec = dynamic_cast<SPDYCodec*>(codec.get());
51  EXPECT_NE(spdyCodec, nullptr);
53 
54  // On a somewhat contrived example, if TLS we should return the version
55  // negotiated through ALPN.
56  codec = secondFactory.getCodec(
57  "h2", TransportDirection::DOWNSTREAM, true /* isTLS */);
58  HTTP2Codec* httpCodec = dynamic_cast<HTTP2Codec*>(codec.get());
59  EXPECT_NE(httpCodec, nullptr);
61 }
62 
63 TEST(HTTPDefaultSessionCodecFactoryTest, GetCodecH2) {
65  // If set directly on the acceptor, we should always return the H2C version.
66  conf.plaintextProtocol = "h2c";
67  HTTPDefaultSessionCodecFactory factory(conf);
68  auto codec = factory.getCodec(
69  "http/1.1", TransportDirection::DOWNSTREAM, false /* isTLS */);
70  HTTP2Codec* httpCodec = dynamic_cast<HTTP2Codec*>(codec.get());
71  EXPECT_NE(httpCodec, nullptr);
73 
74  // On a somewhat contrived example, if TLS we should return the version
75  // negotiated through ALPN.
76  codec = factory.getCodec(
77  "http/1.1", TransportDirection::UPSTREAM, true /* isTLS */);
78  HTTP1xCodec* http1Codec = dynamic_cast<HTTP1xCodec*>(codec.get());
79  EXPECT_NE(http1Codec, nullptr);
81 }
82 
83 TEST(HTTPDefaultSessionCodecFactoryTest, GetCodecUpgradeProtocols) {
84  std::list<std::string> plainTextUpgrades = {http2::kProtocolCleartextString};
86  conf.allowedPlaintextUpgradeProtocols = plainTextUpgrades;
87  HTTPDefaultSessionCodecFactory factory(conf);
88 
89  auto codec =
90  factory.getCodec("http/1.1", TransportDirection::DOWNSTREAM, false);
91  HTTP1xCodec* downstreamCodec = dynamic_cast<HTTP1xCodec*>(codec.get());
92  EXPECT_NE(downstreamCodec, nullptr);
93  EXPECT_FALSE(downstreamCodec->getAllowedUpgradeProtocols().empty());
94  EXPECT_EQ(downstreamCodec->getAllowedUpgradeProtocols(),
96 
97  // If TLS, we should not attempt to upgrade.
98  codec = factory.getCodec("http/1.1", TransportDirection::DOWNSTREAM, true);
99  downstreamCodec = dynamic_cast<HTTP1xCodec*>(codec.get());
100  EXPECT_NE(downstreamCodec, nullptr);
101  EXPECT_TRUE(downstreamCodec->getAllowedUpgradeProtocols().empty());
102 }
103 
104 TEST(HTTPDefaultSessionCodecFactoryTest, GetCodec) {
106  HTTPDefaultSessionCodecFactory factory(conf);
107 
108  // Empty protocol should default to http/1.1
109  auto codec =
110  factory.getCodec("", TransportDirection::DOWNSTREAM, false /* isTLS */);
111  HTTP1xCodec* http1Codec = dynamic_cast<HTTP1xCodec*>(codec.get());
112  EXPECT_NE(http1Codec, nullptr);
114 
115  codec = factory.getCodec(
116  "spdy/3.1", TransportDirection::UPSTREAM, false /* isTLS */);
117  SPDYCodec* spdyCodec = dynamic_cast<SPDYCodec*>(codec.get());
118  EXPECT_NE(spdyCodec, nullptr);
120 
121  codec =
122  factory.getCodec("h2", TransportDirection::DOWNSTREAM, false /* isTLS */);
123  HTTP2Codec* httpCodec = dynamic_cast<HTTP2Codec*>(codec.get());
124  EXPECT_NE(httpCodec, nullptr);
126 
127  // Not supported protocols should return nullptr.
128  codec = factory.getCodec(
129  "not/supported", TransportDirection::DOWNSTREAM, false /* isTLS */);
130  EXPECT_EQ(codec, nullptr);
131 }
const std::string & getAllowedUpgradeProtocols()
const std::string kProtocolCleartextString
CodecProtocol getProtocol() const override
Definition: HTTP1xCodec.h:28
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
CodecFactory codec
std::list< std::string > allowedPlaintextUpgradeProtocols
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::unique_ptr< HTTPCodec > getCodec(const std::string &nextProtocol, TransportDirection direction, bool isTLS) override
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
CodecProtocol getProtocol() const override
Definition: HTTP2Codec.h:40
TEST(HTTPDefaultSessionCodecFactoryTest, GetCodecSPDY)
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
CodecProtocol getProtocol() const override
Definition: SPDYCodec.cpp:197