Home | History | Annotate | Download | only in url_request
      1 // Copyright (c) 2012 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/url_request/url_request_job.h"
      6 
      7 #include "net/base/request_priority.h"
      8 #include "net/http/http_transaction_unittest.h"
      9 #include "net/url_request/url_request_test_util.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 namespace net {
     13 
     14 namespace {
     15 
     16 // This is a header that signals the end of the data.
     17 const char kGzipGata[] = "\x1f\x08b\x08\0\0\0\0\0\0\3\3\0\0\0\0\0\0\0\0";
     18 
     19 void GZipServer(const HttpRequestInfo* request,
     20                 std::string* response_status,
     21                 std::string* response_headers,
     22                 std::string* response_data) {
     23   response_data->assign(kGzipGata, sizeof(kGzipGata));
     24 }
     25 
     26 const MockTransaction kGZip_Transaction = {
     27     "http://www.google.com/gzyp",
     28     "GET",
     29     base::Time(),
     30     "",
     31     LOAD_NORMAL,
     32     "HTTP/1.1 200 OK",
     33     "Cache-Control: max-age=10000\n"
     34     "Content-Encoding: gzip\n"
     35     "Content-Length: 30\n",  // Intentionally wrong.
     36     base::Time(),
     37     "",
     38     TEST_MODE_NORMAL,
     39     &GZipServer,
     40     0,
     41     OK
     42 };
     43 
     44 const MockTransaction kRedirect_Transaction = {
     45     "http://www.google.com/redirect",
     46     "GET",
     47     base::Time(),
     48     "",
     49     LOAD_NORMAL,
     50     "HTTP/1.1 302 Found",
     51     "Cache-Control: max-age=10000\n"
     52     "Location: http://www.google.com/destination\n"
     53     "Content-Length: 5\n",
     54     base::Time(),
     55     "hello",
     56     TEST_MODE_NORMAL,
     57     NULL,
     58     0,
     59     OK
     60 };
     61 
     62 }  // namespace
     63 
     64 TEST(URLRequestJob, TransactionNotifiedWhenDone) {
     65   MockNetworkLayer network_layer;
     66   TestURLRequestContext context;
     67   context.set_http_transaction_factory(&network_layer);
     68 
     69   TestDelegate d;
     70   TestURLRequest req(
     71       GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d, &context);
     72   AddMockTransaction(&kGZip_Transaction);
     73 
     74   req.set_method("GET");
     75   req.Start();
     76 
     77   base::MessageLoop::current()->Run();
     78 
     79   EXPECT_TRUE(network_layer.done_reading_called());
     80 
     81   RemoveMockTransaction(&kGZip_Transaction);
     82 }
     83 
     84 TEST(URLRequestJob, SyncTransactionNotifiedWhenDone) {
     85   MockNetworkLayer network_layer;
     86   TestURLRequestContext context;
     87   context.set_http_transaction_factory(&network_layer);
     88 
     89   TestDelegate d;
     90   TestURLRequest req(
     91       GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d, &context);
     92   MockTransaction transaction(kGZip_Transaction);
     93   transaction.test_mode = TEST_MODE_SYNC_ALL;
     94   AddMockTransaction(&transaction);
     95 
     96   req.set_method("GET");
     97   req.Start();
     98 
     99   base::MessageLoop::current()->Run();
    100 
    101   EXPECT_TRUE(network_layer.done_reading_called());
    102 
    103   RemoveMockTransaction(&transaction);
    104 }
    105 
    106 TEST(URLRequestJob, RedirectTransactionNotifiedWhenDone) {
    107   MockNetworkLayer network_layer;
    108   TestURLRequestContext context;
    109   context.set_http_transaction_factory(&network_layer);
    110 
    111   TestDelegate d;
    112   TestURLRequest req(
    113       GURL(kRedirect_Transaction.url), DEFAULT_PRIORITY, &d, &context);
    114   AddMockTransaction(&kRedirect_Transaction);
    115 
    116   req.set_method("GET");
    117   req.Start();
    118 
    119   base::MessageLoop::current()->Run();
    120 
    121   EXPECT_TRUE(network_layer.done_reading_called());
    122 
    123   RemoveMockTransaction(&kRedirect_Transaction);
    124 }
    125 
    126 }  // namespace net
    127