1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/compiler_specific.h"
#include "base/platform_test.h"
#include "net/base/client_socket_factory.h"
#include "net/base/test_completion_callback.h"
#include "net/base/upload_data.h"
#include "net/http/http_network_session.h"
#include "net/http/http_network_transaction.h"
#include "net/http/http_transaction_unittest.h"
#include "testing/gtest/include/gtest/gtest.h"
//-----------------------------------------------------------------------------
struct MockConnect {
bool async;
int result;
// Asynchronous connection success.
MockConnect() : async(true), result(net::OK) { }
};
struct MockRead {
// Read failure (no data).
MockRead(bool async, int result) : async(async) , result(result), data(NULL),
data_len(0) { }
// Asynchronous read success (inferred data length).
MockRead(const char* data) : async(true), data(data), data_len(strlen(data)),
result(0) { }
// Read success (inferred data length).
MockRead(bool async, const char* data) : async(async), data(data),
data_len(strlen(data)), result(0) { }
// Read success.
MockRead(bool async, const char* data, int data_len) : async(async),
data(data), data_len(data_len), result(0) { }
bool async;
int result;
const char* data;
int data_len;
};
struct MockSocket {
MockSocket() : reads(NULL) { }
MockConnect connect;
MockRead* reads;
};
// Holds an array of MockSocket elements. As MockTCPClientSocket objects get
// instantiated, they take their data from the i'th element of this array.
//
// Tests should assign the first N entries of mock_sockets to point to valid
// MockSocket objects. The first unused entry should be NULL'd.
//
MockSocket* mock_sockets[10];
// Index of the next mock_sockets element to use.
int mock_sockets_index;
class MockTCPClientSocket : public net::ClientSocket {
public:
MockTCPClientSocket(const net::AddressList& addresses)
: data_(mock_sockets[mock_sockets_index++]),
ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)),
callback_(NULL),
read_index_(0),
read_offset_(0),
connected_(false) {
DCHECK(data_) << "overran mock_sockets array";
}
// ClientSocket methods:
virtual int Connect(net::CompletionCallback* callback) {
DCHECK(!callback_);
if (connected_)
return net::OK;
connected_ = true;
if (data_->connect.async) {
RunCallbackAsync(callback, data_->connect.result);
return net::ERR_IO_PENDING;
}
return data_->connect.result;
}
virtual int ReconnectIgnoringLastError(net::CompletionCallback* callback) {
NOTREACHED();
return net::ERR_FAILED;
}
virtual void Disconnect() {
connected_ = false;
callback_ = NULL;
}
virtual bool IsConnected() const {
return connected_;
}
// Socket methods:
virtual int Read(char* buf, int buf_len, net::CompletionCallback* callback) {
DCHECK(!callback_);
MockRead& r = data_->reads[read_index_];
int result;
if (r.data) {
if (r.data_len - read_offset_ > 0) {
result = std::min(buf_len, r.data_len - read_offset_);
memcpy(buf, r.data + read_offset_, result);
read_offset_ += result;
if (read_offset_ == r.data_len) {
read_index_++;
read_offset_ = 0;
}
} else {
result = 0; // EOF
}
} else {
result = r.result;
}
if (r.async) {
RunCallbackAsync(callback, result);
return net::ERR_IO_PENDING;
}
return result;
}
virtual int Write(const char* buf, int buf_len,
net::CompletionCallback* callback) {
DCHECK(!callback_);
return buf_len; // OK, we wrote it.
}
private:
void RunCallbackAsync(net::CompletionCallback* callback, int result) {
callback_ = callback;
MessageLoop::current()->PostTask(FROM_HERE,
method_factory_.NewRunnableMethod(
&MockTCPClientSocket::RunCallback, result));
}
void RunCallback(int result) {
net::CompletionCallback* c = callback_;
callback_ = NULL;
if (c)
c->Run(result);
}
MockSocket* data_;
ScopedRunnableMethodFactory<MockTCPClientSocket> method_factory_;
net::CompletionCallback* callback_;
int read_index_;
int read_offset_;
bool connected_;
};
class MockClientSocketFactory : public net::ClientSocketFactory {
public:
virtual net::ClientSocket* CreateTCPClientSocket(
const net::AddressList& addresses) {
return new MockTCPClientSocket(addresses);
}
virtual net::ClientSocket* CreateSSLClientSocket(
net::ClientSocket* transport_socket,
const std::string& hostname) {
return NULL;
}
};
MockClientSocketFactory mock_socket_factory;
class NullProxyResolver : public net::ProxyResolver {
public:
virtual int GetProxyConfig(net::ProxyConfig* config) {
return net::ERR_FAILED;
}
virtual int GetProxyForURL(const std::string& query_url,
const std::string& pac_url,
net::ProxyInfo* results) {
return net::ERR_FAILED;
}
};
net::HttpNetworkSession* CreateSession() {
return new net::HttpNetworkSession(new NullProxyResolver());
}
class HttpNetworkTransactionTest : public PlatformTest {
public:
virtual void SetUp() {
PlatformTest::SetUp();
mock_sockets[0] = NULL;
mock_sockets_index = 0;
}
protected:
void KeepAliveConnectionResendRequestTest(const MockRead& read_failure);
};
struct SimpleGetHelperResult {
std::string status_line;
std::string response_data;
};
SimpleGetHelperResult SimpleGetHelper(MockRead data_reads[]) {
SimpleGetHelperResult out;
net::HttpTransaction* trans = new net::HttpNetworkTransaction(
CreateSession(), &mock_socket_factory);
net::HttpRequestInfo request;
request.method = "GET";
request.url = GURL("http://www.google.com/");
request.load_flags = 0;
MockSocket data;
data.reads = data_reads;
mock_sockets[0] = &data;
mock_sockets[1] = NULL;
TestCompletionCallback callback;
int rv = trans->Start(&request, &callback);
EXPECT_EQ(net::ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(net::OK, rv);
const net::HttpResponseInfo* response = trans->GetResponseInfo();
EXPECT_TRUE(response != NULL);
EXPECT_TRUE(response->headers != NULL);
out.status_line = response->headers->GetStatusLine();
rv = ReadTransaction(trans, &out.response_data);
EXPECT_EQ(net::OK, rv);
trans->Destroy();
// Empty the current queue.
MessageLoop::current()->RunAllPending();
return out;
}
//-----------------------------------------------------------------------------
TEST_F(HttpNetworkTransactionTest, Basic) {
net::HttpTransaction* trans = new net::HttpNetworkTransaction(
CreateSession(), &mock_socket_factory);
trans->Destroy();
}
TEST_F(HttpNetworkTransactionTest, SimpleGET) {
MockRead data_reads[] = {
MockRead("HTTP/1.0 200 OK\r\n\r\n"),
MockRead("hello world"),
MockRead(false, net::OK),
};
SimpleGetHelperResult out = SimpleGetHelper(data_reads);
EXPECT_EQ("HTTP/1.0 200 OK", out.status_line);
EXPECT_EQ("hello world", out.response_data);
}
// Response with no status line.
TEST_F(HttpNetworkTransactionTest, SimpleGETNoHeaders) {
MockRead data_reads[] = {
MockRead("hello world"),
MockRead(false, net::OK),
};
SimpleGetHelperResult out = SimpleGetHelper(data_reads);
EXPECT_EQ("HTTP/0.9 200 OK", out.status_line);
EXPECT_EQ("hello world", out.response_data);
}
// Allow up to 4 bytes of junk to precede status line.
TEST_F(HttpNetworkTransactionTest, StatusLineJunk2Bytes) {
MockRead data_reads[] = {
MockRead("xxxHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"),
MockRead(false, net::OK),
};
SimpleGetHelperResult out = SimpleGetHelper(data_reads);
EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line);
EXPECT_EQ("DATA", out.response_data);
}
// Allow up to 4 bytes of junk to precede status line.
TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes) {
MockRead data_reads[] = {
MockRead("\n\nQJHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"),
MockRead(false, net::OK),
};
SimpleGetHelperResult out = SimpleGetHelper(data_reads);
EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line);
EXPECT_EQ("DATA", out.response_data);
}
// Beyond 4 bytes of slop and it should fail to find a status line.
TEST_F(HttpNetworkTransactionTest, StatusLineJunk5Bytes) {
MockRead data_reads[] = {
MockRead("xxxxxHTTP/1.1 404 Not Found\nServer: blah"),
MockRead(false, net::OK),
};
SimpleGetHelperResult out = SimpleGetHelper(data_reads);
EXPECT_EQ("HTTP/0.9 200 OK", out.status_line);
EXPECT_EQ("xxxxxHTTP/1.1 404 Not Found\nServer: blah", out.response_data);
}
// Same as StatusLineJunk4Bytes, except the read chunks are smaller.
TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes_Slow) {
MockRead data_reads[] = {
MockRead("\n"),
MockRead("\n"),
MockRead("Q"),
MockRead("J"),
MockRead("HTTP/1.0 404 Not Found\nServer: blah\n\nDATA"),
MockRead(false, net::OK),
};
SimpleGetHelperResult out = SimpleGetHelper(data_reads);
EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line);
EXPECT_EQ("DATA", out.response_data);
}
// Close the connection before enough bytes to have a status line.
TEST_F(HttpNetworkTransactionTest, StatusLinePartial) {
MockRead data_reads[] = {
MockRead("HTT"),
MockRead(false, net::OK),
};
SimpleGetHelperResult out = SimpleGetHelper(data_reads);
EXPECT_EQ("HTTP/0.9 200 OK", out.status_line);
EXPECT_EQ("HTT", out.response_data);
}
// Simulate a 204 response, lacking a Content-Length header, sent over a
// persistent connection. The response should still terminate since a 204
// cannot have a response body.
TEST_F(HttpNetworkTransactionTest, StopsReading204) {
MockRead data_reads[] = {
MockRead("HTTP/1.1 204 No Content\r\n\r\n"),
MockRead("junk"), // Should not be read!!
MockRead(false, net::OK),
};
SimpleGetHelperResult out = SimpleGetHelper(data_reads);
EXPECT_EQ("HTTP/1.1 204 No Content", out.status_line);
EXPECT_EQ("", out.response_data);
}
TEST_F(HttpNetworkTransactionTest, ReuseConnection) {
scoped_refptr<net::HttpNetworkSession> session = CreateSession();
MockRead data_reads[] = {
MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"),
MockRead("hello"),
MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"),
MockRead("world"),
MockRead(false, net::OK),
};
MockSocket data;
data.reads = data_reads;
mock_sockets[0] = &data;
mock_sockets[1] = NULL;
const char* kExpectedResponseData[] = {
"hello", "world"
};
for (int i = 0; i < 2; ++i) {
net::HttpTransaction* trans =
new net::HttpNetworkTransaction(session, &mock_socket_factory);
net::HttpRequestInfo request;
request.method = "GET";
request.url = GURL("http://www.google.com/");
request.load_flags = 0;
TestCompletionCallback callback;
int rv = trans->Start(&request, &callback);
EXPECT_EQ(net::ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(net::OK, rv);
const net::HttpResponseInfo* response = trans->GetResponseInfo();
EXPECT_TRUE(response != NULL);
EXPECT_TRUE(response->headers != NULL);
EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine());
std::string response_data;
rv = ReadTransaction(trans, &response_data);
EXPECT_EQ(net::OK, rv);
EXPECT_EQ(kExpectedResponseData[i], response_data);
trans->Destroy();
// Empty the current queue.
MessageLoop::current()->RunAllPending();
}
}
TEST_F(HttpNetworkTransactionTest, Ignores100) {
net::HttpTransaction* trans = new net::HttpNetworkTransaction(
CreateSession(), &mock_socket_factory);
net::HttpRequestInfo request;
request.method = "POST";
request.url = GURL("http://www.foo.com/");
request.upload_data = new net::UploadData;
request.upload_data->AppendBytes("foo", 3);
request.load_flags = 0;
MockRead data_reads[] = {
MockRead("HTTP/1.0 100 Continue\r\n\r\n"),
MockRead("HTTP/1.0 200 OK\r\n\r\n"),
MockRead("hello world"),
MockRead(false, net::OK),
};
MockSocket data;
data.reads = data_reads;
mock_sockets[0] = &data;
mock_sockets[1] = NULL;
TestCompletionCallback callback;
int rv = trans->Start(&request, &callback);
EXPECT_EQ(net::ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(net::OK, rv);
const net::HttpResponseInfo* response = trans->GetResponseInfo();
EXPECT_TRUE(response != NULL);
EXPECT_TRUE(response->headers != NULL);
EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine());
std::string response_data;
rv = ReadTransaction(trans, &response_data);
EXPECT_EQ(net::OK, rv);
EXPECT_EQ("hello world", response_data);
trans->Destroy();
// Empty the current queue.
MessageLoop::current()->RunAllPending();
}
// read_failure specifies a read failure that should cause the network
// transaction to resend the request.
void HttpNetworkTransactionTest::KeepAliveConnectionResendRequestTest(
const MockRead& read_failure) {
scoped_refptr<net::HttpNetworkSession> session = CreateSession();
net::HttpRequestInfo request;
request.method = "GET";
request.url = GURL("http://www.foo.com/");
request.load_flags = 0;
MockRead data1_reads[] = {
MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"),
MockRead("hello"),
read_failure, // Now, we reuse the connection and fail the first read.
};
MockSocket data1;
data1.reads = data1_reads;
mock_sockets[0] = &data1;
MockRead data2_reads[] = {
MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"),
MockRead("world"),
MockRead(true, net::OK),
};
MockSocket data2;
data2.reads = data2_reads;
mock_sockets[1] = &data2;
const char* kExpectedResponseData[] = {
"hello", "world"
};
for (int i = 0; i < 2; ++i) {
TestCompletionCallback callback;
net::HttpTransaction* trans =
new net::HttpNetworkTransaction(session, &mock_socket_factory);
int rv = trans->Start(&request, &callback);
EXPECT_EQ(net::ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(net::OK, rv);
const net::HttpResponseInfo* response = trans->GetResponseInfo();
EXPECT_TRUE(response != NULL);
EXPECT_TRUE(response->headers != NULL);
EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine());
std::string response_data;
rv = ReadTransaction(trans, &response_data);
EXPECT_EQ(net::OK, rv);
EXPECT_EQ(kExpectedResponseData[i], response_data);
trans->Destroy();
// Empty the current queue.
MessageLoop::current()->RunAllPending();
}
}
TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionReset) {
MockRead read_failure(true, net::ERR_CONNECTION_RESET);
KeepAliveConnectionResendRequestTest(read_failure);
}
TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionEOF) {
MockRead read_failure(false, net::OK); // EOF
KeepAliveConnectionResendRequestTest(read_failure);
}
TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionReset) {
net::HttpTransaction* trans = new net::HttpNetworkTransaction(
CreateSession(), &mock_socket_factory);
net::HttpRequestInfo request;
request.method = "GET";
request.url = GURL("http://www.google.com/");
request.load_flags = 0;
MockRead data_reads[] = {
MockRead(true, net::ERR_CONNECTION_RESET),
MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used
MockRead("hello world"),
MockRead(false, net::OK),
};
MockSocket data;
data.reads = data_reads;
mock_sockets[0] = &data;
mock_sockets[1] = NULL;
TestCompletionCallback callback;
int rv = trans->Start(&request, &callback);
EXPECT_EQ(net::ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
EXPECT_EQ(net::ERR_CONNECTION_RESET, rv);
const net::HttpResponseInfo* response = trans->GetResponseInfo();
EXPECT_TRUE(response == NULL);
trans->Destroy();
// Empty the current queue.
MessageLoop::current()->RunAllPending();
}
// What do various browsers do when the server closes a non-keepalive
// connection without sending any response header or body?
//
// IE7: error page
// Safari 3.1.2 (Windows): error page
// Firefox 3.0.1: blank page
// Opera 9.52: after five attempts, blank page
// Us with WinHTTP: error page (net::ERR_INVALID_RESPONSE)
// Us: blank page
TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionEOF) {
MockRead data_reads[] = {
MockRead(false, net::OK), // EOF
MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used
MockRead("hello world"),
MockRead(false, net::OK),
};
SimpleGetHelperResult out = SimpleGetHelper(data_reads);
EXPECT_EQ("HTTP/0.9 200 OK", out.status_line);
EXPECT_EQ("", out.response_data);
}
|