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
|
// 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 "net/http/http_transaction_unittest.h"
#include "base/hash_tables.h"
#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"
//-----------------------------------------------------------------------------
// mock transaction data
const MockTransaction kSimpleGET_Transaction = {
"http://www.google.com/",
"GET",
base::Time(),
"",
net::LOAD_NORMAL,
"HTTP/1.1 200 OK",
"Cache-Control: max-age=10000\n",
base::Time(),
"<html><body>Google Blah Blah</body></html>",
TEST_MODE_NORMAL,
NULL,
0
};
const MockTransaction kSimplePOST_Transaction = {
"http://bugdatabase.com/edit",
"POST",
base::Time(),
"",
net::LOAD_NORMAL,
"HTTP/1.1 200 OK",
"",
base::Time(),
"<html><body>Google Blah Blah</body></html>",
TEST_MODE_NORMAL,
NULL,
0
};
const MockTransaction kTypicalGET_Transaction = {
"http://www.example.com/~foo/bar.html",
"GET",
base::Time(),
"",
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",
base::Time(),
"<html><body>Google Blah Blah</body></html>",
TEST_MODE_NORMAL,
NULL,
0
};
const MockTransaction kETagGET_Transaction = {
"http://www.google.com/foopy",
"GET",
base::Time(),
"",
net::LOAD_NORMAL,
"HTTP/1.1 200 OK",
"Cache-Control: max-age=10000\n"
"Etag: foopy\n",
base::Time(),
"<html><body>Google Blah Blah</body></html>",
TEST_MODE_NORMAL,
NULL,
0
};
const MockTransaction kRangeGET_Transaction = {
"http://www.google.com/",
"GET",
base::Time(),
"Range: 0-100\r\n",
net::LOAD_NORMAL,
"HTTP/1.1 200 OK",
"Cache-Control: max-age=10000\n",
base::Time(),
"<html><body>Google Blah Blah</body></html>",
TEST_MODE_NORMAL,
NULL,
0
};
static const MockTransaction* const kBuiltinMockTransactions[] = {
&kSimpleGET_Transaction,
&kSimplePOST_Transaction,
&kTypicalGET_Transaction,
&kETagGET_Transaction,
&kRangeGET_Transaction
};
typedef base::hash_map<std::string, const MockTransaction*>
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 (size_t 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 {
scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(256);
rv = trans->Read(buf, 256, &callback);
if (rv == net::ERR_IO_PENDING)
rv = callback.WaitForResult();
if (rv > 0) {
content.append(buf->data(), rv);
} else if (rv < 0) {
return rv;
}
} while (rv > 0);
result->swap(content);
return net::OK;
}
|