http_basic_state_unittest.cc 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/http/http_basic_state.h"
  5. #include "base/memory/ptr_util.h"
  6. #include "net/base/request_priority.h"
  7. #include "net/http/http_request_info.h"
  8. #include "net/log/net_log_with_source.h"
  9. #include "net/socket/client_socket_handle.h"
  10. #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace net {
  13. namespace {
  14. TEST(HttpBasicStateTest, ConstructsProperly) {
  15. auto handle = std::make_unique<ClientSocketHandle>();
  16. ClientSocketHandle* const handle_ptr = handle.get();
  17. // Ownership of |handle| is passed to |state|.
  18. const HttpBasicState state(std::move(handle), true /* using_proxy */);
  19. EXPECT_EQ(handle_ptr, state.connection());
  20. EXPECT_TRUE(state.using_proxy());
  21. }
  22. TEST(HttpBasicStateTest, ConstructsProperlyWithDifferentOptions) {
  23. const HttpBasicState state(std::make_unique<ClientSocketHandle>(),
  24. false /* using_proxy */);
  25. EXPECT_FALSE(state.using_proxy());
  26. }
  27. TEST(HttpBasicStateTest, ReleaseConnectionWorks) {
  28. auto handle = std::make_unique<ClientSocketHandle>();
  29. ClientSocketHandle* const handle_ptr = handle.get();
  30. // Ownership of |handle| is passed to |state|.
  31. HttpBasicState state(std::move(handle), false);
  32. const std::unique_ptr<ClientSocketHandle> released_connection(
  33. state.ReleaseConnection());
  34. EXPECT_EQ(nullptr, state.connection());
  35. EXPECT_EQ(handle_ptr, released_connection.get());
  36. }
  37. TEST(HttpBasicStateTest, InitializeWorks) {
  38. HttpBasicState state(std::make_unique<ClientSocketHandle>(), false);
  39. const HttpRequestInfo request_info;
  40. state.Initialize(&request_info, LOW, NetLogWithSource());
  41. EXPECT_TRUE(state.parser());
  42. }
  43. TEST(HttpBasicStateTest, TrafficAnnotationStored) {
  44. HttpBasicState state(std::make_unique<ClientSocketHandle>(), false);
  45. HttpRequestInfo request_info;
  46. request_info.traffic_annotation =
  47. MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS);
  48. state.Initialize(&request_info, LOW, NetLogWithSource());
  49. EXPECT_EQ(TRAFFIC_ANNOTATION_FOR_TESTS,
  50. NetworkTrafficAnnotationTag(state.traffic_annotation()));
  51. }
  52. TEST(HttpBasicStateTest, DeleteParser) {
  53. HttpBasicState state(std::make_unique<ClientSocketHandle>(), false);
  54. const HttpRequestInfo request_info;
  55. state.Initialize(&request_info, LOW, NetLogWithSource());
  56. EXPECT_TRUE(state.parser());
  57. state.DeleteParser();
  58. EXPECT_EQ(nullptr, state.parser());
  59. }
  60. TEST(HttpBasicStateTest, GenerateRequestLineNoProxy) {
  61. const bool use_proxy = false;
  62. HttpBasicState state(std::make_unique<ClientSocketHandle>(), use_proxy);
  63. HttpRequestInfo request_info;
  64. request_info.url = GURL("http://www.example.com/path?foo=bar#hoge");
  65. request_info.method = "PUT";
  66. state.Initialize(&request_info, LOW, NetLogWithSource());
  67. EXPECT_EQ("PUT /path?foo=bar HTTP/1.1\r\n", state.GenerateRequestLine());
  68. }
  69. TEST(HttpBasicStateTest, GenerateRequestLineWithProxy) {
  70. const bool use_proxy = true;
  71. HttpBasicState state(std::make_unique<ClientSocketHandle>(), use_proxy);
  72. HttpRequestInfo request_info;
  73. request_info.url = GURL("http://www.example.com/path?foo=bar#hoge");
  74. request_info.method = "PUT";
  75. state.Initialize(&request_info, LOW, NetLogWithSource());
  76. EXPECT_EQ("PUT http://www.example.com/path?foo=bar HTTP/1.1\r\n",
  77. state.GenerateRequestLine());
  78. }
  79. } // namespace
  80. } // namespace net