Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame^] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 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/http/http_basic_state.h" |
| 6 | |
bnc | 3faa41e | 2016-08-18 14:16:18 | [diff] [blame] | 7 | #include "base/memory/ptr_util.h" |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 8 | #include "net/base/request_priority.h" |
| 9 | #include "net/http/http_request_info.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 10 | #include "net/log/net_log_with_source.h" |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 11 | #include "net/socket/client_socket_handle.h" |
Ramin Halavati | 9e2c51a1 | 2018-02-14 07:56:20 | [diff] [blame] | 12 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
| 15 | namespace net { |
| 16 | namespace { |
| 17 | |
| 18 | TEST(HttpBasicStateTest, ConstructsProperly) { |
Tsuyoshi Horo | c39623a8 | 2022-07-11 01:27:58 | [diff] [blame] | 19 | auto handle = std::make_unique<ClientSocketHandle>(); |
| 20 | ClientSocketHandle* const handle_ptr = handle.get(); |
bnc | 3faa41e | 2016-08-18 14:16:18 | [diff] [blame] | 21 | // Ownership of |handle| is passed to |state|. |
Tsuyoshi Horo | c39623a8 | 2022-07-11 01:27:58 | [diff] [blame] | 22 | const HttpBasicState state(std::move(handle), true /* using_proxy */); |
| 23 | EXPECT_EQ(handle_ptr, state.connection()); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 24 | EXPECT_TRUE(state.using_proxy()); |
| 25 | } |
| 26 | |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 27 | TEST(HttpBasicStateTest, ConstructsProperlyWithDifferentOptions) { |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 28 | const HttpBasicState state(std::make_unique<ClientSocketHandle>(), |
Matt Menke | a31ed58 | 2019-09-19 20:14:11 | [diff] [blame] | 29 | false /* using_proxy */); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 30 | EXPECT_FALSE(state.using_proxy()); |
| 31 | } |
| 32 | |
| 33 | TEST(HttpBasicStateTest, ReleaseConnectionWorks) { |
Tsuyoshi Horo | c39623a8 | 2022-07-11 01:27:58 | [diff] [blame] | 34 | auto handle = std::make_unique<ClientSocketHandle>(); |
| 35 | ClientSocketHandle* const handle_ptr = handle.get(); |
bnc | 3faa41e | 2016-08-18 14:16:18 | [diff] [blame] | 36 | // Ownership of |handle| is passed to |state|. |
Tsuyoshi Horo | c39623a8 | 2022-07-11 01:27:58 | [diff] [blame] | 37 | HttpBasicState state(std::move(handle), false); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 38 | const std::unique_ptr<ClientSocketHandle> released_connection( |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 39 | state.ReleaseConnection()); |
Tsuyoshi Horo | 291961af | 2022-06-16 08:51:27 | [diff] [blame] | 40 | EXPECT_EQ(nullptr, state.connection()); |
Tsuyoshi Horo | c39623a8 | 2022-07-11 01:27:58 | [diff] [blame] | 41 | EXPECT_EQ(handle_ptr, released_connection.get()); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | TEST(HttpBasicStateTest, InitializeWorks) { |
Matt Menke | a31ed58 | 2019-09-19 20:14:11 | [diff] [blame] | 45 | HttpBasicState state(std::make_unique<ClientSocketHandle>(), false); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 46 | const HttpRequestInfo request_info; |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 47 | state.Initialize(&request_info, LOW, NetLogWithSource()); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 48 | EXPECT_TRUE(state.parser()); |
| 49 | } |
| 50 | |
Ramin Halavati | 9e2c51a1 | 2018-02-14 07:56:20 | [diff] [blame] | 51 | TEST(HttpBasicStateTest, TrafficAnnotationStored) { |
Matt Menke | a31ed58 | 2019-09-19 20:14:11 | [diff] [blame] | 52 | HttpBasicState state(std::make_unique<ClientSocketHandle>(), false); |
Ramin Halavati | 9e2c51a1 | 2018-02-14 07:56:20 | [diff] [blame] | 53 | HttpRequestInfo request_info; |
| 54 | request_info.traffic_annotation = |
| 55 | MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 56 | state.Initialize(&request_info, LOW, NetLogWithSource()); |
Ramin Halavati | 9e2c51a1 | 2018-02-14 07:56:20 | [diff] [blame] | 57 | EXPECT_EQ(TRAFFIC_ANNOTATION_FOR_TESTS, |
| 58 | NetworkTrafficAnnotationTag(state.traffic_annotation())); |
| 59 | } |
| 60 | |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 61 | TEST(HttpBasicStateTest, DeleteParser) { |
Matt Menke | a31ed58 | 2019-09-19 20:14:11 | [diff] [blame] | 62 | HttpBasicState state(std::make_unique<ClientSocketHandle>(), false); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 63 | const HttpRequestInfo request_info; |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 64 | state.Initialize(&request_info, LOW, NetLogWithSource()); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 65 | EXPECT_TRUE(state.parser()); |
| 66 | state.DeleteParser(); |
Tsuyoshi Horo | 291961af | 2022-06-16 08:51:27 | [diff] [blame] | 67 | EXPECT_EQ(nullptr, state.parser()); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | TEST(HttpBasicStateTest, GenerateRequestLineNoProxy) { |
| 71 | const bool use_proxy = false; |
Matt Menke | a31ed58 | 2019-09-19 20:14:11 | [diff] [blame] | 72 | HttpBasicState state(std::make_unique<ClientSocketHandle>(), use_proxy); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 73 | HttpRequestInfo request_info; |
| 74 | request_info.url = GURL("http://www.example.com/path?foo=bar#hoge"); |
| 75 | request_info.method = "PUT"; |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 76 | state.Initialize(&request_info, LOW, NetLogWithSource()); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 77 | EXPECT_EQ("PUT /path?foo=bar HTTP/1.1\r\n", state.GenerateRequestLine()); |
| 78 | } |
| 79 | |
| 80 | TEST(HttpBasicStateTest, GenerateRequestLineWithProxy) { |
| 81 | const bool use_proxy = true; |
Matt Menke | a31ed58 | 2019-09-19 20:14:11 | [diff] [blame] | 82 | HttpBasicState state(std::make_unique<ClientSocketHandle>(), use_proxy); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 83 | HttpRequestInfo request_info; |
| 84 | request_info.url = GURL("http://www.example.com/path?foo=bar#hoge"); |
| 85 | request_info.method = "PUT"; |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 86 | state.Initialize(&request_info, LOW, NetLogWithSource()); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 87 | EXPECT_EQ("PUT http://www.example.com/path?foo=bar HTTP/1.1\r\n", |
| 88 | state.GenerateRequestLine()); |
| 89 | } |
| 90 | |
| 91 | } // namespace |
| 92 | } // namespace net |