OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_network_transaction.h" | 5 #include "net/http/http_network_transaction.h" |
6 | 6 |
7 #include <math.h> // ceil | 7 #include <math.h> // ceil |
8 #include <stdarg.h> | 8 #include <stdarg.h> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | 167 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
168 MessageLoop::current()->RunAllPending(); | 168 MessageLoop::current()->RunAllPending(); |
169 spdy::SpdyFramer::set_enable_compression_default(true); | 169 spdy::SpdyFramer::set_enable_compression_default(true); |
170 // Empty the current queue. | 170 // Empty the current queue. |
171 MessageLoop::current()->RunAllPending(); | 171 MessageLoop::current()->RunAllPending(); |
172 PlatformTest::TearDown(); | 172 PlatformTest::TearDown(); |
173 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | 173 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
174 MessageLoop::current()->RunAllPending(); | 174 MessageLoop::current()->RunAllPending(); |
175 } | 175 } |
176 | 176 |
177 void KeepAliveConnectionResendRequestTest(const MockRead& read_failure); | 177 // Either |write_failure| specifies a write failure or |read_failure| |
| 178 // specifies a read failure when using a reused socket. In either case, the |
| 179 // failure should cause the network transaction to resend the request, and the |
| 180 // other argument should be NULL. |
| 181 void KeepAliveConnectionResendRequestTest(const MockWrite* write_failure, |
| 182 const MockRead* read_failure); |
178 | 183 |
179 SimpleGetHelperResult SimpleGetHelperForData(StaticSocketDataProvider* data[], | 184 SimpleGetHelperResult SimpleGetHelperForData(StaticSocketDataProvider* data[], |
180 size_t data_count) { | 185 size_t data_count) { |
181 SimpleGetHelperResult out; | 186 SimpleGetHelperResult out; |
182 | 187 |
183 HttpRequestInfo request; | 188 HttpRequestInfo request; |
184 request.method = "GET"; | 189 request.method = "GET"; |
185 request.url = GURL("http://www.google.com/"); | 190 request.url = GURL("http://www.google.com/"); |
186 request.load_flags = 0; | 191 request.load_flags = 0; |
187 | 192 |
(...skipping 811 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
999 | 1004 |
1000 TestOldCompletionCallback callback; | 1005 TestOldCompletionCallback callback; |
1001 | 1006 |
1002 int rv = trans->Start(&request, &callback, BoundNetLog()); | 1007 int rv = trans->Start(&request, &callback, BoundNetLog()); |
1003 EXPECT_EQ(ERR_IO_PENDING, rv); | 1008 EXPECT_EQ(ERR_IO_PENDING, rv); |
1004 | 1009 |
1005 rv = callback.WaitForResult(); | 1010 rv = callback.WaitForResult(); |
1006 EXPECT_EQ(ERR_EMPTY_RESPONSE, rv); | 1011 EXPECT_EQ(ERR_EMPTY_RESPONSE, rv); |
1007 } | 1012 } |
1008 | 1013 |
1009 // read_failure specifies a read failure that should cause the network | |
1010 // transaction to resend the request. | |
1011 void HttpNetworkTransactionTest::KeepAliveConnectionResendRequestTest( | 1014 void HttpNetworkTransactionTest::KeepAliveConnectionResendRequestTest( |
1012 const MockRead& read_failure) { | 1015 const MockWrite* write_failure, |
| 1016 const MockRead* read_failure) { |
1013 HttpRequestInfo request; | 1017 HttpRequestInfo request; |
1014 request.method = "GET"; | 1018 request.method = "GET"; |
1015 request.url = GURL("http://www.foo.com/"); | 1019 request.url = GURL("http://www.foo.com/"); |
1016 request.load_flags = 0; | 1020 request.load_flags = 0; |
1017 | 1021 |
1018 SessionDependencies session_deps; | 1022 SessionDependencies session_deps; |
1019 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); | 1023 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
1020 | 1024 |
| 1025 // Written data for successfully sending both requests. |
| 1026 MockWrite data1_writes[] = { |
| 1027 MockWrite("GET / HTTP/1.1\r\n" |
| 1028 "Host: www.foo.com\r\n" |
| 1029 "Connection: keep-alive\r\n\r\n"), |
| 1030 MockWrite("GET / HTTP/1.1\r\n" |
| 1031 "Host: www.foo.com\r\n" |
| 1032 "Connection: keep-alive\r\n\r\n") |
| 1033 }; |
| 1034 |
| 1035 // Read results for the first request. |
1021 MockRead data1_reads[] = { | 1036 MockRead data1_reads[] = { |
1022 MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), | 1037 MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
1023 MockRead("hello"), | 1038 MockRead("hello"), |
1024 read_failure, // Now, we reuse the connection and fail the first read. | 1039 MockRead(true, OK), |
1025 }; | 1040 }; |
1026 StaticSocketDataProvider data1(data1_reads, arraysize(data1_reads), NULL, 0); | 1041 |
| 1042 if (write_failure) { |
| 1043 ASSERT_TRUE(!read_failure); |
| 1044 data1_writes[1] = *write_failure; |
| 1045 } else { |
| 1046 ASSERT_TRUE(read_failure); |
| 1047 data1_reads[2] = *read_failure; |
| 1048 } |
| 1049 |
| 1050 StaticSocketDataProvider data1(data1_reads, arraysize(data1_reads), |
| 1051 data1_writes, arraysize(data1_writes)); |
1027 session_deps.socket_factory.AddSocketDataProvider(&data1); | 1052 session_deps.socket_factory.AddSocketDataProvider(&data1); |
1028 | 1053 |
1029 MockRead data2_reads[] = { | 1054 MockRead data2_reads[] = { |
1030 MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), | 1055 MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
1031 MockRead("world"), | 1056 MockRead("world"), |
1032 MockRead(true, OK), | 1057 MockRead(true, OK), |
1033 }; | 1058 }; |
1034 StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); | 1059 StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); |
1035 session_deps.socket_factory.AddSocketDataProvider(&data2); | 1060 session_deps.socket_factory.AddSocketDataProvider(&data2); |
1036 | 1061 |
(...skipping 18 matching lines...) Expand all Loading... |
1055 EXPECT_TRUE(response->headers != NULL); | 1080 EXPECT_TRUE(response->headers != NULL); |
1056 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | 1081 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
1057 | 1082 |
1058 std::string response_data; | 1083 std::string response_data; |
1059 rv = ReadTransaction(trans.get(), &response_data); | 1084 rv = ReadTransaction(trans.get(), &response_data); |
1060 EXPECT_EQ(OK, rv); | 1085 EXPECT_EQ(OK, rv); |
1061 EXPECT_EQ(kExpectedResponseData[i], response_data); | 1086 EXPECT_EQ(kExpectedResponseData[i], response_data); |
1062 } | 1087 } |
1063 } | 1088 } |
1064 | 1089 |
| 1090 TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionNotConnectedOnWrite) { |
| 1091 MockWrite write_failure(true, ERR_SOCKET_NOT_CONNECTED); |
| 1092 KeepAliveConnectionResendRequestTest(&write_failure, NULL); |
| 1093 } |
| 1094 |
1065 TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionReset) { | 1095 TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionReset) { |
1066 MockRead read_failure(true, ERR_CONNECTION_RESET); | 1096 MockRead read_failure(true, ERR_CONNECTION_RESET); |
1067 KeepAliveConnectionResendRequestTest(read_failure); | 1097 KeepAliveConnectionResendRequestTest(NULL, &read_failure); |
1068 } | 1098 } |
1069 | 1099 |
1070 TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionEOF) { | 1100 TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionEOF) { |
1071 MockRead read_failure(false, OK); // EOF | 1101 MockRead read_failure(false, OK); // EOF |
1072 KeepAliveConnectionResendRequestTest(read_failure); | 1102 KeepAliveConnectionResendRequestTest(NULL, &read_failure); |
1073 } | 1103 } |
1074 | 1104 |
1075 TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionReset) { | 1105 TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionReset) { |
1076 HttpRequestInfo request; | 1106 HttpRequestInfo request; |
1077 request.method = "GET"; | 1107 request.method = "GET"; |
1078 request.url = GURL("http://www.google.com/"); | 1108 request.url = GURL("http://www.google.com/"); |
1079 request.load_flags = 0; | 1109 request.load_flags = 0; |
1080 | 1110 |
1081 SessionDependencies session_deps; | 1111 SessionDependencies session_deps; |
1082 scoped_ptr<HttpTransaction> trans( | 1112 scoped_ptr<HttpTransaction> trans( |
(...skipping 8553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9636 StaticSocketDataProvider* data[] = { &data1, &data2 }; | 9666 StaticSocketDataProvider* data[] = { &data1, &data2 }; |
9637 | 9667 |
9638 SimpleGetHelperResult out = SimpleGetHelperForData(data, arraysize(data)); | 9668 SimpleGetHelperResult out = SimpleGetHelperForData(data, arraysize(data)); |
9639 | 9669 |
9640 EXPECT_EQ(OK, out.rv); | 9670 EXPECT_EQ(OK, out.rv); |
9641 EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); | 9671 EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
9642 EXPECT_EQ("hello world", out.response_data); | 9672 EXPECT_EQ("hello world", out.response_data); |
9643 } | 9673 } |
9644 | 9674 |
9645 } // namespace net | 9675 } // namespace net |
OLD | NEW |