// Copyright 2008, Google Inc. // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "net/http/http_transaction_unittest.h" #include #include #include "base/message_loop.h" #include "base/string_util.h" #include "net/base/net_errors.h" #include "net/base/load_flags.h" #include "net/disk_cache/disk_cache.h" #include "net/http/http_cache.h" #include "net/http/http_request_info.h" #include "net/http/http_response_info.h" #include "net/http/http_transaction.h" #include "testing/gtest/include/gtest/gtest.h" #pragma warning(disable: 4355) //----------------------------------------------------------------------------- // mock transaction data const MockTransaction kSimpleGET_Transaction = { "http://www.google.com/", "GET", "", net::LOAD_NORMAL, "HTTP/1.1 200 OK", "Cache-Control: max-age=10000\n", "Google Blah Blah", TEST_MODE_NORMAL, NULL, 0 }; const MockTransaction kSimplePOST_Transaction = { "http://bugdatabase.com/edit", "POST", "", net::LOAD_NORMAL, "HTTP/1.1 200 OK", "", "Google Blah Blah", TEST_MODE_NORMAL, NULL, 0 }; const MockTransaction kTypicalGET_Transaction = { "http://www.example.com/~foo/bar.html", "GET", "", net::LOAD_NORMAL, "HTTP/1.1 200 OK", "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n", "Google Blah Blah", TEST_MODE_NORMAL, NULL, 0 }; const MockTransaction kETagGET_Transaction = { "http://www.google.com/foopy", "GET", "", net::LOAD_NORMAL, "HTTP/1.1 200 OK", "Cache-Control: max-age=10000\n" "Etag: foopy\n", "Google Blah Blah", TEST_MODE_NORMAL, NULL, 0 }; const MockTransaction kRangeGET_Transaction = { "http://www.google.com/", "GET", "Range: 0-100\r\n", net::LOAD_NORMAL, "HTTP/1.1 200 OK", "Cache-Control: max-age=10000\n", "Google Blah Blah", TEST_MODE_NORMAL, NULL, 0 }; static const MockTransaction* const kBuiltinMockTransactions[] = { &kSimpleGET_Transaction, &kSimplePOST_Transaction, &kTypicalGET_Transaction, &kETagGET_Transaction, &kRangeGET_Transaction }; typedef stdext::hash_map MockTransactionMap; static MockTransactionMap mock_transactions; void AddMockTransaction(const MockTransaction* trans) { mock_transactions[GURL(trans->url).spec()] = trans; } void RemoveMockTransaction(const MockTransaction* trans) { mock_transactions.erase(GURL(trans->url).spec()); } const MockTransaction* FindMockTransaction(const GURL& url) { // look for overrides: MockTransactionMap::const_iterator it = mock_transactions.find(url.spec()); if (it != mock_transactions.end()) return it->second; // look for builtins: for (int i = 0; i < arraysize(kBuiltinMockTransactions); ++i) { if (url == GURL(kBuiltinMockTransactions[i]->url)) return kBuiltinMockTransactions[i]; } return NULL; } //----------------------------------------------------------------------------- // static int TestTransactionConsumer::quit_counter_ = 0; //----------------------------------------------------------------------------- // helpers int ReadTransaction(net::HttpTransaction* trans, std::string* result) { int rv; TestCompletionCallback callback; std::string content; do { char buf[256]; rv = trans->Read(buf, sizeof(buf), &callback); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); if (rv > 0) { content.append(buf, rv); } else if (rv < 0) { return rv; } } while (rv > 0); result->swap(content); return net::OK; }