1 // Copyright (c) 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.
5 #include "net/spdy/spdy_websocket_test_util.h"
7 #include "net/spdy/buffered_spdy_framer.h"
8 #include "net/spdy/spdy_http_utils.h"
12 static const int kDefaultAssociatedStreamId
= 0;
13 static const bool kDefaultCompressed
= false;
14 static const char* const kDefaultDataPointer
= NULL
;
15 static const uint32 kDefaultDataLength
= 0;
16 static const char** const kDefaultExtraHeaders
= NULL
;
17 static const int kDefaultExtraHeaderCount
= 0;
19 SpdyWebSocketTestUtil::SpdyWebSocketTestUtil(
20 NextProto protocol
) : spdy_util_(protocol
) {}
22 std::string
SpdyWebSocketTestUtil::GetHeader(const SpdyHeaderBlock
& headers
,
23 const std::string
& key
) const {
24 SpdyHeaderBlock::const_iterator it
= headers
.find(GetHeaderKey(key
));
25 return (it
== headers
.end()) ? "" : it
->second
;
28 void SpdyWebSocketTestUtil::SetHeader(
29 const std::string
& key
,
30 const std::string
& value
,
31 SpdyHeaderBlock
* headers
) const {
32 (*headers
)[GetHeaderKey(key
)] = value
;
35 SpdyFrame
* SpdyWebSocketTestUtil::ConstructSpdyWebSocketSynStream(
40 scoped_ptr
<SpdyHeaderBlock
> headers(new SpdyHeaderBlock());
41 SetHeader("path", path
, headers
.get());
42 SetHeader("host", host
, headers
.get());
43 SetHeader("version", "WebSocket/13", headers
.get());
44 SetHeader("scheme", "ws", headers
.get());
45 SetHeader("origin", origin
, headers
.get());
46 return spdy_util_
.ConstructSpdyControlFrame(headers
.Pass(),
55 SpdyFrame
* SpdyWebSocketTestUtil::ConstructSpdyWebSocketSynReply(
57 scoped_ptr
<SpdyHeaderBlock
> headers(new SpdyHeaderBlock());
58 SetHeader("status", "101", headers
.get());
59 return spdy_util_
.ConstructSpdyControlFrame(headers
.Pass(),
68 SpdyFrame
* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHandshakeRequestFrame(
69 scoped_ptr
<SpdyHeaderBlock
> headers
,
70 SpdyStreamId stream_id
,
71 RequestPriority request_priority
) {
72 // SPDY SYN_STREAM control frame header.
73 const SpdyHeaderInfo kSynStreamHeader
= {
76 kDefaultAssociatedStreamId
,
77 ConvertRequestPriorityToSpdyPriority(request_priority
, 2),
78 kSpdyCredentialSlotUnused
,
87 // Construct SPDY SYN_STREAM control frame.
88 return spdy_util_
.ConstructSpdyFrame(
93 SpdyFrame
* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHandshakeResponseFrame(
94 scoped_ptr
<SpdyHeaderBlock
> headers
,
95 SpdyStreamId stream_id
,
96 RequestPriority request_priority
) {
97 // SPDY SYN_REPLY control frame header.
98 const SpdyHeaderInfo kSynReplyHeader
= {
101 kDefaultAssociatedStreamId
,
102 ConvertRequestPriorityToSpdyPriority(request_priority
, 2),
103 kSpdyCredentialSlotUnused
,
112 // Construct SPDY SYN_REPLY control frame.
113 return spdy_util_
.ConstructSpdyFrame(
118 SpdyFrame
* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHeadersFrame(
122 scoped_ptr
<SpdyHeaderBlock
> headers(new SpdyHeaderBlock());
123 SetHeader("opcode", "1", headers
.get()); // text frame
124 SetHeader("length", length
, headers
.get());
125 SetHeader("fin", fin
? "1" : "0", headers
.get());
126 return spdy_util_
.ConstructSpdyControlFrame(headers
.Pass(),
127 /*compression*/ false,
135 SpdyFrame
* SpdyWebSocketTestUtil::ConstructSpdyWebSocketDataFrame(
138 SpdyStreamId stream_id
,
141 // Construct SPDY data frame.
142 BufferedSpdyFramer
framer(spdy_util_
.spdy_version(), false);
143 return framer
.CreateDataFrame(
147 fin
? DATA_FLAG_FIN
: DATA_FLAG_NONE
);
150 SpdyFrame
* SpdyWebSocketTestUtil::ConstructSpdySettings(
151 const SettingsMap
& settings
) const {
152 return spdy_util_
.ConstructSpdySettings(settings
);
155 SpdyMajorVersion
SpdyWebSocketTestUtil::spdy_version() const {
156 return spdy_util_
.spdy_version();
159 std::string
SpdyWebSocketTestUtil::GetHeaderKey(
160 const std::string
& key
) const {
161 return (spdy_util_
.is_spdy2() ? "" : ":") + key
;