blob: 2dacb63d1d3237aa1af90b0035f1d1ab44d98f07 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NET_SPDY_SPDY_STREAM_TEST_UTIL_H_
#define NET_SPDY_SPDY_STREAM_TEST_UTIL_H_
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_piece.h"
#include "net/base/io_buffer.h"
#include "net/base/test_completion_callback.h"
#include "net/spdy/spdy_read_queue.h"
#include "net/spdy/spdy_stream.h"
namespace net {
namespace test {
// Delegate that calls Close() on |stream_| on OnClose. Used by tests
// to make sure that such an action is harmless.
class ClosingDelegate : public SpdyStream::Delegate {
public:
explicit ClosingDelegate(const base::WeakPtr<SpdyStream>& stream);
virtual ~ClosingDelegate();
// SpdyStream::Delegate implementation.
virtual SpdySendStatus OnSendHeadersComplete() OVERRIDE;
virtual int OnSendBody() OVERRIDE;
virtual SpdySendStatus OnSendBodyComplete(size_t bytes_sent) OVERRIDE;
virtual int OnResponseReceived(const SpdyHeaderBlock& response,
base::Time response_time,
int status) OVERRIDE;
virtual void OnHeadersSent() OVERRIDE;
virtual int OnDataReceived(scoped_ptr<SpdyBuffer> buffer) OVERRIDE;
virtual void OnDataSent(size_t bytes_sent) OVERRIDE;
virtual void OnClose(int status) OVERRIDE;
// Returns whether or not the stream is closed.
bool StreamIsClosed() const { return !stream_; }
private:
base::WeakPtr<SpdyStream> stream_;
};
// Base class with shared functionality for test delegate
// implementations below.
class StreamDelegateBase : public SpdyStream::Delegate {
public:
explicit StreamDelegateBase(const base::WeakPtr<SpdyStream>& stream);
virtual ~StreamDelegateBase();
virtual SpdySendStatus OnSendHeadersComplete() OVERRIDE;
virtual int OnSendBody() = 0;
virtual SpdySendStatus OnSendBodyComplete(size_t bytes_sent) = 0;
virtual int OnResponseReceived(const SpdyHeaderBlock& response,
base::Time response_time,
int status) OVERRIDE;
virtual void OnHeadersSent() OVERRIDE;
virtual int OnDataReceived(scoped_ptr<SpdyBuffer> buffer) OVERRIDE;
virtual void OnDataSent(size_t bytes_sent) OVERRIDE;
virtual void OnClose(int status) OVERRIDE;
// Waits for the stream to be closed and returns the status passed
// to OnClose().
int WaitForClose();
// Drains all data from the underlying read queue and returns it as
// a string.
std::string TakeReceivedData();
// Returns whether or not the stream is closed.
bool StreamIsClosed() const { return !stream_; }
// Returns the stream's ID. If called when the stream is closed,
// returns the stream's ID when it was open.
SpdyStreamId stream_id() const { return stream_id_; }
std::string GetResponseHeaderValue(const std::string& name) const;
bool send_headers_completed() const { return send_headers_completed_; }
int headers_sent() const { return headers_sent_; }
int data_sent() const { return data_sent_; }
protected:
const base::WeakPtr<SpdyStream>& stream() { return stream_; }
private:
base::WeakPtr<SpdyStream> stream_;
SpdyStreamId stream_id_;
TestCompletionCallback callback_;
bool send_headers_completed_;
SpdyHeaderBlock response_;
SpdyReadQueue received_data_queue_;
int headers_sent_;
int data_sent_;
};
// Test delegate that does nothing. Used to capture data about the
// stream, e.g. its id when it was open.
class StreamDelegateDoNothing : public StreamDelegateBase {
public:
StreamDelegateDoNothing(const base::WeakPtr<SpdyStream>& stream);
virtual ~StreamDelegateDoNothing();
virtual int OnSendBody() OVERRIDE;
virtual SpdySendStatus OnSendBodyComplete(size_t bytes_sent) OVERRIDE;
};
// Test delegate that sends data immediately in OnResponseReceived().
class StreamDelegateSendImmediate : public StreamDelegateBase {
public:
// Both |headers| and |buf| can be NULL.
StreamDelegateSendImmediate(const base::WeakPtr<SpdyStream>& stream,
scoped_ptr<SpdyHeaderBlock> headers,
base::StringPiece data);
virtual ~StreamDelegateSendImmediate();
virtual int OnSendBody() OVERRIDE;
virtual SpdySendStatus OnSendBodyComplete(size_t bytes_sent) OVERRIDE;
virtual int OnResponseReceived(const SpdyHeaderBlock& response,
base::Time response_time,
int status) OVERRIDE;
private:
scoped_ptr<SpdyHeaderBlock> headers_;
base::StringPiece data_;
};
// Test delegate that sends body data.
class StreamDelegateWithBody : public StreamDelegateBase {
public:
StreamDelegateWithBody(const base::WeakPtr<SpdyStream>& stream,
base::StringPiece data);
virtual ~StreamDelegateWithBody();
virtual SpdySendStatus OnSendHeadersComplete() OVERRIDE;
virtual int OnSendBody() OVERRIDE;
virtual SpdySendStatus OnSendBodyComplete(size_t bytes_sent) OVERRIDE;
int body_data_sent() const { return body_data_sent_; }
private:
scoped_refptr<DrainableIOBuffer> buf_;
int body_data_sent_;
};
} // namespace test
} // namespace net
#endif // NET_SPDY_SPDY_STREAM_TEST_UTIL_H_
|