11e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// found in the LICENSE file. 41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "net/http/http_basic_state.h" 61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "net/base/completion_callback.h" 81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "net/base/request_priority.h" 91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "net/http/http_request_info.h" 101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "net/socket/client_socket_handle.h" 111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace net { 141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace { 151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST(HttpBasicStateTest, ConstructsProperly) { 171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) ClientSocketHandle* const handle = new ClientSocketHandle; 181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // Ownership of handle is passed to |state|. 191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const HttpBasicState state(handle, true); 201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) EXPECT_EQ(handle, state.connection()); 211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) EXPECT_TRUE(state.using_proxy()); 221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST(HttpBasicStateTest, UsingProxyCanBeFalse) { 251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const HttpBasicState state(new ClientSocketHandle(), false); 261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) EXPECT_FALSE(state.using_proxy()); 271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST(HttpBasicStateTest, ReleaseConnectionWorks) { 301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) ClientSocketHandle* const handle = new ClientSocketHandle; 311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) HttpBasicState state(handle, false); 321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const scoped_ptr<ClientSocketHandle> released_connection( 331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) state.ReleaseConnection()); 341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) EXPECT_EQ(NULL, state.connection()); 351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) EXPECT_EQ(handle, released_connection.get()); 361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST(HttpBasicStateTest, InitializeWorks) { 391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) HttpBasicState state(new ClientSocketHandle(), false); 401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const HttpRequestInfo request_info; 411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) EXPECT_EQ(OK, 421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) state.Initialize( 431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) &request_info, LOW, BoundNetLog(), CompletionCallback())); 441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) EXPECT_TRUE(state.parser()); 451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST(HttpBasicStateTest, DeleteParser) { 481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) HttpBasicState state(new ClientSocketHandle(), false); 491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const HttpRequestInfo request_info; 501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) state.Initialize(&request_info, LOW, BoundNetLog(), CompletionCallback()); 511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) EXPECT_TRUE(state.parser()); 521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) state.DeleteParser(); 531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) EXPECT_EQ(NULL, state.parser()); 541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST(HttpBasicStateTest, GenerateRequestLineNoProxy) { 571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const bool use_proxy = false; 581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) HttpBasicState state(new ClientSocketHandle(), use_proxy); 591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) HttpRequestInfo request_info; 601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) request_info.url = GURL("http://www.example.com/path?foo=bar#hoge"); 611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) request_info.method = "PUT"; 621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) state.Initialize(&request_info, LOW, BoundNetLog(), CompletionCallback()); 631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) EXPECT_EQ("PUT /path?foo=bar HTTP/1.1\r\n", state.GenerateRequestLine()); 641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST(HttpBasicStateTest, GenerateRequestLineWithProxy) { 671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const bool use_proxy = true; 681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) HttpBasicState state(new ClientSocketHandle(), use_proxy); 691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) HttpRequestInfo request_info; 701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) request_info.url = GURL("http://www.example.com/path?foo=bar#hoge"); 711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) request_info.method = "PUT"; 721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) state.Initialize(&request_info, LOW, BoundNetLog(), CompletionCallback()); 731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) EXPECT_EQ("PUT http://www.example.com/path?foo=bar HTTP/1.1\r\n", 741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) state.GenerateRequestLine()); 751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} // namespace 781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} // namespace net 79