Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / spdy / spdy_stream_test_util.h
blob9c260ea2e0e5f6df4bfffc9dca805be81f42066c
1 // Copyright (c) 2012 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 #ifndef NET_SPDY_SPDY_STREAM_TEST_UTIL_H_
6 #define NET_SPDY_SPDY_STREAM_TEST_UTIL_H_
8 #include "base/compiler_specific.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string_piece.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/test_completion_callback.h"
14 #include "net/spdy/spdy_read_queue.h"
15 #include "net/spdy/spdy_stream.h"
17 namespace net {
19 namespace test {
21 // Delegate that calls Close() on |stream_| on OnClose. Used by tests
22 // to make sure that such an action is harmless.
23 class ClosingDelegate : public SpdyStream::Delegate {
24 public:
25 explicit ClosingDelegate(const base::WeakPtr<SpdyStream>& stream);
26 ~ClosingDelegate() override;
28 // SpdyStream::Delegate implementation.
29 void OnRequestHeadersSent() override;
30 SpdyResponseHeadersStatus OnResponseHeadersUpdated(
31 const SpdyHeaderBlock& response_headers) override;
32 void OnDataReceived(scoped_ptr<SpdyBuffer> buffer) override;
33 void OnDataSent() override;
34 void OnTrailers(const SpdyHeaderBlock& trailers) override;
35 void OnClose(int status) override;
37 // Returns whether or not the stream is closed.
38 bool StreamIsClosed() const { return !stream_.get(); }
40 private:
41 base::WeakPtr<SpdyStream> stream_;
44 // Base class with shared functionality for test delegate
45 // implementations below.
46 class StreamDelegateBase : public SpdyStream::Delegate {
47 public:
48 explicit StreamDelegateBase(const base::WeakPtr<SpdyStream>& stream);
49 ~StreamDelegateBase() override;
51 void OnRequestHeadersSent() override;
52 SpdyResponseHeadersStatus OnResponseHeadersUpdated(
53 const SpdyHeaderBlock& response_headers) override;
54 void OnDataReceived(scoped_ptr<SpdyBuffer> buffer) override;
55 void OnDataSent() override;
56 void OnTrailers(const SpdyHeaderBlock& trailers) override;
57 void OnClose(int status) override;
59 // Waits for the stream to be closed and returns the status passed
60 // to OnClose().
61 int WaitForClose();
63 // Drains all data from the underlying read queue and returns it as
64 // a string.
65 std::string TakeReceivedData();
67 // Returns whether or not the stream is closed.
68 bool StreamIsClosed() const { return !stream_.get(); }
70 // Returns the stream's ID. If called when the stream is closed,
71 // returns the stream's ID when it was open.
72 SpdyStreamId stream_id() const { return stream_id_; }
74 std::string GetResponseHeaderValue(const std::string& name) const;
75 bool send_headers_completed() const { return send_headers_completed_; }
77 protected:
78 const base::WeakPtr<SpdyStream>& stream() { return stream_; }
80 private:
81 base::WeakPtr<SpdyStream> stream_;
82 SpdyStreamId stream_id_;
83 TestCompletionCallback callback_;
84 bool send_headers_completed_;
85 SpdyHeaderBlock response_headers_;
86 SpdyReadQueue received_data_queue_;
89 // Test delegate that does nothing. Used to capture data about the
90 // stream, e.g. its id when it was open.
91 class StreamDelegateDoNothing : public StreamDelegateBase {
92 public:
93 explicit StreamDelegateDoNothing(const base::WeakPtr<SpdyStream>& stream);
94 ~StreamDelegateDoNothing() override;
97 // Test delegate that sends data immediately in OnResponseHeadersUpdated().
98 class StreamDelegateSendImmediate : public StreamDelegateBase {
99 public:
100 // |data| can be NULL.
101 StreamDelegateSendImmediate(const base::WeakPtr<SpdyStream>& stream,
102 base::StringPiece data);
103 ~StreamDelegateSendImmediate() override;
105 SpdyResponseHeadersStatus OnResponseHeadersUpdated(
106 const SpdyHeaderBlock& response_headers) override;
108 private:
109 base::StringPiece data_;
112 // Test delegate that sends body data.
113 class StreamDelegateWithBody : public StreamDelegateBase {
114 public:
115 StreamDelegateWithBody(const base::WeakPtr<SpdyStream>& stream,
116 base::StringPiece data);
117 ~StreamDelegateWithBody() override;
119 void OnRequestHeadersSent() override;
121 private:
122 scoped_refptr<StringIOBuffer> buf_;
125 // Test delegate that closes stream in OnResponseHeadersUpdated().
126 class StreamDelegateCloseOnHeaders : public StreamDelegateBase {
127 public:
128 explicit StreamDelegateCloseOnHeaders(
129 const base::WeakPtr<SpdyStream>& stream);
130 ~StreamDelegateCloseOnHeaders() override;
132 SpdyResponseHeadersStatus OnResponseHeadersUpdated(
133 const SpdyHeaderBlock& response_headers) override;
136 } // namespace test
138 } // namespace net
140 #endif // NET_SPDY_SPDY_STREAM_TEST_UTIL_H_