Home | History | Annotate | Download | only in http
      1 // Copyright (c) 2010 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/base/cert_verifier.h"
      6 #include "net/base/mock_host_resolver.h"
      7 #include "net/base/net_log.h"
      8 #include "net/base/ssl_config_service_defaults.h"
      9 #include "net/http/http_network_layer.h"
     10 #include "net/http/http_network_session.h"
     11 #include "net/http/http_transaction_unittest.h"
     12 #include "net/proxy/proxy_service.h"
     13 #include "net/socket/socket_test_util.h"
     14 #include "net/spdy/spdy_session_pool.h"
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 #include "testing/platform_test.h"
     17 
     18 namespace net {
     19 
     20 namespace {
     21 
     22 class HttpNetworkLayerTest : public PlatformTest {
     23  protected:
     24   HttpNetworkLayerTest()
     25       : proxy_service_(ProxyService::CreateDirect()),
     26         ssl_config_service_(new SSLConfigServiceDefaults) {
     27     HttpNetworkSession::Params session_params;
     28     session_params.client_socket_factory = &mock_socket_factory_;
     29     session_params.host_resolver = &host_resolver_;
     30     session_params.cert_verifier = &cert_verifier_;
     31     session_params.proxy_service = proxy_service_;
     32     session_params.ssl_config_service = ssl_config_service_;
     33     network_session_ = new HttpNetworkSession(session_params);
     34     factory_.reset(new HttpNetworkLayer(network_session_));
     35   }
     36 
     37   MockClientSocketFactory mock_socket_factory_;
     38   MockHostResolver host_resolver_;
     39   CertVerifier cert_verifier_;
     40   const scoped_refptr<ProxyService> proxy_service_;
     41   const scoped_refptr<SSLConfigService> ssl_config_service_;
     42   scoped_refptr<HttpNetworkSession> network_session_;
     43   scoped_ptr<HttpNetworkLayer> factory_;
     44 };
     45 
     46 TEST_F(HttpNetworkLayerTest, CreateAndDestroy) {
     47   scoped_ptr<HttpTransaction> trans;
     48   int rv = factory_->CreateTransaction(&trans);
     49   EXPECT_EQ(OK, rv);
     50   EXPECT_TRUE(trans.get() != NULL);
     51 }
     52 
     53 TEST_F(HttpNetworkLayerTest, Suspend) {
     54   scoped_ptr<HttpTransaction> trans;
     55   int rv = factory_->CreateTransaction(&trans);
     56   EXPECT_EQ(OK, rv);
     57 
     58   trans.reset();
     59 
     60   factory_->Suspend(true);
     61 
     62   rv = factory_->CreateTransaction(&trans);
     63   EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, rv);
     64 
     65   ASSERT_TRUE(trans == NULL);
     66 
     67   factory_->Suspend(false);
     68 
     69   rv = factory_->CreateTransaction(&trans);
     70   EXPECT_EQ(OK, rv);
     71 }
     72 
     73 TEST_F(HttpNetworkLayerTest, GET) {
     74   MockRead data_reads[] = {
     75     MockRead("HTTP/1.0 200 OK\r\n\r\n"),
     76     MockRead("hello world"),
     77     MockRead(false, OK),
     78   };
     79   MockWrite data_writes[] = {
     80     MockWrite("GET / HTTP/1.1\r\n"
     81                    "Host: www.google.com\r\n"
     82                    "Connection: keep-alive\r\n"
     83                    "User-Agent: Foo/1.0\r\n\r\n"),
     84   };
     85   StaticSocketDataProvider data(data_reads, arraysize(data_reads),
     86                                      data_writes, arraysize(data_writes));
     87   mock_socket_factory_.AddSocketDataProvider(&data);
     88 
     89   TestCompletionCallback callback;
     90 
     91   HttpRequestInfo request_info;
     92   request_info.url = GURL("http://www.google.com/");
     93   request_info.method = "GET";
     94   request_info.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent,
     95                                        "Foo/1.0");
     96   request_info.load_flags = LOAD_NORMAL;
     97 
     98   scoped_ptr<HttpTransaction> trans;
     99   int rv = factory_->CreateTransaction(&trans);
    100   EXPECT_EQ(OK, rv);
    101 
    102   rv = trans->Start(&request_info, &callback, BoundNetLog());
    103   if (rv == ERR_IO_PENDING)
    104     rv = callback.WaitForResult();
    105   ASSERT_EQ(OK, rv);
    106 
    107   std::string contents;
    108   rv = ReadTransaction(trans.get(), &contents);
    109   EXPECT_EQ(OK, rv);
    110   EXPECT_EQ("hello world", contents);
    111 }
    112 
    113 }  // namespace
    114 
    115 }  // namespace net
    116