Land Recent QUIC changes.
[chromium-blink-merge.git] / net / tools / quic / test_tools / quic_test_client.h
blob9640505959ea5d277810e6860fa0f18dacdb6b67
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_QUIC_TEST_TOOLS_QUIC_CLIENT_H_
6 #define NET_QUIC_TEST_TOOLS_QUIC_CLIENT_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "net/quic/quic_framer.h"
13 #include "net/quic/quic_packet_creator.h"
14 #include "net/quic/quic_protocol.h"
15 #include "net/quic/test_tools/quic_test_writer.h"
16 #include "net/tools/quic/quic_client.h"
18 namespace net {
20 class ProofVerifier;
22 namespace tools {
24 namespace test {
26 class HTTPMessage;
28 // A toy QUIC client used for testing.
29 class QuicTestClient : public ReliableQuicStream::Visitor {
30 public:
31 QuicTestClient(IPEndPoint server_address, const string& server_hostname,
32 const QuicVersion version);
33 QuicTestClient(IPEndPoint server_address,
34 const string& server_hostname,
35 bool secure,
36 const QuicVersion version);
37 QuicTestClient(IPEndPoint server_address,
38 const string& server_hostname,
39 bool secure,
40 const QuicConfig& config,
41 const QuicVersion version);
43 virtual ~QuicTestClient();
45 // ExpectCertificates controls whether the server is expected to provide
46 // certificates. The certificates, if any, are not verified, but the common
47 // name is recorded and available with |cert_common_name()|.
48 void ExpectCertificates(bool on);
50 // Clears any outstanding state and sends a simple GET of 'uri' to the
51 // server. Returns 0 if the request failed and no bytes were written.
52 ssize_t SendRequest(const string& uri);
53 ssize_t SendMessage(const HTTPMessage& message);
55 string SendCustomSynchronousRequest(const HTTPMessage& message);
56 string SendSynchronousRequest(const string& uri);
58 // Wraps data in a quic packet and sends it.
59 ssize_t SendData(string data, bool last_data);
61 QuicPacketCreator::Options* options() { return client_->options(); }
63 const BalsaHeaders *response_headers() const {return &headers_;}
65 void WaitForResponse();
67 void Connect();
68 void ResetConnection();
69 void Disconnect();
70 IPEndPoint LocalSocketAddress() const;
71 void ClearPerRequestState();
72 void WaitForInitialResponse();
73 ssize_t Send(const void *buffer, size_t size);
74 int response_size() const;
75 size_t bytes_read() const;
76 size_t bytes_written() const;
78 // From ReliableQuicStream::Visitor
79 virtual void OnClose(ReliableQuicStream* stream) OVERRIDE;
81 // Configures client_ to take ownership of and use the writer.
82 // Must be called before initial connect.
83 void UseWriter(net::test::QuicTestWriter* writer);
85 // Returns NULL if the maximum number of streams have already been created.
86 QuicReliableClientStream* GetOrCreateStream();
88 QuicRstStreamErrorCode stream_error() { return stream_error_; }
89 QuicErrorCode connection_error() { return client()->session()->error(); }
91 QuicClient* client() { return client_.get(); }
93 // cert_common_name returns the common name value of the server's certificate,
94 // or the empty string if no certificate was presented.
95 const string& cert_common_name() const;
97 const string& response_body() {return response_;}
98 bool connected() const;
100 void set_auto_reconnect(bool reconnect) { auto_reconnect_ = reconnect; }
102 void set_priority(QuicPriority priority) { priority_ = priority; }
104 private:
105 void Initialize(IPEndPoint address, const string& hostname, bool secure);
107 IPEndPoint server_address_;
108 IPEndPoint client_address_;
109 scoped_ptr<QuicClient> client_; // The actual client
110 QuicReliableClientStream* stream_;
112 QuicRstStreamErrorCode stream_error_;
114 BalsaHeaders headers_;
115 QuicPriority priority_;
117 string response_;
118 uint64 bytes_read_;
119 uint64 bytes_written_;
120 // True if the client has never connected before. The client will
121 // auto-connect exactly once before sending data. If something causes a
122 // connection reset, it will not automatically reconnect.
123 bool never_connected_;
124 bool secure_;
125 // If true, the client will always reconnect if necessary before creating a
126 // stream.
127 bool auto_reconnect_;
129 // proof_verifier_ points to a RecordingProofVerifier that is owned by
130 // client_.
131 ProofVerifier* proof_verifier_;
134 } // namespace test
136 } // namespace tools
137 } // namespace net
139 #endif // NET_QUIC_TEST_TOOLS_QUIC_CLIENT_H_