OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/spdy/spdy_websocket_stream.h" | 5 #include "net/spdy/spdy_websocket_stream.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "net/base/completion_callback.h" | 10 #include "net/base/completion_callback.h" |
11 #include "net/proxy/proxy_server.h" | 11 #include "net/proxy/proxy_server.h" |
12 #include "net/spdy/spdy_http_utils.h" | 12 #include "net/spdy/spdy_http_utils.h" |
13 #include "net/spdy/spdy_protocol.h" | 13 #include "net/spdy/spdy_protocol.h" |
14 #include "net/spdy/spdy_session.h" | 14 #include "net/spdy/spdy_session.h" |
15 #include "net/spdy/spdy_test_util.h" | 15 #include "net/spdy/spdy_test_util.h" |
| 16 #include "net/spdy/spdy_websocket_test_util.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 spdy::SpdyFrame* ConstructSpdyWebSocketHandshakeReq( | |
21 const char* const url, | |
22 const char* const origin, | |
23 const char* const protocol, | |
24 bool compressed, | |
25 spdy::SpdyStreamId stream_id, | |
26 net::RequestPriority request_priority) { | |
27 const net::SpdyHeaderInfo kSynStreamHeader = { | |
28 spdy::SYN_STREAM, | |
29 stream_id, | |
30 0, // Associated stream ID | |
31 net::ConvertRequestPriorityToSpdyPriority(request_priority), | |
32 spdy::CONTROL_FLAG_NONE, | |
33 compressed, | |
34 spdy::INVALID, // Status | |
35 NULL, // Data, | |
36 0, // Length | |
37 spdy::DATA_FLAG_NONE | |
38 }; | |
39 | |
40 const char* const headers[] = { | |
41 "url", | |
42 url, | |
43 "origin", | |
44 origin, | |
45 "protocol", | |
46 protocol, | |
47 }; | |
48 int header_size = arraysize(headers) / 2; | |
49 if (protocol == NULL) | |
50 header_size -= 1; | |
51 | |
52 return ConstructSpdyPacket( | |
53 kSynStreamHeader, | |
54 NULL, | |
55 0, | |
56 headers, | |
57 header_size); | |
58 } | |
59 | |
60 spdy::SpdyFrame* ConstructSpdyWebSocketHandshakeResp( | |
61 const char* const url, | |
62 const char* const origin, | |
63 const char* const protocol, | |
64 bool compressed, | |
65 spdy::SpdyStreamId stream_id, | |
66 net::RequestPriority request_priority) { | |
67 const net::SpdyHeaderInfo kSynReplyHeader = { | |
68 spdy::SYN_REPLY, | |
69 stream_id, | |
70 0, // Associated stream ID | |
71 net::ConvertRequestPriorityToSpdyPriority(request_priority), | |
72 spdy::CONTROL_FLAG_NONE, | |
73 false, | |
74 spdy::INVALID, // Status | |
75 NULL, // Data | |
76 0, // Length | |
77 spdy::DATA_FLAG_NONE | |
78 }; | |
79 | |
80 const char* const headers[] = { | |
81 "sec-websocket-location", | |
82 url, | |
83 "sec-websocket-origin", | |
84 origin, | |
85 "sec-websocket-protocol", | |
86 protocol, | |
87 }; | |
88 int header_size = arraysize(headers) / 2; | |
89 if (protocol == NULL) | |
90 header_size -= 1; | |
91 | |
92 return ConstructSpdyPacket( | |
93 kSynReplyHeader, | |
94 NULL, | |
95 0, | |
96 headers, | |
97 header_size); | |
98 } | |
99 | |
100 spdy::SpdyFrame* ConstructSpdyWebSocketFrame( | |
101 const char* data, | |
102 int len, | |
103 spdy::SpdyStreamId stream_id, | |
104 bool fin) { | |
105 spdy::SpdyFramer framer; | |
106 return framer.CreateDataFrame( | |
107 stream_id, data, len, | |
108 fin ? spdy::DATA_FLAG_FIN : spdy::DATA_FLAG_NONE); | |
109 } | |
110 | |
111 struct SpdyWebSocketStreamEvent { | 21 struct SpdyWebSocketStreamEvent { |
112 enum EventType { | 22 enum EventType { |
113 EVENT_CREATED, | 23 EVENT_CREATED, |
114 EVENT_SENT_HEADERS, | 24 EVENT_SENT_HEADERS, |
115 EVENT_RECEIVED_HEADER, | 25 EVENT_RECEIVED_HEADER, |
116 EVENT_SENT_DATA, | 26 EVENT_SENT_DATA, |
117 EVENT_RECEIVED_DATA, | 27 EVENT_RECEIVED_DATA, |
118 EVENT_CLOSE, | 28 EVENT_CLOSE, |
119 }; | 29 }; |
120 SpdyWebSocketStreamEvent(EventType type, | 30 SpdyWebSocketStreamEvent(EventType type, |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 virtual void TearDown() { | 201 virtual void TearDown() { |
292 MessageLoop::current()->RunAllPending(); | 202 MessageLoop::current()->RunAllPending(); |
293 } | 203 } |
294 | 204 |
295 void EnableCompression(bool enabled) { | 205 void EnableCompression(bool enabled) { |
296 spdy::SpdyFramer::set_enable_compression_default(enabled); | 206 spdy::SpdyFramer::set_enable_compression_default(enabled); |
297 } | 207 } |
298 void Prepare(spdy::SpdyStreamId stream_id) { | 208 void Prepare(spdy::SpdyStreamId stream_id) { |
299 stream_id_ = stream_id; | 209 stream_id_ = stream_id; |
300 | 210 |
301 request_frame_.reset(ConstructSpdyWebSocketHandshakeReq( | 211 const char* const request_headers[] = { |
302 "ws://example.com/echo", | 212 "url", "ws://example.com/echo", |
303 "http://example.com/wsdemo", | 213 "origin", "http://example.com/wsdemo", |
304 NULL, | 214 }; |
305 false, | 215 |
| 216 int request_header_count = arraysize(request_headers) / 2; |
| 217 |
| 218 const char* const response_headers[] = { |
| 219 "sec-websocket-location", "ws://example.com/echo", |
| 220 "sec-websocket-origin", "http://example.com/wsdemo", |
| 221 }; |
| 222 |
| 223 int response_header_count = arraysize(response_headers) / 2; |
| 224 |
| 225 request_frame_.reset(ConstructSpdyWebSocketHandshakeRequestFrame( |
| 226 request_headers, |
| 227 request_header_count, |
306 stream_id_, | 228 stream_id_, |
307 HIGHEST)); | 229 HIGHEST)); |
308 response_frame_.reset(ConstructSpdyWebSocketHandshakeResp( | 230 response_frame_.reset(ConstructSpdyWebSocketHandshakeResponseFrame( |
309 "ws://example.com/echo", | 231 response_headers, |
310 "http://example.com/wsdemo", | 232 response_header_count, |
311 NULL, | |
312 false, | |
313 stream_id_, | 233 stream_id_, |
314 HIGHEST)); | 234 HIGHEST)); |
315 | 235 |
316 message_frame_.reset(ConstructSpdyWebSocketFrame( | 236 message_frame_.reset(ConstructSpdyWebSocketDataFrame( |
317 kMessageFrame, kMessageFrameLength, stream_id_, false)); | 237 kMessageFrame, |
| 238 kMessageFrameLength, |
| 239 stream_id_, |
| 240 false)); |
318 | 241 |
319 closing_frame_.reset(ConstructSpdyWebSocketFrame( | 242 closing_frame_.reset(ConstructSpdyWebSocketDataFrame( |
320 kClosingFrame, kClosingFrameLength, stream_id_, false)); | 243 kClosingFrame, |
| 244 kClosingFrameLength, |
| 245 stream_id_, |
| 246 false)); |
321 } | 247 } |
322 int InitSession(MockRead* reads, size_t reads_count, | 248 int InitSession(MockRead* reads, size_t reads_count, |
323 MockWrite* writes, size_t writes_count, | 249 MockWrite* writes, size_t writes_count, |
324 bool throttling) { | 250 bool throttling) { |
325 data_ = new OrderedSocketData(reads, reads_count, writes, writes_count); | 251 data_ = new OrderedSocketData(reads, reads_count, writes, writes_count); |
326 session_deps_.socket_factory->AddSocketDataProvider(data_.get()); | 252 session_deps_.socket_factory->AddSocketDataProvider(data_.get()); |
327 http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); | 253 http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); |
328 SpdySessionPool* spdy_session_pool(http_session_->spdy_session_pool()); | 254 SpdySessionPool* spdy_session_pool(http_session_->spdy_session_pool()); |
329 | 255 |
330 if (throttling) { | 256 if (throttling) { |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 EXPECT_EQ(OK, events[7].result); | 602 EXPECT_EQ(OK, events[7].result); |
677 | 603 |
678 // EOF close SPDY session. | 604 // EOF close SPDY session. |
679 EXPECT_TRUE(!http_session_->spdy_session_pool()->HasSession( | 605 EXPECT_TRUE(!http_session_->spdy_session_pool()->HasSession( |
680 host_port_proxy_pair_)); | 606 host_port_proxy_pair_)); |
681 EXPECT_TRUE(data()->at_read_eof()); | 607 EXPECT_TRUE(data()->at_read_eof()); |
682 EXPECT_TRUE(data()->at_write_eof()); | 608 EXPECT_TRUE(data()->at_write_eof()); |
683 } | 609 } |
684 | 610 |
685 } // namespace net | 611 } // namespace net |
OLD | NEW |