1 // Copyright 2013 The Chromium Authors
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
7 #include "base/memory/ptr_util.h"
8 #include "net/base/request_priority.h"
9 #include "net/http/http_request_info.h"
10 #include "net/log/net_log_with_source.h"
11 #include "net/socket/client_socket_handle.h"
12 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace net {
16 namespace {
17
TEST(HttpBasicStateTest,ConstructsProperly)18 TEST(HttpBasicStateTest, ConstructsProperly) {
19 auto handle = std::make_unique<ClientSocketHandle>();
20 ClientSocketHandle* const handle_ptr = handle.get();
21 // Ownership of |handle| is passed to |state|.
22 const HttpBasicState state(std::move(handle),
23 true /* is_for_get_to_http_proxy */);
24 EXPECT_EQ(handle_ptr, state.connection());
25 EXPECT_TRUE(state.is_for_get_to_http_proxy());
26 }
27
TEST(HttpBasicStateTest,ConstructsProperlyWithDifferentOptions)28 TEST(HttpBasicStateTest, ConstructsProperlyWithDifferentOptions) {
29 const HttpBasicState state(std::make_unique<ClientSocketHandle>(),
30 false /* is_for_get_to_http_proxy */);
31 EXPECT_FALSE(state.is_for_get_to_http_proxy());
32 }
33
TEST(HttpBasicStateTest,ReleaseConnectionWorks)34 TEST(HttpBasicStateTest, ReleaseConnectionWorks) {
35 auto handle = std::make_unique<ClientSocketHandle>();
36 ClientSocketHandle* const handle_ptr = handle.get();
37 // Ownership of |handle| is passed to |state|.
38 HttpBasicState state(std::move(handle), false);
39 const std::unique_ptr<ClientSocketHandle> released_connection(
40 state.ReleaseConnection());
41 EXPECT_EQ(nullptr, state.connection());
42 EXPECT_EQ(handle_ptr, released_connection.get());
43 }
44
TEST(HttpBasicStateTest,InitializeWorks)45 TEST(HttpBasicStateTest, InitializeWorks) {
46 HttpBasicState state(std::make_unique<ClientSocketHandle>(), false);
47 const HttpRequestInfo request_info;
48 state.Initialize(&request_info, LOW, NetLogWithSource());
49 EXPECT_TRUE(state.parser());
50 }
51
TEST(HttpBasicStateTest,TrafficAnnotationStored)52 TEST(HttpBasicStateTest, TrafficAnnotationStored) {
53 HttpBasicState state(std::make_unique<ClientSocketHandle>(), false);
54 HttpRequestInfo request_info;
55 request_info.traffic_annotation =
56 MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS);
57 state.Initialize(&request_info, LOW, NetLogWithSource());
58 EXPECT_EQ(TRAFFIC_ANNOTATION_FOR_TESTS,
59 NetworkTrafficAnnotationTag(state.traffic_annotation()));
60 }
61
TEST(HttpBasicStateTest,DeleteParser)62 TEST(HttpBasicStateTest, DeleteParser) {
63 HttpBasicState state(std::make_unique<ClientSocketHandle>(), false);
64 const HttpRequestInfo request_info;
65 state.Initialize(&request_info, LOW, NetLogWithSource());
66 EXPECT_TRUE(state.parser());
67 state.DeleteParser();
68 EXPECT_EQ(nullptr, state.parser());
69 }
70
TEST(HttpBasicStateTest,GenerateRequestLineNoProxy)71 TEST(HttpBasicStateTest, GenerateRequestLineNoProxy) {
72 const bool use_proxy = false;
73 HttpBasicState state(std::make_unique<ClientSocketHandle>(), use_proxy);
74 HttpRequestInfo request_info;
75 request_info.url = GURL("http://www.example.com/path?foo=bar#hoge");
76 request_info.method = "PUT";
77 state.Initialize(&request_info, LOW, NetLogWithSource());
78 EXPECT_EQ("PUT /path?foo=bar HTTP/1.1\r\n", state.GenerateRequestLine());
79 }
80
TEST(HttpBasicStateTest,GenerateRequestLineWithProxy)81 TEST(HttpBasicStateTest, GenerateRequestLineWithProxy) {
82 const bool use_proxy = true;
83 HttpBasicState state(std::make_unique<ClientSocketHandle>(), use_proxy);
84 HttpRequestInfo request_info;
85 request_info.url = GURL("http://www.example.com/path?foo=bar#hoge");
86 request_info.method = "PUT";
87 state.Initialize(&request_info, LOW, NetLogWithSource());
88 EXPECT_EQ("PUT http://www.example.com/path?foo=bar HTTP/1.1\r\n",
89 state.GenerateRequestLine());
90 }
91
92 } // namespace
93 } // namespace net
94