1 // Copyright (c) 2010 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 // HttpAlternateProtocols is an in-memory data structure used for keeping track 6 // of which HTTP HostPortPairs have an alternate protocol that can be used 7 // instead of HTTP on a different port. 8 9 #include "net/http/http_alternate_protocols.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 namespace net { 13 namespace { 14 15 TEST(HttpAlternateProtocols, Basic) { 16 HttpAlternateProtocols alternate_protocols; 17 HostPortPair test_host_port_pair("foo", 80); 18 EXPECT_FALSE( 19 alternate_protocols.HasAlternateProtocolFor(test_host_port_pair)); 20 alternate_protocols.SetAlternateProtocolFor( 21 test_host_port_pair, 443, HttpAlternateProtocols::NPN_SPDY_1); 22 ASSERT_TRUE(alternate_protocols.HasAlternateProtocolFor(test_host_port_pair)); 23 const HttpAlternateProtocols::PortProtocolPair alternate = 24 alternate_protocols.GetAlternateProtocolFor(test_host_port_pair); 25 EXPECT_EQ(443, alternate.port); 26 EXPECT_EQ(HttpAlternateProtocols::NPN_SPDY_1, alternate.protocol); 27 } 28 29 TEST(HttpAlternateProtocols, SetBroken) { 30 HttpAlternateProtocols alternate_protocols; 31 HostPortPair test_host_port_pair("foo", 80); 32 alternate_protocols.MarkBrokenAlternateProtocolFor(test_host_port_pair); 33 ASSERT_TRUE(alternate_protocols.HasAlternateProtocolFor(test_host_port_pair)); 34 HttpAlternateProtocols::PortProtocolPair alternate = 35 alternate_protocols.GetAlternateProtocolFor(test_host_port_pair); 36 EXPECT_EQ(HttpAlternateProtocols::BROKEN, alternate.protocol); 37 38 alternate_protocols.SetAlternateProtocolFor( 39 test_host_port_pair, 40 1234, 41 HttpAlternateProtocols::NPN_SPDY_1); 42 alternate = alternate_protocols.GetAlternateProtocolFor(test_host_port_pair); 43 EXPECT_EQ(HttpAlternateProtocols::BROKEN, alternate.protocol) 44 << "Second attempt should be ignored."; 45 } 46 47 TEST(HttpAlternateProtocols, Forced) { 48 // Test forced alternate protocols. 49 50 HttpAlternateProtocols::PortProtocolPair default_protocol; 51 default_protocol.port = 1234; 52 default_protocol.protocol = HttpAlternateProtocols::NPN_SPDY_2; 53 HttpAlternateProtocols::ForceAlternateProtocol(default_protocol); 54 55 HttpAlternateProtocols alternate_protocols; 56 57 // Verify the forced protocol. 58 HostPortPair test_host_port_pair("foo", 80); 59 EXPECT_TRUE( 60 alternate_protocols.HasAlternateProtocolFor(test_host_port_pair)); 61 HttpAlternateProtocols::PortProtocolPair alternate = 62 alternate_protocols.GetAlternateProtocolFor(test_host_port_pair); 63 EXPECT_EQ(default_protocol.port, alternate.port); 64 EXPECT_EQ(default_protocol.protocol, alternate.protocol); 65 66 // Verify the real protocol overrides the forced protocol. 67 alternate_protocols.SetAlternateProtocolFor( 68 test_host_port_pair, 443, HttpAlternateProtocols::NPN_SPDY_1); 69 ASSERT_TRUE(alternate_protocols.HasAlternateProtocolFor(test_host_port_pair)); 70 alternate = alternate_protocols.GetAlternateProtocolFor(test_host_port_pair); 71 EXPECT_EQ(443, alternate.port); 72 EXPECT_EQ(HttpAlternateProtocols::NPN_SPDY_1, alternate.protocol); 73 74 // Turn off the static, forced alternate protocol so that tests don't 75 // have this state. 76 HttpAlternateProtocols::DisableForcedAlternateProtocol(); 77 78 // Verify the forced protocol is off. 79 HostPortPair test_host_port_pair2("bar", 80); 80 EXPECT_FALSE( 81 alternate_protocols.HasAlternateProtocolFor(test_host_port_pair2)); 82 } 83 84 } // namespace 85 } // namespace net 86