xref: /aosp_15_r20/external/cronet/net/spdy/spdy_stream_test_util.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_SPDY_SPDY_STREAM_TEST_UTIL_H_
6 #define NET_SPDY_SPDY_STREAM_TEST_UTIL_H_
7 
8 #include <memory>
9 #include <string>
10 #include <string_view>
11 
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_refptr.h"
14 #include "net/base/io_buffer.h"
15 #include "net/base/load_timing_info.h"
16 #include "net/base/test_completion_callback.h"
17 #include "net/log/net_log_source.h"
18 #include "net/spdy/spdy_read_queue.h"
19 #include "net/spdy/spdy_stream.h"
20 
21 namespace net::test {
22 
23 // Delegate that calls Close() on |stream_| on OnClose. Used by tests
24 // to make sure that such an action is harmless.
25 class ClosingDelegate : public SpdyStream::Delegate {
26  public:
27   explicit ClosingDelegate(const base::WeakPtr<SpdyStream>& stream);
28   ~ClosingDelegate() override;
29 
30   // SpdyStream::Delegate implementation.
31   void OnEarlyHintsReceived(const spdy::Http2HeaderBlock& headers) override;
32   void OnHeadersSent() override;
33   void OnHeadersReceived(
34       const spdy::Http2HeaderBlock& response_headers) override;
35   void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override;
36   void OnDataSent() override;
37   void OnTrailers(const spdy::Http2HeaderBlock& trailers) override;
38   void OnClose(int status) override;
39   bool CanGreaseFrameType() const override;
40   NetLogSource source_dependency() const override;
41 
42   // Returns whether or not the stream is closed.
StreamIsClosed()43   bool StreamIsClosed() const { return !stream_.get(); }
44 
45  private:
46   base::WeakPtr<SpdyStream> stream_;
47 };
48 
49 // Base class with shared functionality for test delegate
50 // implementations below.
51 class StreamDelegateBase : public SpdyStream::Delegate {
52  public:
53   explicit StreamDelegateBase(const base::WeakPtr<SpdyStream>& stream);
54   ~StreamDelegateBase() override;
55 
56   void OnHeadersSent() override;
57   void OnEarlyHintsReceived(const spdy::Http2HeaderBlock& headers) override;
58   void OnHeadersReceived(
59       const spdy::Http2HeaderBlock& response_headers) override;
60   void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override;
61   void OnDataSent() override;
62   void OnTrailers(const spdy::Http2HeaderBlock& trailers) override;
63   void OnClose(int status) override;
64   bool CanGreaseFrameType() const override;
65   NetLogSource source_dependency() const override;
66 
67   // Waits for the stream to be closed and returns the status passed
68   // to OnClose().
69   int WaitForClose();
70 
71   // Drains all data from the underlying read queue and returns it as
72   // a string.
73   std::string TakeReceivedData();
74 
75   // Returns whether or not the stream is closed.
StreamIsClosed()76   bool StreamIsClosed() const { return !stream_.get(); }
77 
78   // Returns the stream's ID. If called when the stream is closed,
79   // returns the stream's ID when it was open.
stream_id()80   spdy::SpdyStreamId stream_id() const { return stream_id_; }
81 
82   // Returns 103 Early Hints response headers.
early_hints()83   const std::vector<spdy::Http2HeaderBlock>& early_hints() const {
84     return early_hints_;
85   }
86 
87   std::string GetResponseHeaderValue(const std::string& name) const;
send_headers_completed()88   bool send_headers_completed() const { return send_headers_completed_; }
89 
90   // Returns the load timing info on the stream. This must be called after the
91   // stream is closed in order to get the up-to-date information.
92   const LoadTimingInfo& GetLoadTimingInfo();
93 
94  protected:
stream()95   const base::WeakPtr<SpdyStream>& stream() { return stream_; }
96 
97  private:
98   base::WeakPtr<SpdyStream> stream_;
99   spdy::SpdyStreamId stream_id_ = 0;
100   TestCompletionCallback callback_;
101   bool send_headers_completed_ = false;
102   std::vector<spdy::Http2HeaderBlock> early_hints_;
103   spdy::Http2HeaderBlock response_headers_;
104   SpdyReadQueue received_data_queue_;
105   LoadTimingInfo load_timing_info_;
106 };
107 
108 // Test delegate that does nothing. Used to capture data about the
109 // stream, e.g. its id when it was open.
110 class StreamDelegateDoNothing : public StreamDelegateBase {
111  public:
112   explicit StreamDelegateDoNothing(const base::WeakPtr<SpdyStream>& stream);
113   ~StreamDelegateDoNothing() override;
114 };
115 
116 // Test delegate that consumes data as it arrives.
117 class StreamDelegateConsumeData : public StreamDelegateBase {
118  public:
119   explicit StreamDelegateConsumeData(const base::WeakPtr<SpdyStream>& stream);
120   ~StreamDelegateConsumeData() override;
121 
122   void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override;
123 };
124 
125 // Test delegate that sends data immediately in OnHeadersReceived().
126 class StreamDelegateSendImmediate : public StreamDelegateBase {
127  public:
128   // |data| can be NULL.
129   StreamDelegateSendImmediate(const base::WeakPtr<SpdyStream>& stream,
130                               std::string_view data);
131   ~StreamDelegateSendImmediate() override;
132 
133   void OnHeadersReceived(
134       const spdy::Http2HeaderBlock& response_headers) override;
135 
136  private:
137   std::string_view data_;
138 };
139 
140 // Test delegate that sends body data.
141 class StreamDelegateWithBody : public StreamDelegateBase {
142  public:
143   StreamDelegateWithBody(const base::WeakPtr<SpdyStream>& stream,
144                          std::string_view data);
145   ~StreamDelegateWithBody() override;
146 
147   void OnHeadersSent() override;
148 
149  private:
150   scoped_refptr<StringIOBuffer> buf_;
151 };
152 
153 // Test delegate that closes stream in OnHeadersReceived().
154 class StreamDelegateCloseOnHeaders : public StreamDelegateBase {
155  public:
156   explicit StreamDelegateCloseOnHeaders(
157       const base::WeakPtr<SpdyStream>& stream);
158   ~StreamDelegateCloseOnHeaders() override;
159 
160   void OnHeadersReceived(
161       const spdy::Http2HeaderBlock& response_headers) override;
162 };
163 
164 // Test delegate that sets a flag when EOF is detected.
165 class StreamDelegateDetectEOF : public StreamDelegateBase {
166  public:
167   explicit StreamDelegateDetectEOF(const base::WeakPtr<SpdyStream>& stream);
168   ~StreamDelegateDetectEOF() override;
169 
170   void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override;
171 
eof_detected()172   bool eof_detected() const { return eof_detected_; }
173 
174  private:
175   bool eof_detected_ = false;
176 };
177 
178 }  // namespace net::test
179 
180 #endif  // NET_SPDY_SPDY_STREAM_TEST_UTIL_H_
181