Home | History | Annotate | Download | only in http
      1 // Copyright (c) 2006-2008 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/http/http_transaction_unittest.h"
      6 
      7 #include "base/hash_tables.h"
      8 #include "base/message_loop.h"
      9 #include "base/string_util.h"
     10 #include "net/base/net_errors.h"
     11 #include "net/base/load_flags.h"
     12 #include "net/disk_cache/disk_cache.h"
     13 #include "net/http/http_cache.h"
     14 #include "net/http/http_request_info.h"
     15 #include "net/http/http_response_info.h"
     16 #include "net/http/http_transaction.h"
     17 #include "testing/gtest/include/gtest/gtest.h"
     18 
     19 //-----------------------------------------------------------------------------
     20 // mock transaction data
     21 
     22 const MockTransaction kSimpleGET_Transaction = {
     23   "http://www.google.com/",
     24   "GET",
     25   base::Time(),
     26   "",
     27   net::LOAD_NORMAL,
     28   "HTTP/1.1 200 OK",
     29   "Cache-Control: max-age=10000\n",
     30   base::Time(),
     31   "<html><body>Google Blah Blah</body></html>",
     32   TEST_MODE_NORMAL,
     33   NULL,
     34   0
     35 };
     36 
     37 const MockTransaction kSimplePOST_Transaction = {
     38   "http://bugdatabase.com/edit",
     39   "POST",
     40   base::Time(),
     41   "",
     42   net::LOAD_NORMAL,
     43   "HTTP/1.1 200 OK",
     44   "",
     45   base::Time(),
     46   "<html><body>Google Blah Blah</body></html>",
     47   TEST_MODE_NORMAL,
     48   NULL,
     49   0
     50 };
     51 
     52 const MockTransaction kTypicalGET_Transaction = {
     53   "http://www.example.com/~foo/bar.html",
     54   "GET",
     55   base::Time(),
     56   "",
     57   net::LOAD_NORMAL,
     58   "HTTP/1.1 200 OK",
     59   "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
     60   "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n",
     61   base::Time(),
     62   "<html><body>Google Blah Blah</body></html>",
     63   TEST_MODE_NORMAL,
     64   NULL,
     65   0
     66 };
     67 
     68 const MockTransaction kETagGET_Transaction = {
     69   "http://www.google.com/foopy",
     70   "GET",
     71   base::Time(),
     72   "",
     73   net::LOAD_NORMAL,
     74   "HTTP/1.1 200 OK",
     75   "Cache-Control: max-age=10000\n"
     76   "Etag: foopy\n",
     77   base::Time(),
     78   "<html><body>Google Blah Blah</body></html>",
     79   TEST_MODE_NORMAL,
     80   NULL,
     81   0
     82 };
     83 
     84 const MockTransaction kRangeGET_Transaction = {
     85   "http://www.google.com/",
     86   "GET",
     87   base::Time(),
     88   "Range: 0-100\r\n",
     89   net::LOAD_NORMAL,
     90   "HTTP/1.1 200 OK",
     91   "Cache-Control: max-age=10000\n",
     92   base::Time(),
     93   "<html><body>Google Blah Blah</body></html>",
     94   TEST_MODE_NORMAL,
     95   NULL,
     96   0
     97 };
     98 
     99 static const MockTransaction* const kBuiltinMockTransactions[] = {
    100   &kSimpleGET_Transaction,
    101   &kSimplePOST_Transaction,
    102   &kTypicalGET_Transaction,
    103   &kETagGET_Transaction,
    104   &kRangeGET_Transaction
    105 };
    106 
    107 typedef base::hash_map<std::string, const MockTransaction*>
    108     MockTransactionMap;
    109 static MockTransactionMap mock_transactions;
    110 
    111 void AddMockTransaction(const MockTransaction* trans) {
    112   mock_transactions[GURL(trans->url).spec()] = trans;
    113 }
    114 
    115 void RemoveMockTransaction(const MockTransaction* trans) {
    116   mock_transactions.erase(GURL(trans->url).spec());
    117 }
    118 
    119 const MockTransaction* FindMockTransaction(const GURL& url) {
    120   // look for overrides:
    121   MockTransactionMap::const_iterator it = mock_transactions.find(url.spec());
    122   if (it != mock_transactions.end())
    123     return it->second;
    124 
    125   // look for builtins:
    126   for (size_t i = 0; i < arraysize(kBuiltinMockTransactions); ++i) {
    127     if (url == GURL(kBuiltinMockTransactions[i]->url))
    128       return kBuiltinMockTransactions[i];
    129   }
    130   return NULL;
    131 }
    132 
    133 
    134 //-----------------------------------------------------------------------------
    135 
    136 // static
    137 int TestTransactionConsumer::quit_counter_ = 0;
    138 
    139 
    140 //-----------------------------------------------------------------------------
    141 // helpers
    142 
    143 int ReadTransaction(net::HttpTransaction* trans, std::string* result) {
    144   int rv;
    145 
    146   TestCompletionCallback callback;
    147 
    148   std::string content;
    149   do {
    150     scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(256);
    151     rv = trans->Read(buf, 256, &callback);
    152     if (rv == net::ERR_IO_PENDING)
    153       rv = callback.WaitForResult();
    154     if (rv > 0) {
    155       content.append(buf->data(), rv);
    156     } else if (rv < 0) {
    157       return rv;
    158     }
    159   } while (rv > 0);
    160 
    161   result->swap(content);
    162   return net::OK;
    163 }
    164