1// Copyright (c) 2009 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#include "net/flip/flip_stream.h"
6#include "base/ref_counted.h"
7#include "net/base/mock_host_resolver.h"
8#include "net/base/net_errors.h"
9#include "net/base/ssl_config_service.h"
10#include "net/base/ssl_config_service_defaults.h"
11#include "net/base/test_completion_callback.h"
12#include "net/flip/flip_session.h"
13#include "net/flip/flip_session_pool.h"
14#include "net/http/http_network_session.h"
15#include "net/http/http_request_info.h"
16#include "net/http/http_response_info.h"
17#include "net/proxy/proxy_service.h"
18#include "net/socket/socket_test_util.h"
19#include "testing/gtest/include/gtest/gtest.h"
20
21namespace net {
22
23class FlipSessionPoolPeer {
24 public:
25  explicit FlipSessionPoolPeer(const scoped_refptr<FlipSessionPool>& pool)
26      : pool_(pool) {}
27
28  void RemoveFlipSession(const scoped_refptr<FlipSession>& session) {
29    pool_->Remove(session);
30  }
31
32 private:
33  const scoped_refptr<FlipSessionPool> pool_;
34
35  DISALLOW_COPY_AND_ASSIGN(FlipSessionPoolPeer);
36};
37
38namespace {
39
40// Create a proxy service which fails on all requests (falls back to direct).
41ProxyService* CreateNullProxyService() {
42  return ProxyService::CreateNull();
43}
44
45// Helper to manage the lifetimes of the dependencies for a
46// FlipNetworkTransaction.
47class SessionDependencies {
48 public:
49  // Default set of dependencies -- "null" proxy service.
50  SessionDependencies()
51      : host_resolver(new MockHostResolver),
52        proxy_service(CreateNullProxyService()),
53        ssl_config_service(new SSLConfigServiceDefaults),
54        flip_session_pool(new FlipSessionPool) {}
55
56  // Custom proxy service dependency.
57  explicit SessionDependencies(ProxyService* proxy_service)
58      : host_resolver(new MockHostResolver),
59        proxy_service(proxy_service),
60        ssl_config_service(new SSLConfigServiceDefaults),
61        flip_session_pool(new FlipSessionPool) {}
62
63  scoped_refptr<MockHostResolverBase> host_resolver;
64  scoped_refptr<ProxyService> proxy_service;
65  scoped_refptr<SSLConfigService> ssl_config_service;
66  MockClientSocketFactory socket_factory;
67  scoped_refptr<FlipSessionPool> flip_session_pool;
68};
69
70HttpNetworkSession* CreateSession(SessionDependencies* session_deps) {
71  return new HttpNetworkSession(NULL,
72                                session_deps->host_resolver,
73                                session_deps->proxy_service,
74                                &session_deps->socket_factory,
75                                session_deps->ssl_config_service,
76                                session_deps->flip_session_pool);
77}
78
79class FlipStreamTest : public testing::Test {
80 protected:
81  FlipStreamTest()
82      : session_(CreateSession(&session_deps_)),
83        pool_peer_(session_->flip_session_pool()) {}
84
85  scoped_refptr<FlipSession> CreateFlipSession() {
86    HostResolver::RequestInfo resolve_info("www.google.com", 80);
87    scoped_refptr<FlipSession> session(
88        session_->flip_session_pool()->Get(resolve_info, session_));
89    return session;
90  }
91
92  virtual void TearDown() {
93    MessageLoop::current()->RunAllPending();
94  }
95
96  SessionDependencies session_deps_;
97  scoped_refptr<HttpNetworkSession> session_;
98  FlipSessionPoolPeer pool_peer_;
99};
100
101// Needs fixing, see http://crbug.com/28622
102TEST_F(FlipStreamTest, SendRequest) {
103  scoped_refptr<FlipSession> session(CreateFlipSession());
104  HttpRequestInfo request;
105  request.method = "GET";
106  request.url = GURL("http://www.google.com/");
107  TestCompletionCallback callback;
108  HttpResponseInfo response;
109
110  scoped_refptr<FlipStream> stream(new FlipStream(session, 1, false, NULL));
111  EXPECT_EQ(ERR_IO_PENDING, stream->SendRequest(NULL, &response, &callback));
112
113  // Need to manually remove the flip session since normally it gets removed on
114  // socket close/error, but we aren't communicating over a socket here.
115  pool_peer_.RemoveFlipSession(session);
116}
117
118// TODO(willchan): Write a longer test for FlipStream that exercises all
119// methods.
120
121}  // namespace
122
123}  // namespace net
124