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), true /* using_proxy */);
23 EXPECT_EQ(handle_ptr, state.connection());
24 EXPECT_TRUE(state.using_proxy());
25 }
26
TEST(HttpBasicStateTest,ConstructsProperlyWithDifferentOptions)27 TEST(HttpBasicStateTest, ConstructsProperlyWithDifferentOptions) {
28 const HttpBasicState state(std::make_unique<ClientSocketHandle>(),
29 false /* using_proxy */);
30 EXPECT_FALSE(state.using_proxy());
31 }
32
TEST(HttpBasicStateTest,ReleaseConnectionWorks)33 TEST(HttpBasicStateTest, ReleaseConnectionWorks) {
34 auto handle = std::make_unique<ClientSocketHandle>();
35 ClientSocketHandle* const handle_ptr = handle.get();
36 // Ownership of |handle| is passed to |state|.
37 HttpBasicState state(std::move(handle), false);
38 const std::unique_ptr<ClientSocketHandle> released_connection(
39 state.ReleaseConnection());
40 EXPECT_EQ(nullptr, state.connection());
41 EXPECT_EQ(handle_ptr, released_connection.get());
42 }
43
TEST(HttpBasicStateTest,InitializeWorks)44 TEST(HttpBasicStateTest, InitializeWorks) {
45 HttpBasicState state(std::make_unique<ClientSocketHandle>(), false);
46 const HttpRequestInfo request_info;
47 state.Initialize(&request_info, LOW, NetLogWithSource());
48 EXPECT_TRUE(state.parser());
49 }
50
TEST(HttpBasicStateTest,TrafficAnnotationStored)51 TEST(HttpBasicStateTest, TrafficAnnotationStored) {
52 HttpBasicState state(std::make_unique<ClientSocketHandle>(), false);
53 HttpRequestInfo request_info;
54 request_info.traffic_annotation =
55 MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS);
56 state.Initialize(&request_info, LOW, NetLogWithSource());
57 EXPECT_EQ(TRAFFIC_ANNOTATION_FOR_TESTS,
58 NetworkTrafficAnnotationTag(state.traffic_annotation()));
59 }
60
TEST(HttpBasicStateTest,DeleteParser)61 TEST(HttpBasicStateTest, DeleteParser) {
62 HttpBasicState state(std::make_unique<ClientSocketHandle>(), false);
63 const HttpRequestInfo request_info;
64 state.Initialize(&request_info, LOW, NetLogWithSource());
65 EXPECT_TRUE(state.parser());
66 state.DeleteParser();
67 EXPECT_EQ(nullptr, state.parser());
68 }
69
TEST(HttpBasicStateTest,GenerateRequestLineNoProxy)70 TEST(HttpBasicStateTest, GenerateRequestLineNoProxy) {
71 const bool use_proxy = false;
72 HttpBasicState state(std::make_unique<ClientSocketHandle>(), use_proxy);
73 HttpRequestInfo request_info;
74 request_info.url = GURL("http://www.example.com/path?foo=bar#hoge");
75 request_info.method = "PUT";
76 state.Initialize(&request_info, LOW, NetLogWithSource());
77 EXPECT_EQ("PUT /path?foo=bar HTTP/1.1\r\n", state.GenerateRequestLine());
78 }
79
TEST(HttpBasicStateTest,GenerateRequestLineWithProxy)80 TEST(HttpBasicStateTest, GenerateRequestLineWithProxy) {
81 const bool use_proxy = true;
82 HttpBasicState state(std::make_unique<ClientSocketHandle>(), use_proxy);
83 HttpRequestInfo request_info;
84 request_info.url = GURL("http://www.example.com/path?foo=bar#hoge");
85 request_info.method = "PUT";
86 state.Initialize(&request_info, LOW, NetLogWithSource());
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
93