blob: 46d33b6179876b1360ac49dd5f195c7f445beaa1 [file] [log] [blame]
QUICHE teama296f272021-04-06 12:54:53 -07001#include "http2/adapter/test_frame_sequence.h"
2
3#include "http2/adapter/http2_util.h"
4#include "spdy/core/spdy_framer.h"
5
6namespace http2 {
7namespace adapter {
8namespace test {
9
10TestFrameSequence& TestFrameSequence::ClientPreface() {
11 preface_ = spdy::kHttp2ConnectionHeaderPrefix;
12 frames_.push_back(absl::make_unique<spdy::SpdySettingsIR>());
13 return *this;
14}
15
16TestFrameSequence& TestFrameSequence::ServerPreface() {
17 frames_.push_back(absl::make_unique<spdy::SpdySettingsIR>());
18 return *this;
19}
20
21TestFrameSequence& TestFrameSequence::Data(Http2StreamId stream_id,
22 absl::string_view payload,
23 bool fin,
24 absl::optional<int> padding_length) {
25 auto data = absl::make_unique<spdy::SpdyDataIR>(stream_id, payload);
26 data->set_fin(fin);
27 if (padding_length) {
28 data->set_padding_len(padding_length.value());
29 }
30 frames_.push_back(std::move(data));
31 return *this;
32}
33
34TestFrameSequence& TestFrameSequence::RstStream(Http2StreamId stream_id,
35 Http2ErrorCode error) {
36 frames_.push_back(absl::make_unique<spdy::SpdyRstStreamIR>(
37 stream_id, TranslateErrorCode(error)));
38 return *this;
39}
40
41TestFrameSequence& TestFrameSequence::Settings(
42 absl::Span<Http2Setting> values) {
43 auto settings = absl::make_unique<spdy::SpdySettingsIR>();
44 for (const Http2Setting& setting : values) {
45 settings->AddSetting(setting.id, setting.value);
46 }
47 frames_.push_back(std::move(settings));
48 return *this;
49}
50
51TestFrameSequence& TestFrameSequence::SettingsAck() {
52 auto settings = absl::make_unique<spdy::SpdySettingsIR>();
53 settings->set_is_ack(true);
54 frames_.push_back(std::move(settings));
55 return *this;
56}
57
58TestFrameSequence& TestFrameSequence::Ping(Http2PingId id) {
59 frames_.push_back(absl::make_unique<spdy::SpdyPingIR>(id));
60 return *this;
61}
62
63TestFrameSequence& TestFrameSequence::PingAck(Http2PingId id) {
64 auto ping = absl::make_unique<spdy::SpdyPingIR>(id);
65 ping->set_is_ack(true);
66 frames_.push_back(std::move(ping));
67 return *this;
68}
69
70TestFrameSequence& TestFrameSequence::GoAway(Http2StreamId last_good_stream_id,
71 Http2ErrorCode error,
72 absl::string_view payload) {
73 frames_.push_back(absl::make_unique<spdy::SpdyGoAwayIR>(
74 last_good_stream_id, TranslateErrorCode(error), std::string(payload)));
75 return *this;
76}
77
78TestFrameSequence& TestFrameSequence::Headers(Http2StreamId stream_id,
79 spdy::Http2HeaderBlock block,
80 bool fin) {
81 auto headers =
82 absl::make_unique<spdy::SpdyHeadersIR>(stream_id, std::move(block));
83 headers->set_fin(fin);
84 frames_.push_back(std::move(headers));
85 return *this;
86}
87
88TestFrameSequence& TestFrameSequence::Headers(Http2StreamId stream_id,
89 absl::Span<const Header> headers,
90 bool fin) {
91 spdy::SpdyHeaderBlock block;
92 for (const Header& header : headers) {
93 block[header.first] = header.second;
94 }
95 return Headers(stream_id, std::move(block), fin);
96}
97
98TestFrameSequence& TestFrameSequence::WindowUpdate(Http2StreamId stream_id,
99 int32_t delta) {
100 frames_.push_back(
101 absl::make_unique<spdy::SpdyWindowUpdateIR>(stream_id, delta));
102 return *this;
103}
104
105TestFrameSequence& TestFrameSequence::Priority(Http2StreamId stream_id,
106 Http2StreamId parent_stream_id,
107 int weight,
108 bool exclusive) {
109 frames_.push_back(absl::make_unique<spdy::SpdyPriorityIR>(
110 stream_id, parent_stream_id, weight, exclusive));
111 return *this;
112}
113
114std::string TestFrameSequence::Serialize() {
115 std::string result;
116 if (!preface_.empty()) {
117 result = preface_;
118 }
119 spdy::SpdyFramer framer(spdy::SpdyFramer::ENABLE_COMPRESSION);
120 for (const auto& frame : frames_) {
121 spdy::SpdySerializedFrame f = framer.SerializeFrame(*frame);
122 absl::StrAppend(&result, absl::string_view(f));
123 }
124 return result;
125}
126
127} // namespace test
128} // namespace adapter
129} // namespace http2