Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / net / url_request / url_request_job_unittest.cc
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 "base/run_loop.h"
8 #include "net/base/request_priority.h"
9 #include "net/http/http_transaction_unittest.h"
10 #include "net/url_request/url_request_test_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace net {
14
15 namespace {
16
17 // This is a header that signals the end of the data.
18 const char kGzipData[] = "\x1f\x08b\x08\0\0\0\0\0\0\3\3\0\0\0\0\0\0\0\0";
19 const char kGzipDataWithName[] =
20     "\x1f\x08b\x08\x08\0\0\0\0\0\0name\0\3\0\0\0\0\0\0\0\0";
21
22 void GZipServer(const HttpRequestInfo* request,
23                 std::string* response_status,
24                 std::string* response_headers,
25                 std::string* response_data) {
26   response_data->assign(kGzipData, sizeof(kGzipData));
27 }
28
29 void BigGZipServer(const HttpRequestInfo* request,
30                    std::string* response_status,
31                    std::string* response_headers,
32                    std::string* response_data) {
33   response_data->assign(kGzipDataWithName, sizeof(kGzipDataWithName));
34   response_data->insert(10, 64 * 1024, 'a');
35 }
36
37 const MockTransaction kGZip_Transaction = {
38     "http://www.google.com/gzyp",
39     "GET",
40     base::Time(),
41     "",
42     LOAD_NORMAL,
43     "HTTP/1.1 200 OK",
44     "Cache-Control: max-age=10000\n"
45     "Content-Encoding: gzip\n"
46     "Content-Length: 30\n",  // Intentionally wrong.
47     base::Time(),
48     "",
49     TEST_MODE_NORMAL,
50     &GZipServer,
51     0,
52     OK
53 };
54
55 const MockTransaction kRedirect_Transaction = {
56     "http://www.google.com/redirect",
57     "GET",
58     base::Time(),
59     "",
60     LOAD_NORMAL,
61     "HTTP/1.1 302 Found",
62     "Cache-Control: max-age=10000\n"
63     "Location: http://www.google.com/destination\n"
64     "Content-Length: 5\n",
65     base::Time(),
66     "hello",
67     TEST_MODE_NORMAL,
68     NULL,
69     0,
70     OK
71 };
72
73 }  // namespace
74
75 TEST(URLRequestJob, TransactionNotifiedWhenDone) {
76   MockNetworkLayer network_layer;
77   TestURLRequestContext context;
78   context.set_http_transaction_factory(&network_layer);
79
80   TestDelegate d;
81   TestURLRequest req(
82       GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d, &context);
83   AddMockTransaction(&kGZip_Transaction);
84
85   req.set_method("GET");
86   req.Start();
87
88   base::MessageLoop::current()->Run();
89
90   EXPECT_TRUE(network_layer.done_reading_called());
91
92   RemoveMockTransaction(&kGZip_Transaction);
93 }
94
95 TEST(URLRequestJob, SyncTransactionNotifiedWhenDone) {
96   MockNetworkLayer network_layer;
97   TestURLRequestContext context;
98   context.set_http_transaction_factory(&network_layer);
99
100   TestDelegate d;
101   TestURLRequest req(
102       GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d, &context);
103   MockTransaction transaction(kGZip_Transaction);
104   transaction.test_mode = TEST_MODE_SYNC_ALL;
105   AddMockTransaction(&transaction);
106
107   req.set_method("GET");
108   req.Start();
109
110   base::RunLoop().Run();
111
112   EXPECT_TRUE(network_layer.done_reading_called());
113
114   RemoveMockTransaction(&transaction);
115 }
116
117 // Tests processing a large gzip header one byte at a time.
118 TEST(URLRequestJob, SyncSlowTransaction) {
119   MockNetworkLayer network_layer;
120   TestURLRequestContext context;
121   context.set_http_transaction_factory(&network_layer);
122
123   TestDelegate d;
124   TestURLRequest req(
125       GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d, &context);
126   MockTransaction transaction(kGZip_Transaction);
127   transaction.test_mode = TEST_MODE_SYNC_ALL | TEST_MODE_SLOW_READ;
128   transaction.handler = &BigGZipServer;
129   AddMockTransaction(&transaction);
130
131   req.set_method("GET");
132   req.Start();
133
134   base::RunLoop().Run();
135
136   EXPECT_TRUE(network_layer.done_reading_called());
137
138   RemoveMockTransaction(&transaction);
139 }
140
141 TEST(URLRequestJob, RedirectTransactionNotifiedWhenDone) {
142   MockNetworkLayer network_layer;
143   TestURLRequestContext context;
144   context.set_http_transaction_factory(&network_layer);
145
146   TestDelegate d;
147   TestURLRequest req(
148       GURL(kRedirect_Transaction.url), DEFAULT_PRIORITY, &d, &context);
149   AddMockTransaction(&kRedirect_Transaction);
150
151   req.set_method("GET");
152   req.Start();
153
154   base::RunLoop().Run();
155
156   EXPECT_TRUE(network_layer.done_reading_called());
157
158   RemoveMockTransaction(&kRedirect_Transaction);
159 }
160
161 TEST(URLRequestJob, TransactionNotCachedWhenNetworkDelegateRedirects) {
162   MockNetworkLayer network_layer;
163   TestNetworkDelegate network_delegate;
164   network_delegate.set_redirect_on_headers_received_url(GURL("http://foo"));
165   TestURLRequestContext context;
166   context.set_http_transaction_factory(&network_layer);
167   context.set_network_delegate(&network_delegate);
168
169   TestDelegate d;
170   TestURLRequest req(GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d,
171                      &context);
172   AddMockTransaction(&kGZip_Transaction);
173
174   req.set_method("GET");
175   req.Start();
176
177   base::RunLoop().Run();
178
179   EXPECT_TRUE(network_layer.stop_caching_called());
180
181   RemoveMockTransaction(&kGZip_Transaction);
182 }
183
184 }  // namespace net