spdy_stream_test_util.h revision a93a17c8d99d686bd4a1511e5504e5e6cc9fcadf
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 SpdySendStatus OnSendHeadersComplete() OVERRIDE;
30  virtual int OnSendBody() OVERRIDE;
31  virtual SpdySendStatus OnSendBodyComplete(size_t bytes_sent) OVERRIDE;
32  virtual int OnResponseReceived(const SpdyHeaderBlock& response,
33                                 base::Time response_time,
34                                 int status) OVERRIDE;
35  virtual void OnHeadersSent() OVERRIDE;
36  virtual int OnDataReceived(scoped_ptr<SpdyBuffer> buffer) OVERRIDE;
37  virtual void OnDataSent(size_t bytes_sent) OVERRIDE;
38  virtual void OnClose(int status) OVERRIDE;
39
40  // Returns whether or not the stream is closed.
41  bool StreamIsClosed() const { return !stream_; }
42
43 private:
44  base::WeakPtr<SpdyStream> stream_;
45};
46
47// Base class with shared functionality for test delegate
48// implementations below.
49class StreamDelegateBase : public SpdyStream::Delegate {
50 public:
51  explicit StreamDelegateBase(const base::WeakPtr<SpdyStream>& stream);
52  virtual ~StreamDelegateBase();
53
54  virtual SpdySendStatus OnSendHeadersComplete() OVERRIDE;
55  virtual int OnSendBody() = 0;
56  virtual SpdySendStatus OnSendBodyComplete(size_t bytes_sent) = 0;
57  virtual int OnResponseReceived(const SpdyHeaderBlock& response,
58                                 base::Time response_time,
59                                 int status) OVERRIDE;
60  virtual void OnHeadersSent() OVERRIDE;
61  virtual int OnDataReceived(scoped_ptr<SpdyBuffer> buffer) OVERRIDE;
62  virtual void OnDataSent(size_t bytes_sent) OVERRIDE;
63  virtual void OnClose(int status) OVERRIDE;
64
65  // Waits for the stream to be closed and returns the status passed
66  // to OnClose().
67  int WaitForClose();
68
69  // Drains all data from the underlying read queue and returns it as
70  // a string.
71  std::string TakeReceivedData();
72
73  // Returns whether or not the stream is closed.
74  bool StreamIsClosed() const { return !stream_; }
75
76  // Returns the stream's ID. If called when the stream is closed,
77  // returns the stream's ID when it was open.
78  SpdyStreamId stream_id() const { return stream_id_; }
79
80  std::string GetResponseHeaderValue(const std::string& name) const;
81  bool send_headers_completed() const { return send_headers_completed_; }
82  int headers_sent() const { return headers_sent_; }
83  int data_sent() const { return data_sent_; }
84
85 protected:
86  const base::WeakPtr<SpdyStream>& stream() { return stream_; }
87
88 private:
89  base::WeakPtr<SpdyStream> stream_;
90  SpdyStreamId stream_id_;
91  TestCompletionCallback callback_;
92  bool send_headers_completed_;
93  SpdyHeaderBlock response_;
94  SpdyReadQueue received_data_queue_;
95  int headers_sent_;
96  int data_sent_;
97};
98
99// Test delegate that does nothing. Used to capture data about the
100// stream, e.g. its id when it was open.
101class StreamDelegateDoNothing : public StreamDelegateBase {
102 public:
103  StreamDelegateDoNothing(const base::WeakPtr<SpdyStream>& stream);
104  virtual ~StreamDelegateDoNothing();
105
106  virtual int OnSendBody() OVERRIDE;
107  virtual SpdySendStatus OnSendBodyComplete(size_t bytes_sent) OVERRIDE;
108};
109
110// Test delegate that sends data immediately in OnResponseReceived().
111class StreamDelegateSendImmediate : public StreamDelegateBase {
112 public:
113  // Both |headers| and |buf| can be NULL.
114  StreamDelegateSendImmediate(const base::WeakPtr<SpdyStream>& stream,
115                              scoped_ptr<SpdyHeaderBlock> headers,
116                              base::StringPiece data);
117  virtual ~StreamDelegateSendImmediate();
118
119  virtual int OnSendBody() OVERRIDE;
120  virtual SpdySendStatus OnSendBodyComplete(size_t bytes_sent) OVERRIDE;
121  virtual int OnResponseReceived(const SpdyHeaderBlock& response,
122                                 base::Time response_time,
123                                 int status) OVERRIDE;
124
125 private:
126  scoped_ptr<SpdyHeaderBlock> headers_;
127  base::StringPiece data_;
128};
129
130// Test delegate that sends body data.
131class StreamDelegateWithBody : public StreamDelegateBase {
132 public:
133  StreamDelegateWithBody(const base::WeakPtr<SpdyStream>& stream,
134                         base::StringPiece data);
135  virtual ~StreamDelegateWithBody();
136
137  virtual int OnSendBody() OVERRIDE;
138  virtual SpdySendStatus OnSendBodyComplete(size_t bytes_sent) OVERRIDE;
139
140  int body_data_sent() const { return body_data_sent_; }
141
142 private:
143  scoped_refptr<DrainableIOBuffer> buf_;
144  int body_data_sent_;
145};
146
147} // namespace test
148
149} // namespace net
150
151#endif // NET_SPDY_SPDY_STREAM_TEST_UTIL_H_
152