spdy_stream_test_util.h revision 868fa2fe829687343ffae624259930155e16dbd8
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.
4
5#ifndef NET_SPDY_SPDY_STREAM_TEST_UTIL_H_
6#define NET_SPDY_SPDY_STREAM_TEST_UTIL_H_
7
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"
16
17namespace net {
18
19namespace test {
20
21// Delegate that calls Close() on |stream_| on OnClose. Used by tests
22// to make sure that such an action is harmless.
23class ClosingDelegate : public SpdyStream::Delegate {
24 public:
25  explicit ClosingDelegate(const base::WeakPtr<SpdyStream>& stream);
26  virtual ~ClosingDelegate();
27
28  // SpdyStream::Delegate implementation.
29  virtual void OnRequestHeadersSent() OVERRIDE;
30  virtual int OnResponseHeadersReceived(const SpdyHeaderBlock& response,
31                                        base::Time response_time,
32                                        int status) OVERRIDE;
33  virtual int OnDataReceived(scoped_ptr<SpdyBuffer> buffer) OVERRIDE;
34  virtual void OnDataSent() OVERRIDE;
35  virtual void OnClose(int status) OVERRIDE;
36
37  // Returns whether or not the stream is closed.
38  bool StreamIsClosed() const { return !stream_.get(); }
39
40 private:
41  base::WeakPtr<SpdyStream> stream_;
42};
43
44// Base class with shared functionality for test delegate
45// implementations below.
46class StreamDelegateBase : public SpdyStream::Delegate {
47 public:
48  explicit StreamDelegateBase(const base::WeakPtr<SpdyStream>& stream);
49  virtual ~StreamDelegateBase();
50
51  virtual void OnRequestHeadersSent() OVERRIDE;
52  virtual int OnResponseHeadersReceived(const SpdyHeaderBlock& response,
53                                        base::Time response_time,
54                                        int status) OVERRIDE;
55  virtual int OnDataReceived(scoped_ptr<SpdyBuffer> buffer) OVERRIDE;
56  virtual void OnDataSent() OVERRIDE;
57  virtual void OnClose(int status) OVERRIDE;
58
59  // Waits for the stream to be closed and returns the status passed
60  // to OnClose().
61  int WaitForClose();
62
63  // Drains all data from the underlying read queue and returns it as
64  // a string.
65  std::string TakeReceivedData();
66
67  // Returns whether or not the stream is closed.
68  bool StreamIsClosed() const { return !stream_.get(); }
69
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_; }
73
74  std::string GetResponseHeaderValue(const std::string& name) const;
75  bool send_headers_completed() const { return send_headers_completed_; }
76
77 protected:
78  const base::WeakPtr<SpdyStream>& stream() { return stream_; }
79
80 private:
81  base::WeakPtr<SpdyStream> stream_;
82  SpdyStreamId stream_id_;
83  TestCompletionCallback callback_;
84  bool send_headers_completed_;
85  SpdyHeaderBlock response_;
86  SpdyReadQueue received_data_queue_;
87};
88
89// Test delegate that does nothing. Used to capture data about the
90// stream, e.g. its id when it was open.
91class StreamDelegateDoNothing : public StreamDelegateBase {
92 public:
93  StreamDelegateDoNothing(const base::WeakPtr<SpdyStream>& stream);
94  virtual ~StreamDelegateDoNothing();
95};
96
97// Test delegate that sends data immediately in OnResponseHeadersReceived().
98class StreamDelegateSendImmediate : public StreamDelegateBase {
99 public:
100  // |data| can be NULL.
101  StreamDelegateSendImmediate(const base::WeakPtr<SpdyStream>& stream,
102                              base::StringPiece data);
103  virtual ~StreamDelegateSendImmediate();
104
105  virtual int OnResponseHeadersReceived(const SpdyHeaderBlock& response,
106                                        base::Time response_time,
107                                        int status) OVERRIDE;
108
109 private:
110  base::StringPiece data_;
111};
112
113// Test delegate that sends body data.
114class StreamDelegateWithBody : public StreamDelegateBase {
115 public:
116  StreamDelegateWithBody(const base::WeakPtr<SpdyStream>& stream,
117                         base::StringPiece data);
118  virtual ~StreamDelegateWithBody();
119
120  virtual void OnRequestHeadersSent() OVERRIDE;
121
122 private:
123  scoped_refptr<StringIOBuffer> buf_;
124};
125
126} // namespace test
127
128} // namespace net
129
130#endif // NET_SPDY_SPDY_STREAM_TEST_UTIL_H_
131