OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
15 #include "net/base/auth.h" | 15 #include "net/base/auth.h" |
| 16 #include "net/base/io_buffer.h" |
16 #include "net/base/net_log_unittest.h" | 17 #include "net/base/net_log_unittest.h" |
| 18 #include "net/base/test_data_stream.h" |
17 #include "net/base/upload_bytes_element_reader.h" | 19 #include "net/base/upload_bytes_element_reader.h" |
18 #include "net/base/upload_data_stream.h" | 20 #include "net/base/upload_data_stream.h" |
19 #include "net/base/upload_file_element_reader.h" | 21 #include "net/base/upload_file_element_reader.h" |
20 #include "net/http/http_network_session_peer.h" | 22 #include "net/http/http_network_session_peer.h" |
21 #include "net/http/http_transaction_unittest.h" | 23 #include "net/http/http_transaction_unittest.h" |
22 #include "net/socket/client_socket_pool_base.h" | 24 #include "net/socket/client_socket_pool_base.h" |
23 #include "net/spdy/buffered_spdy_framer.h" | 25 #include "net/spdy/buffered_spdy_framer.h" |
24 #include "net/spdy/spdy_http_stream.h" | 26 #include "net/spdy/spdy_http_stream.h" |
25 #include "net/spdy/spdy_http_utils.h" | 27 #include "net/spdy/spdy_http_utils.h" |
26 #include "net/spdy/spdy_session.h" | 28 #include "net/spdy/spdy_session.h" |
(...skipping 3895 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3922 MessageLoop::current()->RunUntilIdle(); | 3924 MessageLoop::current()->RunUntilIdle(); |
3923 | 3925 |
3924 // Verify that we consumed all test data. | 3926 // Verify that we consumed all test data. |
3925 helper.VerifyDataConsumed(); | 3927 helper.VerifyDataConsumed(); |
3926 | 3928 |
3927 EXPECT_EQ(OK, out.rv); | 3929 EXPECT_EQ(OK, out.rv); |
3928 EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); | 3930 EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
3929 EXPECT_EQ("messagemessagemessagemessage", out.response_data); | 3931 EXPECT_EQ("messagemessagemessagemessage", out.response_data); |
3930 } | 3932 } |
3931 | 3933 |
| 3934 // Verify the case where SpdySession reads all the data synchronously without |
| 3935 // yielding. |
| 3936 TEST_P(SpdyNetworkTransactionSpdy2Test, BufferedSyncRead) { |
| 3937 BufferedSpdyFramer framer(2, false); |
| 3938 |
| 3939 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
| 3940 MockWrite writes[] = { CreateMockWrite(*req) }; |
| 3941 |
| 3942 scoped_ptr<SpdyFrame> syn_reply( |
| 3943 ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 3944 syn_reply->set_flags(CONTROL_FLAG_NONE); // Turn off FIN bit. |
| 3945 |
| 3946 const int kPayloadSize = 1600; |
| 3947 net::TestDataStream test_stream; |
| 3948 scoped_refptr<net::IOBuffer> payload(new net::IOBuffer(kPayloadSize)); |
| 3949 test_stream.GetBytes(payload->data(), kPayloadSize); |
| 3950 char* payload_data = payload->data(); |
| 3951 std::string payload_content(payload_data, kPayloadSize); |
| 3952 scoped_ptr<SpdyFrame> data_frame( |
| 3953 framer.CreateDataFrame(1, payload_data, kPayloadSize, DATA_FLAG_FIN)); |
| 3954 const SpdyFrame* frames[2] = { |
| 3955 syn_reply.get(), |
| 3956 data_frame.get() |
| 3957 }; |
| 3958 char combined_frames[5000]; |
| 3959 int combined_frames_len = |
| 3960 CombineFrames(frames, arraysize(frames), |
| 3961 combined_frames, arraysize(combined_frames)); |
| 3962 |
| 3963 MockRead reads[] = { |
| 3964 MockRead(ASYNC, combined_frames, combined_frames_len), |
| 3965 MockRead(ASYNC, 0, 0) // EOF |
| 3966 }; |
| 3967 |
| 3968 DelayedSocketData data(1, reads, arraysize(reads), |
| 3969 writes, arraysize(writes)); |
| 3970 |
| 3971 NormalSpdyTransactionHelper helper(CreateGetRequest(), |
| 3972 BoundNetLog(), GetParam(), NULL); |
| 3973 helper.RunPreTestSetup(); |
| 3974 helper.AddData(&data); |
| 3975 HttpNetworkTransaction* trans = helper.trans(); |
| 3976 |
| 3977 TestCompletionCallback callback; |
| 3978 int rv = trans->Start( |
| 3979 &CreateGetRequest(), callback.callback(), BoundNetLog()); |
| 3980 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3981 |
| 3982 TransactionHelperResult out = helper.output(); |
| 3983 out.rv = callback.WaitForResult(); |
| 3984 EXPECT_EQ(out.rv, OK); |
| 3985 |
| 3986 const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3987 EXPECT_TRUE(response->headers != NULL); |
| 3988 EXPECT_TRUE(response->was_fetched_via_spdy); |
| 3989 out.status_line = response->headers->GetStatusLine(); |
| 3990 out.response_info = *response; // Make a copy so we can verify. |
| 3991 |
| 3992 // Read Data. |
| 3993 TestCompletionCallback read_callback; |
| 3994 |
| 3995 std::string content; |
| 3996 int reads_completed = 0; |
| 3997 do { |
| 3998 // Read data in chunks. |
| 3999 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kPayloadSize)); |
| 4000 rv = trans->Read(buf, kPayloadSize, read_callback.callback()); |
| 4001 if (rv > 0) { |
| 4002 content.append(buf->data(), rv); |
| 4003 } else if (rv < 0) { |
| 4004 FAIL() << "Unexpected read error: " << rv; |
| 4005 } |
| 4006 reads_completed++; |
| 4007 } while (rv > 0); |
| 4008 |
| 4009 out.response_data.swap(content); |
| 4010 |
| 4011 // Flush the MessageLoop while the SpdySessionDependencies (in particular, the |
| 4012 // MockClientSocketFactory) are still alive. |
| 4013 MessageLoop::current()->RunUntilIdle(); |
| 4014 |
| 4015 // Verify that we consumed all test data. |
| 4016 helper.VerifyDataConsumed(); |
| 4017 |
| 4018 EXPECT_EQ(OK, out.rv); |
| 4019 EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 4020 EXPECT_EQ(payload_content, out.response_data); |
| 4021 } |
| 4022 |
| 4023 // Verify the case where SpdySession reads kMaxReadBytes data synchronously and |
| 4024 // then reads more data after yielding. |
| 4025 TEST_P(SpdyNetworkTransactionSpdy2Test, BufferedASyncRead) { |
| 4026 BufferedSpdyFramer framer(2, false); |
| 4027 |
| 4028 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
| 4029 MockWrite writes[] = { CreateMockWrite(*req) }; |
| 4030 |
| 4031 scoped_ptr<SpdyFrame> syn_reply( |
| 4032 ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 4033 syn_reply->set_flags(CONTROL_FLAG_NONE); // Turn off FIN bit. |
| 4034 |
| 4035 const int kPayloadSize = kMaxReadBytes * 2; |
| 4036 net::TestDataStream test_stream; |
| 4037 scoped_refptr<net::IOBuffer> payload(new net::IOBuffer(kPayloadSize)); |
| 4038 test_stream.GetBytes(payload->data(), kPayloadSize); |
| 4039 char* payload_data = payload->data(); |
| 4040 std::string payload_content(payload_data, kPayloadSize); |
| 4041 scoped_ptr<SpdyFrame> data_frame( |
| 4042 framer.CreateDataFrame(1, payload_data, kPayloadSize, DATA_FLAG_FIN)); |
| 4043 const SpdyFrame* frames[2] = { |
| 4044 syn_reply.get(), |
| 4045 data_frame.get() |
| 4046 }; |
| 4047 int heap_size = kPayloadSize * 2; |
| 4048 scoped_ptr_malloc<char> combined_frames( |
| 4049 reinterpret_cast<char*>(malloc(heap_size))); |
| 4050 CHECK(combined_frames.get()); |
| 4051 int combined_frames_len = |
| 4052 CombineFrames(frames, arraysize(frames), |
| 4053 combined_frames.get(), heap_size); |
| 4054 |
| 4055 MockRead reads[] = { |
| 4056 MockRead(ASYNC, combined_frames.get(), combined_frames_len), |
| 4057 MockRead(ASYNC, 0, 0) // EOF |
| 4058 }; |
| 4059 |
| 4060 DelayedSocketData data(1, reads, arraysize(reads), |
| 4061 writes, arraysize(writes)); |
| 4062 |
| 4063 NormalSpdyTransactionHelper helper(CreateGetRequest(), |
| 4064 BoundNetLog(), GetParam(), NULL); |
| 4065 helper.RunPreTestSetup(); |
| 4066 helper.AddData(&data); |
| 4067 HttpNetworkTransaction* trans = helper.trans(); |
| 4068 |
| 4069 TestCompletionCallback callback; |
| 4070 int rv = trans->Start( |
| 4071 &CreateGetRequest(), callback.callback(), BoundNetLog()); |
| 4072 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4073 |
| 4074 TransactionHelperResult out = helper.output(); |
| 4075 out.rv = callback.WaitForResult(); |
| 4076 EXPECT_EQ(out.rv, OK); |
| 4077 |
| 4078 const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4079 EXPECT_TRUE(response->headers != NULL); |
| 4080 EXPECT_TRUE(response->was_fetched_via_spdy); |
| 4081 out.status_line = response->headers->GetStatusLine(); |
| 4082 out.response_info = *response; // Make a copy so we can verify. |
| 4083 |
| 4084 // Read Data. |
| 4085 TestCompletionCallback read_callback; |
| 4086 |
| 4087 std::string content; |
| 4088 int reads_completed = 0; |
| 4089 do { |
| 4090 // Read data in chunks. |
| 4091 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kMaxReadBytes)); |
| 4092 rv = trans->Read(buf, kMaxReadBytes, read_callback.callback()); |
| 4093 if (rv > 0) { |
| 4094 content.append(buf->data(), rv); |
| 4095 } else if (rv < 0) { |
| 4096 FAIL() << "Unexpected read error: " << rv; |
| 4097 } |
| 4098 reads_completed++; |
| 4099 } while (rv > 0); |
| 4100 |
| 4101 out.response_data.swap(content); |
| 4102 |
| 4103 // Flush the MessageLoop while the SpdySessionDependencies (in particular, the |
| 4104 // MockClientSocketFactory) are still alive. |
| 4105 MessageLoop::current()->RunUntilIdle(); |
| 4106 |
| 4107 // Verify that we consumed all test data. |
| 4108 helper.VerifyDataConsumed(); |
| 4109 |
| 4110 EXPECT_EQ(OK, out.rv); |
| 4111 EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 4112 EXPECT_EQ(payload_content, out.response_data); |
| 4113 } |
| 4114 |
3932 // Verify the case where we buffer data and close the connection. | 4115 // Verify the case where we buffer data and close the connection. |
3933 TEST_P(SpdyNetworkTransactionSpdy2Test, BufferedClosed) { | 4116 TEST_P(SpdyNetworkTransactionSpdy2Test, BufferedClosed) { |
3934 BufferedSpdyFramer framer(2, false); | 4117 BufferedSpdyFramer framer(2, false); |
3935 | 4118 |
3936 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); | 4119 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
3937 MockWrite writes[] = { CreateMockWrite(*req) }; | 4120 MockWrite writes[] = { CreateMockWrite(*req) }; |
3938 | 4121 |
3939 // All data frames in a single read. | 4122 // All data frames in a single read. |
3940 // NOTE: We don't FIN the stream. | 4123 // NOTE: We don't FIN the stream. |
3941 scoped_ptr<SpdyFrame> data_frame( | 4124 scoped_ptr<SpdyFrame> data_frame( |
(...skipping 1831 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5773 // And now we can allow everything else to run to completion. | 5956 // And now we can allow everything else to run to completion. |
5774 data.SetStop(10); | 5957 data.SetStop(10); |
5775 data.Run(); | 5958 data.Run(); |
5776 EXPECT_EQ(OK, callback2.WaitForResult()); | 5959 EXPECT_EQ(OK, callback2.WaitForResult()); |
5777 EXPECT_EQ(OK, callback3.WaitForResult()); | 5960 EXPECT_EQ(OK, callback3.WaitForResult()); |
5778 | 5961 |
5779 helper.VerifyDataConsumed(); | 5962 helper.VerifyDataConsumed(); |
5780 } | 5963 } |
5781 | 5964 |
5782 } // namespace net | 5965 } // namespace net |
OLD | NEW |