blob: d03e9dec699f56167a2e8be4d1f8472eecee6892 [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2013 The Chromium Authors
[email protected]f7c21732013-10-27 09:38:582// 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
bnc3faa41e2016-08-18 14:16:187#include "base/memory/ptr_util.h"
[email protected]f7c21732013-10-27 09:38:588#include "net/base/request_priority.h"
9#include "net/http/http_request_info.h"
mikecironef22f9812016-10-04 03:40:1910#include "net/log/net_log_with_source.h"
[email protected]f7c21732013-10-27 09:38:5811#include "net/socket/client_socket_handle.h"
Ramin Halavati9e2c51a12018-02-14 07:56:2012#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
[email protected]f7c21732013-10-27 09:38:5813#include "testing/gtest/include/gtest/gtest.h"
14
15namespace net {
16namespace {
17
18TEST(HttpBasicStateTest, ConstructsProperly) {
Tsuyoshi Horoc39623a82022-07-11 01:27:5819 auto handle = std::make_unique<ClientSocketHandle>();
20 ClientSocketHandle* const handle_ptr = handle.get();
bnc3faa41e2016-08-18 14:16:1821 // Ownership of |handle| is passed to |state|.
Tsuyoshi Horoc39623a82022-07-11 01:27:5822 const HttpBasicState state(std::move(handle), true /* using_proxy */);
23 EXPECT_EQ(handle_ptr, state.connection());
[email protected]f7c21732013-10-27 09:38:5824 EXPECT_TRUE(state.using_proxy());
25}
26
mmenkea7da6da2016-09-01 21:56:5227TEST(HttpBasicStateTest, ConstructsProperlyWithDifferentOptions) {
Jeremy Roman0579ed62017-08-29 15:56:1928 const HttpBasicState state(std::make_unique<ClientSocketHandle>(),
Matt Menkea31ed582019-09-19 20:14:1129 false /* using_proxy */);
[email protected]f7c21732013-10-27 09:38:5830 EXPECT_FALSE(state.using_proxy());
31}
32
33TEST(HttpBasicStateTest, ReleaseConnectionWorks) {
Tsuyoshi Horoc39623a82022-07-11 01:27:5834 auto handle = std::make_unique<ClientSocketHandle>();
35 ClientSocketHandle* const handle_ptr = handle.get();
bnc3faa41e2016-08-18 14:16:1836 // Ownership of |handle| is passed to |state|.
Tsuyoshi Horoc39623a82022-07-11 01:27:5837 HttpBasicState state(std::move(handle), false);
danakj1fd259a02016-04-16 03:17:0938 const std::unique_ptr<ClientSocketHandle> released_connection(
[email protected]f7c21732013-10-27 09:38:5839 state.ReleaseConnection());
Tsuyoshi Horo291961af2022-06-16 08:51:2740 EXPECT_EQ(nullptr, state.connection());
Tsuyoshi Horoc39623a82022-07-11 01:27:5841 EXPECT_EQ(handle_ptr, released_connection.get());
[email protected]f7c21732013-10-27 09:38:5842}
43
44TEST(HttpBasicStateTest, InitializeWorks) {
Matt Menkea31ed582019-09-19 20:14:1145 HttpBasicState state(std::make_unique<ClientSocketHandle>(), false);
[email protected]f7c21732013-10-27 09:38:5846 const HttpRequestInfo request_info;
Steven Valdez1c1859172019-04-10 15:33:2847 state.Initialize(&request_info, LOW, NetLogWithSource());
[email protected]f7c21732013-10-27 09:38:5848 EXPECT_TRUE(state.parser());
49}
50
Ramin Halavati9e2c51a12018-02-14 07:56:2051TEST(HttpBasicStateTest, TrafficAnnotationStored) {
Matt Menkea31ed582019-09-19 20:14:1152 HttpBasicState state(std::make_unique<ClientSocketHandle>(), false);
Ramin Halavati9e2c51a12018-02-14 07:56:2053 HttpRequestInfo request_info;
54 request_info.traffic_annotation =
55 MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS);
Steven Valdez1c1859172019-04-10 15:33:2856 state.Initialize(&request_info, LOW, NetLogWithSource());
Ramin Halavati9e2c51a12018-02-14 07:56:2057 EXPECT_EQ(TRAFFIC_ANNOTATION_FOR_TESTS,
58 NetworkTrafficAnnotationTag(state.traffic_annotation()));
59}
60
[email protected]f7c21732013-10-27 09:38:5861TEST(HttpBasicStateTest, DeleteParser) {
Matt Menkea31ed582019-09-19 20:14:1162 HttpBasicState state(std::make_unique<ClientSocketHandle>(), false);
[email protected]f7c21732013-10-27 09:38:5863 const HttpRequestInfo request_info;
Steven Valdez1c1859172019-04-10 15:33:2864 state.Initialize(&request_info, LOW, NetLogWithSource());
[email protected]f7c21732013-10-27 09:38:5865 EXPECT_TRUE(state.parser());
66 state.DeleteParser();
Tsuyoshi Horo291961af2022-06-16 08:51:2767 EXPECT_EQ(nullptr, state.parser());
[email protected]f7c21732013-10-27 09:38:5868}
69
70TEST(HttpBasicStateTest, GenerateRequestLineNoProxy) {
71 const bool use_proxy = false;
Matt Menkea31ed582019-09-19 20:14:1172 HttpBasicState state(std::make_unique<ClientSocketHandle>(), use_proxy);
[email protected]f7c21732013-10-27 09:38:5873 HttpRequestInfo request_info;
74 request_info.url = GURL("http://www.example.com/path?foo=bar#hoge");
75 request_info.method = "PUT";
Steven Valdez1c1859172019-04-10 15:33:2876 state.Initialize(&request_info, LOW, NetLogWithSource());
[email protected]f7c21732013-10-27 09:38:5877 EXPECT_EQ("PUT /path?foo=bar HTTP/1.1\r\n", state.GenerateRequestLine());
78}
79
80TEST(HttpBasicStateTest, GenerateRequestLineWithProxy) {
81 const bool use_proxy = true;
Matt Menkea31ed582019-09-19 20:14:1182 HttpBasicState state(std::make_unique<ClientSocketHandle>(), use_proxy);
[email protected]f7c21732013-10-27 09:38:5883 HttpRequestInfo request_info;
84 request_info.url = GURL("http://www.example.com/path?foo=bar#hoge");
85 request_info.method = "PUT";
Steven Valdez1c1859172019-04-10 15:33:2886 state.Initialize(&request_info, LOW, NetLogWithSource());
[email protected]f7c21732013-10-27 09:38:5887 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