Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: content/browser/loader/netlog_observer_unittest.cc

Issue 2715743007: Adding QUIC request headers to the net log observer (Closed)
Patch Set: csharrison nits Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/loader/netlog_observer.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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
5 #include <memory>
6
7 #include "base/macros.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/values.h"
11 #include "content/browser/loader/netlog_observer.h"
12 #include "content/browser/loader/resource_request_info_impl.h"
13 #include "content/browser/loader/resource_requester_info.h"
14 #include "content/public/common/previews_state.h"
15 #include "content/public/common/resource_type.h"
16 #include "content/public/test/mock_resource_context.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "net/base/request_priority.h"
19 #include "net/log/net_log.h"
20 #include "net/log/net_log_capture_mode.h"
21 #include "net/log/net_log_event_type.h"
22 #include "net/quic/core/quic_types.h"
23 #include "net/spdy/spdy_header_block.h"
24 #include "net/spdy/spdy_protocol.h"
25 #include "net/url_request/url_request_context.h"
26 #include "net/url_request/url_request_netlog_params.h"
27 #include "net/url_request/url_request_test_util.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "third_party/WebKit/public/platform/WebPageVisibilityState.h"
30 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h"
31 #include "ui/base/page_transition_types.h"
32 #include "url/gurl.h"
33
34 namespace content {
35
36 namespace {
37
38 const char kDefaultURL[] = "https://example.test";
39
40 std::unique_ptr<base::Value> QuicRequestCallback(
41 net::QuicStreamId stream_id,
42 const net::SpdyHeaderBlock* headers,
43 net::SpdyPriority priority,
44 net::NetLogCaptureMode capture_mode) {
45 std::unique_ptr<base::DictionaryValue> dict(
46 static_cast<base::DictionaryValue*>(
47 SpdyHeaderBlockNetLogCallback(headers, capture_mode).release()));
48 return std::move(dict);
49 }
50
51 } // namespace
52
53 class NetLogObserverTest : public testing::Test {
54 public:
55 NetLogObserverTest()
56 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), context_(true) {}
57
58 ~NetLogObserverTest() override {}
59
60 void SetUp() override {
61 context_.Init();
62 context_.set_net_log(&net_log_);
63 NetLogObserver::Attach(context_.net_log());
64 request_ = context_.CreateRequest(GURL(kDefaultURL), net::DEFAULT_PRIORITY,
65 nullptr);
66 resource_context_ = base::MakeUnique<MockResourceContext>(&context_);
67 requester_info_ = ResourceRequesterInfo::CreateForRendererTesting(1);
68 ResourceRequestInfoImpl* info = new ResourceRequestInfoImpl(
69 requester_info_,
70 0, // route_id
71 -1, // frame_tree_node_id
72 0, // origin_pid
73 0, // request_id
74 0, // render_frame_id
75 false, // is_main_frame
76 false, // parent_is_main_frame
77 RESOURCE_TYPE_IMAGE, // resource_type
78 ui::PAGE_TRANSITION_LINK, // transition_type
79 false, // should_replace_current_entry
80 false, // is_download
81 false, // is_stream
82 false, // allow_download
83 false, // has_user_gesture
84 false, // enable load timing
85 false, // enable upload progress
86 false, // do_not_prompt_for_login
87 blink::WebReferrerPolicyDefault, // referrer_policy
88 blink::WebPageVisibilityStateVisible, // visibility_state
89 resource_context_.get(), // context
90 true, // report_raw_headers
91 true, // is_async
92 PREVIEWS_OFF, // previews_state
93 std::string(), // original_headers
94 nullptr, // body
95 false); // initiated_in_secure_context
96 info->AssociateWithRequest(request_.get());
97 std::string method = "GET";
98 GURL url(kDefaultURL);
99 request_->net_log().BeginEvent(
100 net::NetLogEventType::URL_REQUEST_START_JOB,
101 base::Bind(&net::NetLogURLRequestStartCallback, &url, &method, 0, -1));
102 }
103
104 void TearDown() override { NetLogObserver::Detach(); }
105
106 void VerifyHeaderValue(const std::string& header,
107 const std::string& expected_value) {
108 scoped_refptr<ResourceResponse> response(new ResourceResponse);
109
110 NetLogObserver::PopulateResponseInfo(request_.get(), response.get());
111
112 int header_was_in_list = false;
113 for (auto header_pair : response->head.devtools_info->request_headers) {
114 if (header == header_pair.first) {
115 EXPECT_EQ(expected_value, header_pair.second);
116 header_was_in_list = true;
117 }
118 }
119 EXPECT_TRUE(header_was_in_list);
120 }
121
122 protected:
123 net::NetLog net_log_;
124 TestBrowserThreadBundle thread_bundle_;
125 net::TestURLRequestContext context_;
126 std::unique_ptr<MockResourceContext> resource_context_;
127 scoped_refptr<ResourceRequesterInfo> requester_info_;
128 std::unique_ptr<net::URLRequest> request_;
129
130 private:
131 DISALLOW_COPY_AND_ASSIGN(NetLogObserverTest);
132 };
133
134 // Verify that the response info is populated with headers for HTTP jobs.
135 TEST_F(NetLogObserverTest, TestHTTPEntry) {
136 std::string header("header");
137 std::string value("value");
138 std::string request_line("HTTP request line");
139
140 net::HttpRequestHeaders request_headers;
141 request_headers.SetHeader(header, value);
142
143 request_->net_log().AddEvent(
144 net::NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST_HEADERS,
145 base::Bind(&net::HttpRequestHeaders::NetLogCallback,
146 base::Unretained(&request_headers), &request_line));
147
148 VerifyHeaderValue(header, value);
149 }
150
151 // Verify that the response info is populated with headers for HTTP2 jobs.
152 TEST_F(NetLogObserverTest, TestHTTP2Entry) {
153 std::string header("header");
154 std::string value("value");
155 net::SpdyHeaderBlock request_headers;
156
157 request_headers[header] = value;
158 request_->net_log().AddEvent(
159 net::NetLogEventType::HTTP_TRANSACTION_HTTP2_SEND_REQUEST_HEADERS,
160 base::Bind(&net::SpdyHeaderBlockNetLogCallback, &request_headers));
161
162 VerifyHeaderValue(header, value);
163 }
164
165 // Verify that the response info is populated with headers for QUIC jobs.
166 TEST_F(NetLogObserverTest, TestQUICEntry) {
167 std::string header("header");
168 std::string value("value");
169 net::SpdyHeaderBlock request_headers;
170
171 request_headers[header] = value;
172 request_->net_log().AddEvent(
173 net::NetLogEventType::HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS,
174 base::Bind(&QuicRequestCallback, 1, &request_headers,
175 net::DEFAULT_PRIORITY));
176
177 VerifyHeaderValue(header, value);
178 }
179
180 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/netlog_observer.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698