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 const int kDefaultAssociatedStreamId
= 0;
13 const bool kDefaultCompressed
= false;
14 const char* const kDefaultDataPointer
= NULL
;
15 const uint32 kDefaultDataLength
= 0;
17 SpdyWebSocketTestUtil::SpdyWebSocketTestUtil(
18 NextProto protocol
) : spdy_util_(protocol
) {}
20 std::string
SpdyWebSocketTestUtil::GetHeader(const SpdyHeaderBlock
& headers
,
21 const std::string
& key
) const {
22 SpdyHeaderBlock::const_iterator it
= headers
.find(GetHeaderKey(key
));
23 return (it
== headers
.end()) ? "" : it
->second
;
26 void SpdyWebSocketTestUtil::SetHeader(
27 const std::string
& key
,
28 const std::string
& value
,
29 SpdyHeaderBlock
* headers
) const {
30 (*headers
)[GetHeaderKey(key
)] = value
;
33 SpdyFrame
* SpdyWebSocketTestUtil::ConstructSpdyWebSocketSynStream(
38 scoped_ptr
<SpdyHeaderBlock
> headers(new SpdyHeaderBlock());
39 SetHeader("path", path
, headers
.get());
40 SetHeader("host", host
, headers
.get());
41 SetHeader("version", "WebSocket/13", headers
.get());
42 SetHeader("scheme", "ws", headers
.get());
43 SetHeader("origin", origin
, headers
.get());
44 return spdy_util_
.ConstructSpdyControlFrame(headers
.Pass(),
53 SpdyFrame
* SpdyWebSocketTestUtil::ConstructSpdyWebSocketSynReply(
55 scoped_ptr
<SpdyHeaderBlock
> headers(new SpdyHeaderBlock());
56 SetHeader("status", "101", headers
.get());
57 return spdy_util_
.ConstructSpdyControlFrame(headers
.Pass(),
66 SpdyFrame
* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHandshakeRequestFrame(
67 scoped_ptr
<SpdyHeaderBlock
> headers
,
68 SpdyStreamId stream_id
,
69 RequestPriority request_priority
) {
70 // SPDY SYN_STREAM control frame header.
71 const SpdyHeaderInfo kSynStreamHeader
= {
74 kDefaultAssociatedStreamId
,
75 ConvertRequestPriorityToSpdyPriority(request_priority
, SPDY2
),
76 kSpdyCredentialSlotUnused
,
85 // Construct SPDY SYN_STREAM control frame.
86 return spdy_util_
.ConstructSpdyFrame(
91 SpdyFrame
* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHandshakeResponseFrame(
92 scoped_ptr
<SpdyHeaderBlock
> headers
,
93 SpdyStreamId stream_id
,
94 RequestPriority request_priority
) {
95 // SPDY SYN_REPLY control frame header.
96 const SpdyHeaderInfo kSynReplyHeader
= {
99 kDefaultAssociatedStreamId
,
100 ConvertRequestPriorityToSpdyPriority(request_priority
, SPDY2
),
101 kSpdyCredentialSlotUnused
,
110 // Construct SPDY SYN_REPLY control frame.
111 return spdy_util_
.ConstructSpdyFrame(
116 SpdyFrame
* SpdyWebSocketTestUtil::ConstructSpdyWebSocketHeadersFrame(
120 scoped_ptr
<SpdyHeaderBlock
> headers(new SpdyHeaderBlock());
121 SetHeader("opcode", "1", headers
.get()); // text frame
122 SetHeader("length", length
, headers
.get());
123 SetHeader("fin", fin
? "1" : "0", headers
.get());
124 return spdy_util_
.ConstructSpdyControlFrame(headers
.Pass(),
125 /*compression*/ false,
133 SpdyFrame
* SpdyWebSocketTestUtil::ConstructSpdyWebSocketDataFrame(
136 SpdyStreamId stream_id
,
139 // Construct SPDY data frame.
140 BufferedSpdyFramer
framer(spdy_util_
.spdy_version(), false);
141 return framer
.CreateDataFrame(
145 fin
? DATA_FLAG_FIN
: DATA_FLAG_NONE
);
148 SpdyFrame
* SpdyWebSocketTestUtil::ConstructSpdySettings(
149 const SettingsMap
& settings
) const {
150 return spdy_util_
.ConstructSpdySettings(settings
);
153 SpdyMajorVersion
SpdyWebSocketTestUtil::spdy_version() const {
154 return spdy_util_
.spdy_version();
157 std::string
SpdyWebSocketTestUtil::GetHeaderKey(
158 const std::string
& key
) const {
159 return (spdy_util_
.is_spdy2() ? "" : ":") + key
;