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"
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
{
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 OnClose(int status
) override
;
36 // Returns whether or not the stream is closed.
37 bool StreamIsClosed() const { return !stream_
.get(); }
40 base::WeakPtr
<SpdyStream
> stream_
;
43 // Base class with shared functionality for test delegate
44 // implementations below.
45 class StreamDelegateBase
: public SpdyStream::Delegate
{
47 explicit StreamDelegateBase(const base::WeakPtr
<SpdyStream
>& stream
);
48 ~StreamDelegateBase() override
;
50 void OnRequestHeadersSent() override
;
51 SpdyResponseHeadersStatus
OnResponseHeadersUpdated(
52 const SpdyHeaderBlock
& response_headers
) override
;
53 void OnDataReceived(scoped_ptr
<SpdyBuffer
> buffer
) override
;
54 void OnDataSent() override
;
55 void OnClose(int status
) override
;
57 // Waits for the stream to be closed and returns the status passed
61 // Drains all data from the underlying read queue and returns it as
63 std::string
TakeReceivedData();
65 // Returns whether or not the stream is closed.
66 bool StreamIsClosed() const { return !stream_
.get(); }
68 // Returns the stream's ID. If called when the stream is closed,
69 // returns the stream's ID when it was open.
70 SpdyStreamId
stream_id() const { return stream_id_
; }
72 std::string
GetResponseHeaderValue(const std::string
& name
) const;
73 bool send_headers_completed() const { return send_headers_completed_
; }
76 const base::WeakPtr
<SpdyStream
>& stream() { return stream_
; }
79 base::WeakPtr
<SpdyStream
> stream_
;
80 SpdyStreamId stream_id_
;
81 TestCompletionCallback callback_
;
82 bool send_headers_completed_
;
83 SpdyHeaderBlock response_headers_
;
84 SpdyReadQueue received_data_queue_
;
87 // Test delegate that does nothing. Used to capture data about the
88 // stream, e.g. its id when it was open.
89 class StreamDelegateDoNothing
: public StreamDelegateBase
{
91 explicit StreamDelegateDoNothing(const base::WeakPtr
<SpdyStream
>& stream
);
92 ~StreamDelegateDoNothing() override
;
95 // Test delegate that sends data immediately in OnResponseHeadersUpdated().
96 class StreamDelegateSendImmediate
: public StreamDelegateBase
{
98 // |data| can be NULL.
99 StreamDelegateSendImmediate(const base::WeakPtr
<SpdyStream
>& stream
,
100 base::StringPiece data
);
101 ~StreamDelegateSendImmediate() override
;
103 SpdyResponseHeadersStatus
OnResponseHeadersUpdated(
104 const SpdyHeaderBlock
& response_headers
) override
;
107 base::StringPiece data_
;
110 // Test delegate that sends body data.
111 class StreamDelegateWithBody
: public StreamDelegateBase
{
113 StreamDelegateWithBody(const base::WeakPtr
<SpdyStream
>& stream
,
114 base::StringPiece data
);
115 ~StreamDelegateWithBody() override
;
117 void OnRequestHeadersSent() override
;
120 scoped_refptr
<StringIOBuffer
> buf_
;
123 // Test delegate that closes stream in OnResponseHeadersUpdated().
124 class StreamDelegateCloseOnHeaders
: public StreamDelegateBase
{
126 explicit StreamDelegateCloseOnHeaders(
127 const base::WeakPtr
<SpdyStream
>& stream
);
128 ~StreamDelegateCloseOnHeaders() override
;
130 SpdyResponseHeadersStatus
OnResponseHeadersUpdated(
131 const SpdyHeaderBlock
& response_headers
) override
;
138 #endif // NET_SPDY_SPDY_STREAM_TEST_UTIL_H_