http_network_layer_unittest.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
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/mock_host_resolver.h" 6#include "net/base/net_log.h" 7#include "net/base/ssl_config_service_defaults.h" 8#include "net/http/http_network_layer.h" 9#include "net/http/http_transaction_unittest.h" 10#include "net/proxy/proxy_service.h" 11#include "net/socket/socket_test_util.h" 12#include "testing/gtest/include/gtest/gtest.h" 13#include "testing/platform_test.h" 14 15class HttpNetworkLayerTest : public PlatformTest { 16}; 17 18TEST_F(HttpNetworkLayerTest, CreateAndDestroy) { 19 net::HttpNetworkLayer factory(NULL, new net::MockHostResolver, 20 net::ProxyService::CreateNull(), new net::SSLConfigServiceDefaults, NULL, 21 NULL, NULL); 22 23 scoped_ptr<net::HttpTransaction> trans; 24 int rv = factory.CreateTransaction(&trans); 25 EXPECT_EQ(net::OK, rv); 26 EXPECT_TRUE(trans.get() != NULL); 27} 28 29TEST_F(HttpNetworkLayerTest, Suspend) { 30 net::HttpNetworkLayer factory(NULL, new net::MockHostResolver, 31 net::ProxyService::CreateNull(), new net::SSLConfigServiceDefaults, NULL, 32 NULL, NULL); 33 34 scoped_ptr<net::HttpTransaction> trans; 35 int rv = factory.CreateTransaction(&trans); 36 EXPECT_EQ(net::OK, rv); 37 38 trans.reset(); 39 40 factory.Suspend(true); 41 42 rv = factory.CreateTransaction(&trans); 43 EXPECT_EQ(net::ERR_NETWORK_IO_SUSPENDED, rv); 44 45 ASSERT_TRUE(trans == NULL); 46 47 factory.Suspend(false); 48 49 rv = factory.CreateTransaction(&trans); 50 EXPECT_EQ(net::OK, rv); 51} 52 53TEST_F(HttpNetworkLayerTest, GET) { 54 net::MockClientSocketFactory mock_socket_factory; 55 net::MockRead data_reads[] = { 56 net::MockRead("HTTP/1.0 200 OK\r\n\r\n"), 57 net::MockRead("hello world"), 58 net::MockRead(false, net::OK), 59 }; 60 net::MockWrite data_writes[] = { 61 net::MockWrite("GET / HTTP/1.1\r\n" 62 "Host: www.google.com\r\n" 63 "Connection: keep-alive\r\n" 64 "User-Agent: Foo/1.0\r\n\r\n"), 65 }; 66 net::StaticSocketDataProvider data(data_reads, arraysize(data_reads), 67 data_writes, arraysize(data_reads)); 68 mock_socket_factory.AddSocketDataProvider(&data); 69 70 net::HttpNetworkLayer factory(&mock_socket_factory, new net::MockHostResolver, 71 net::ProxyService::CreateNull(), new net::SSLConfigServiceDefaults, NULL, 72 NULL, NULL); 73 74 TestCompletionCallback callback; 75 76 scoped_ptr<net::HttpTransaction> trans; 77 int rv = factory.CreateTransaction(&trans); 78 EXPECT_EQ(net::OK, rv); 79 80 net::HttpRequestInfo request_info; 81 request_info.url = GURL("http://www.google.com/"); 82 request_info.method = "GET"; 83 request_info.extra_headers.SetHeader(net::HttpRequestHeaders::kUserAgent, 84 "Foo/1.0"); 85 request_info.load_flags = net::LOAD_NORMAL; 86 87 rv = trans->Start(&request_info, &callback, net::BoundNetLog()); 88 if (rv == net::ERR_IO_PENDING) 89 rv = callback.WaitForResult(); 90 ASSERT_EQ(net::OK, rv); 91 92 std::string contents; 93 rv = ReadTransaction(trans.get(), &contents); 94 EXPECT_EQ(net::OK, rv); 95 EXPECT_EQ("hello world", contents); 96} 97