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<StreamSocketHandle> released_connection(
40 state.ReleaseConnection());
41 EXPECT_EQ(nullptr, state.parser());
42 EXPECT_EQ(nullptr, state.connection());
43 EXPECT_EQ(handle_ptr, released_connection.get());
44 }
45
TEST(HttpBasicStateTest,InitializeWorks)46 TEST(HttpBasicStateTest, InitializeWorks) {
47 HttpBasicState state(std::make_unique<ClientSocketHandle>(), false);
48 const HttpRequestInfo request_info;
49 state.Initialize(&request_info, LOW, NetLogWithSource());
50 EXPECT_TRUE(state.parser());
51 }
52
TEST(HttpBasicStateTest,TrafficAnnotationStored)53 TEST(HttpBasicStateTest, TrafficAnnotationStored) {
54 HttpBasicState state(std::make_unique<ClientSocketHandle>(), false);
55 HttpRequestInfo request_info;
56 request_info.traffic_annotation =
57 MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS);
58 state.Initialize(&request_info, LOW, NetLogWithSource());
59 EXPECT_EQ(TRAFFIC_ANNOTATION_FOR_TESTS,
60 NetworkTrafficAnnotationTag(state.traffic_annotation()));
61 }
62
TEST(HttpBasicStateTest,GenerateRequestLineNoProxy)63 TEST(HttpBasicStateTest, GenerateRequestLineNoProxy) {
64 const bool use_proxy = false;
65 HttpBasicState state(std::make_unique<ClientSocketHandle>(), use_proxy);
66 HttpRequestInfo request_info;
67 request_info.url = GURL("http://www.example.com/path?foo=bar#hoge");
68 request_info.method = "PUT";
69 state.Initialize(&request_info, LOW, NetLogWithSource());
70 EXPECT_EQ("PUT /path?foo=bar HTTP/1.1\r\n", state.GenerateRequestLine());
71 }
72
TEST(HttpBasicStateTest,GenerateRequestLineWithProxy)73 TEST(HttpBasicStateTest, GenerateRequestLineWithProxy) {
74 const bool use_proxy = true;
75 HttpBasicState state(std::make_unique<ClientSocketHandle>(), use_proxy);
76 HttpRequestInfo request_info;
77 request_info.url = GURL("http://www.example.com/path?foo=bar#hoge");
78 request_info.method = "PUT";
79 state.Initialize(&request_info, LOW, NetLogWithSource());
80 EXPECT_EQ("PUT http://www.example.com/path?foo=bar HTTP/1.1\r\n",
81 state.GenerateRequestLine());
82 }
83
84 } // namespace
85 } // namespace net
86