Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: util/net/http_transport_test.cc

Issue 1451793002: Add RandomString() and its test, and use it everywhere it makes sense (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Fix tpyo Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/misc/random_string_test.cc ('k') | util/net/http_transport_test_server.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Crashpad Authors. All rights reserved. 1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include "util/net/http_transport.h" 15 #include "util/net/http_transport.h"
16 16
17 #include <stdint.h> 17 #include <stdint.h>
18 #include <stdlib.h> 18 #include <stdlib.h>
19 #include <string.h> 19 #include <string.h>
20 20
21 #include <vector> 21 #include <vector>
22 22
23 #include "base/files/file_path.h" 23 #include "base/files/file_path.h"
24 #include "base/format_macros.h" 24 #include "base/format_macros.h"
25 #include "base/logging.h" 25 #include "base/logging.h"
26 #include "base/memory/scoped_ptr.h" 26 #include "base/memory/scoped_ptr.h"
27 #include "base/rand_util.h"
28 #include "base/strings/stringprintf.h" 27 #include "base/strings/stringprintf.h"
29 #include "base/strings/utf_string_conversions.h" 28 #include "base/strings/utf_string_conversions.h"
30 #include "build/build_config.h" 29 #include "build/build_config.h"
31 #include "gtest/gtest.h" 30 #include "gtest/gtest.h"
32 #include "test/multiprocess_exec.h" 31 #include "test/multiprocess_exec.h"
33 #include "test/paths.h" 32 #include "test/paths.h"
34 #include "util/file/file_io.h" 33 #include "util/file/file_io.h"
34 #include "util/misc/random_string.h"
35 #include "util/net/http_body.h" 35 #include "util/net/http_body.h"
36 #include "util/net/http_headers.h" 36 #include "util/net/http_headers.h"
37 #include "util/net/http_multipart_builder.h" 37 #include "util/net/http_multipart_builder.h"
38 38
39 namespace crashpad { 39 namespace crashpad {
40 namespace test { 40 namespace test {
41 namespace { 41 namespace {
42 42
43 class HTTPTransportTestFixture : public MultiprocessExec { 43 class HTTPTransportTestFixture : public MultiprocessExec {
44 public: 44 public:
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 ASSERT_TRUE(LoggingReadFile(ReadPipeHandle(), &port, sizeof(port))); 82 ASSERT_TRUE(LoggingReadFile(ReadPipeHandle(), &port, sizeof(port)));
83 83
84 // Then the parent will tell the web server what response code to send 84 // Then the parent will tell the web server what response code to send
85 // for the HTTP request. 85 // for the HTTP request.
86 ASSERT_TRUE(LoggingWriteFile( 86 ASSERT_TRUE(LoggingWriteFile(
87 WritePipeHandle(), &response_code_, sizeof(response_code_))); 87 WritePipeHandle(), &response_code_, sizeof(response_code_)));
88 88
89 // The parent will also tell the web server what response body to send back. 89 // The parent will also tell the web server what response body to send back.
90 // The web server will only send the response body if the response code is 90 // The web server will only send the response body if the response code is
91 // 200. 91 // 200.
92 std::string expect_response_body; 92 const std::string random_string = RandomString();
93 for (size_t index = 0; index < 8; ++index) {
94 expect_response_body += static_cast<char>(base::RandInt(' ', '~'));
95 }
96 93
97 ASSERT_TRUE(LoggingWriteFile(WritePipeHandle(), 94 ASSERT_TRUE(LoggingWriteFile(WritePipeHandle(),
98 expect_response_body.c_str(), 95 random_string.c_str(),
99 expect_response_body.size())); 96 random_string.size()));
100 97
101 // Now execute the HTTP request. 98 // Now execute the HTTP request.
102 scoped_ptr<HTTPTransport> transport(HTTPTransport::Create()); 99 scoped_ptr<HTTPTransport> transport(HTTPTransport::Create());
103 transport->SetMethod("POST"); 100 transport->SetMethod("POST");
104 transport->SetURL(base::StringPrintf("http://127.0.0.1:%d/upload", port)); 101 transport->SetURL(base::StringPrintf("http://127.0.0.1:%d/upload", port));
105 for (const auto& pair : headers_) { 102 for (const auto& pair : headers_) {
106 transport->SetHeader(pair.first, pair.second); 103 transport->SetHeader(pair.first, pair.second);
107 } 104 }
108 transport->SetBodyStream(body_stream_.Pass()); 105 transport->SetBodyStream(body_stream_.Pass());
109 106
110 std::string response_body; 107 std::string response_body;
111 bool success = transport->ExecuteSynchronously(&response_body); 108 bool success = transport->ExecuteSynchronously(&response_body);
112 if (response_code_ == 200) { 109 if (response_code_ == 200) {
113 EXPECT_TRUE(success); 110 EXPECT_TRUE(success);
114 expect_response_body += "\r\n"; 111 std::string expect_response_body = random_string + "\r\n";
115 EXPECT_EQ(expect_response_body, response_body); 112 EXPECT_EQ(expect_response_body, response_body);
116 } else { 113 } else {
117 EXPECT_FALSE(success); 114 EXPECT_FALSE(success);
118 EXPECT_TRUE(response_body.empty()); 115 EXPECT_TRUE(response_body.empty());
119 } 116 }
120 117
121 // Read until the child's stdout closes. 118 // Read until the child's stdout closes.
122 std::string request; 119 std::string request;
123 char buf[32]; 120 char buf[32];
124 FileOperationResult bytes_read; 121 FileOperationResult bytes_read;
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 } 304 }
308 305
309 TEST(HTTPTransport, Upload33k_LengthUnknown) { 306 TEST(HTTPTransport, Upload33k_LengthUnknown) {
310 // The same as Upload33k, but without declaring Content-Length ahead of time. 307 // The same as Upload33k, but without declaring Content-Length ahead of time.
311 RunUpload33k(false); 308 RunUpload33k(false);
312 } 309 }
313 310
314 } // namespace 311 } // namespace
315 } // namespace test 312 } // namespace test
316 } // namespace crashpad 313 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/misc/random_string_test.cc ('k') | util/net/http_transport_test_server.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698