• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_refptr.h"
13 #include "base/strings/string_piece.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,
35       const spdy::Http2HeaderBlock* pushed_request_headers) override;
36   void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override;
37   void OnDataSent() override;
38   void OnTrailers(const spdy::Http2HeaderBlock& trailers) override;
39   void OnClose(int status) override;
40   bool CanGreaseFrameType() const override;
41   NetLogSource source_dependency() const override;
42 
43   // Returns whether or not the stream is closed.
StreamIsClosed()44   bool StreamIsClosed() const { return !stream_.get(); }
45 
46  private:
47   base::WeakPtr<SpdyStream> stream_;
48 };
49 
50 // Base class with shared functionality for test delegate
51 // implementations below.
52 class StreamDelegateBase : public SpdyStream::Delegate {
53  public:
54   explicit StreamDelegateBase(const base::WeakPtr<SpdyStream>& stream);
55   ~StreamDelegateBase() override;
56 
57   void OnHeadersSent() override;
58   void OnEarlyHintsReceived(const spdy::Http2HeaderBlock& headers) override;
59   void OnHeadersReceived(
60       const spdy::Http2HeaderBlock& response_headers,
61       const spdy::Http2HeaderBlock* pushed_request_headers) override;
62   void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override;
63   void OnDataSent() override;
64   void OnTrailers(const spdy::Http2HeaderBlock& trailers) override;
65   void OnClose(int status) override;
66   bool CanGreaseFrameType() const override;
67   NetLogSource source_dependency() const override;
68 
69   // Waits for the stream to be closed and returns the status passed
70   // to OnClose().
71   int WaitForClose();
72 
73   // Drains all data from the underlying read queue and returns it as
74   // a string.
75   std::string TakeReceivedData();
76 
77   // Returns whether or not the stream is closed.
StreamIsClosed()78   bool StreamIsClosed() const { return !stream_.get(); }
79 
80   // Returns the stream's ID. If called when the stream is closed,
81   // returns the stream's ID when it was open.
stream_id()82   spdy::SpdyStreamId stream_id() const { return stream_id_; }
83 
84   // Returns 103 Early Hints response headers.
early_hints()85   const std::vector<spdy::Http2HeaderBlock>& early_hints() const {
86     return early_hints_;
87   }
88 
89   std::string GetResponseHeaderValue(const std::string& name) const;
send_headers_completed()90   bool send_headers_completed() const { return send_headers_completed_; }
91 
92   // Returns the load timing info on the stream. This must be called after the
93   // stream is closed in order to get the up-to-date information.
94   const LoadTimingInfo& GetLoadTimingInfo();
95 
96  protected:
stream()97   const base::WeakPtr<SpdyStream>& stream() { return stream_; }
98 
99  private:
100   base::WeakPtr<SpdyStream> stream_;
101   spdy::SpdyStreamId stream_id_ = 0;
102   TestCompletionCallback callback_;
103   bool send_headers_completed_ = false;
104   std::vector<spdy::Http2HeaderBlock> early_hints_;
105   spdy::Http2HeaderBlock response_headers_;
106   SpdyReadQueue received_data_queue_;
107   LoadTimingInfo load_timing_info_;
108 };
109 
110 // Test delegate that does nothing. Used to capture data about the
111 // stream, e.g. its id when it was open.
112 class StreamDelegateDoNothing : public StreamDelegateBase {
113  public:
114   explicit StreamDelegateDoNothing(const base::WeakPtr<SpdyStream>& stream);
115   ~StreamDelegateDoNothing() override;
116 };
117 
118 // Test delegate that consumes data as it arrives.
119 class StreamDelegateConsumeData : public StreamDelegateBase {
120  public:
121   explicit StreamDelegateConsumeData(const base::WeakPtr<SpdyStream>& stream);
122   ~StreamDelegateConsumeData() override;
123 
124   void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override;
125 };
126 
127 // Test delegate that sends data immediately in OnHeadersReceived().
128 class StreamDelegateSendImmediate : public StreamDelegateBase {
129  public:
130   // |data| can be NULL.
131   StreamDelegateSendImmediate(const base::WeakPtr<SpdyStream>& stream,
132                               base::StringPiece data);
133   ~StreamDelegateSendImmediate() override;
134 
135   void OnHeadersReceived(
136       const spdy::Http2HeaderBlock& response_headers,
137       const spdy::Http2HeaderBlock* pushed_request_headers) override;
138 
139  private:
140   base::StringPiece data_;
141 };
142 
143 // Test delegate that sends body data.
144 class StreamDelegateWithBody : public StreamDelegateBase {
145  public:
146   StreamDelegateWithBody(const base::WeakPtr<SpdyStream>& stream,
147                          base::StringPiece data);
148   ~StreamDelegateWithBody() override;
149 
150   void OnHeadersSent() override;
151 
152  private:
153   scoped_refptr<StringIOBuffer> buf_;
154 };
155 
156 // Test delegate that closes stream in OnHeadersReceived().
157 class StreamDelegateCloseOnHeaders : public StreamDelegateBase {
158  public:
159   explicit StreamDelegateCloseOnHeaders(
160       const base::WeakPtr<SpdyStream>& stream);
161   ~StreamDelegateCloseOnHeaders() override;
162 
163   void OnHeadersReceived(
164       const spdy::Http2HeaderBlock& response_headers,
165       const spdy::Http2HeaderBlock* pushed_request_headers) override;
166 };
167 
168 // Test delegate that sets a flag when EOF is detected.
169 class StreamDelegateDetectEOF : public StreamDelegateBase {
170  public:
171   explicit StreamDelegateDetectEOF(const base::WeakPtr<SpdyStream>& stream);
172   ~StreamDelegateDetectEOF() override;
173 
174   void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override;
175 
eof_detected()176   bool eof_detected() const { return eof_detected_; }
177 
178  private:
179   bool eof_detected_ = false;
180 };
181 
182 }  // namespace net::test
183 
184 #endif  // NET_SPDY_SPDY_STREAM_TEST_UTIL_H_
185