15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NET_SPDY_SPDY_STREAM_TEST_UTIL_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NET_SPDY_SPDY_STREAM_TEST_UTIL_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/compiler_specific.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/strings/string_piece.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/base/io_buffer.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/base/test_completion_callback.h"
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "net/spdy/spdy_read_queue.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/spdy/spdy_stream.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace test {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Delegate that calls Close() on |stream_| on OnClose. Used by tests
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// to make sure that such an action is harmless.
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ClosingDelegate : public SpdyStream::Delegate {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
25a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  explicit ClosingDelegate(const base::WeakPtr<SpdyStream>& stream);
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~ClosingDelegate();
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // SpdyStream::Delegate implementation.
2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void OnRequestHeadersSent() OVERRIDE;
30eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual SpdyResponseHeadersStatus OnResponseHeadersUpdated(
31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      const SpdyHeaderBlock& response_headers) OVERRIDE;
32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual void OnDataReceived(scoped_ptr<SpdyBuffer> buffer) OVERRIDE;
3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void OnDataSent() OVERRIDE;
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnClose(int status) OVERRIDE;
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
36a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // Returns whether or not the stream is closed.
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool StreamIsClosed() const { return !stream_.get(); }
38a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
40a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  base::WeakPtr<SpdyStream> stream_;
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Base class with shared functionality for test delegate
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// implementations below.
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class StreamDelegateBase : public SpdyStream::Delegate {
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
47a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  explicit StreamDelegateBase(const base::WeakPtr<SpdyStream>& stream);
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~StreamDelegateBase();
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void OnRequestHeadersSent() OVERRIDE;
51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual SpdyResponseHeadersStatus OnResponseHeadersUpdated(
52eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      const SpdyHeaderBlock& response_headers) OVERRIDE;
53eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual void OnDataReceived(scoped_ptr<SpdyBuffer> buffer) OVERRIDE;
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void OnDataSent() OVERRIDE;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnClose(int status) OVERRIDE;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Waits for the stream to be closed and returns the status passed
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // to OnClose().
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int WaitForClose();
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Drains all data from the underlying read queue and returns it as
62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // a string.
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::string TakeReceivedData();
64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
65a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // Returns whether or not the stream is closed.
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool StreamIsClosed() const { return !stream_.get(); }
67a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
68a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // Returns the stream's ID. If called when the stream is closed,
69a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // returns the stream's ID when it was open.
70a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  SpdyStreamId stream_id() const { return stream_id_; }
71a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string GetResponseHeaderValue(const std::string& name) const;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool send_headers_completed() const { return send_headers_completed_; }
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) protected:
76a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  const base::WeakPtr<SpdyStream>& stream() { return stream_; }
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
79a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  base::WeakPtr<SpdyStream> stream_;
80a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  SpdyStreamId stream_id_;
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TestCompletionCallback callback_;
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool send_headers_completed_;
83eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SpdyHeaderBlock response_headers_;
84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SpdyReadQueue received_data_queue_;
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
87a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Test delegate that does nothing. Used to capture data about the
88a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// stream, e.g. its id when it was open.
89a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)class StreamDelegateDoNothing : public StreamDelegateBase {
90a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) public:
91a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  StreamDelegateDoNothing(const base::WeakPtr<SpdyStream>& stream);
92a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  virtual ~StreamDelegateDoNothing();
93a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)};
94a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
95eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Test delegate that sends data immediately in OnResponseHeadersUpdated().
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class StreamDelegateSendImmediate : public StreamDelegateBase {
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // |data| can be NULL.
99a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  StreamDelegateSendImmediate(const base::WeakPtr<SpdyStream>& stream,
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              base::StringPiece data);
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~StreamDelegateSendImmediate();
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
103eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual SpdyResponseHeadersStatus OnResponseHeadersUpdated(
104eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      const SpdyHeaderBlock& response_headers) OVERRIDE;
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::StringPiece data_;
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Test delegate that sends body data.
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class StreamDelegateWithBody : public StreamDelegateBase {
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
113a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  StreamDelegateWithBody(const base::WeakPtr<SpdyStream>& stream,
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                         base::StringPiece data);
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~StreamDelegateWithBody();
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void OnRequestHeadersSent() OVERRIDE;
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
12090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_refptr<StringIOBuffer> buf_;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
123116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Test delegate that closes stream in OnResponseHeadersUpdated().
124116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass StreamDelegateCloseOnHeaders : public StreamDelegateBase {
125116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public:
126116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  StreamDelegateCloseOnHeaders(const base::WeakPtr<SpdyStream>& stream);
127116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual ~StreamDelegateCloseOnHeaders();
128116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
129116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual SpdyResponseHeadersStatus OnResponseHeadersUpdated(
130116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const SpdyHeaderBlock& response_headers) OVERRIDE;
131116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch};
132116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace test
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace net
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // NET_SPDY_SPDY_STREAM_TEST_UTIL_H_
138