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

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

Issue 885183004: HTTPTransport: callers should be able to obtain the HTTP response body (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Update other comment Created 5 years, 10 months 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/net/http_transport_mac.mm ('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/format_macros.h" 23 #include "base/format_macros.h"
24 #include "base/logging.h" 24 #include "base/logging.h"
25 #include "base/memory/scoped_ptr.h" 25 #include "base/memory/scoped_ptr.h"
26 #include "base/rand_util.h"
26 #include "base/strings/stringprintf.h" 27 #include "base/strings/stringprintf.h"
27 #include "build/build_config.h" 28 #include "build/build_config.h"
28 #include "gtest/gtest.h" 29 #include "gtest/gtest.h"
29 #include "util/file/file_io.h" 30 #include "util/file/file_io.h"
30 #include "util/net/http_body.h" 31 #include "util/net/http_body.h"
31 #include "util/net/http_headers.h" 32 #include "util/net/http_headers.h"
32 #include "util/net/http_multipart_builder.h" 33 #include "util/net/http_multipart_builder.h"
33 #include "util/test/multiprocess_exec.h" 34 #include "util/test/multiprocess_exec.h"
34 35
35 namespace crashpad { 36 namespace crashpad {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 // The child will write the HTTP server port number as a packed unsigned 76 // The child will write the HTTP server port number as a packed unsigned
76 // short to stdout. 77 // short to stdout.
77 uint16_t port; 78 uint16_t port;
78 ASSERT_TRUE(LoggingReadFile(ReadPipeHandle(), &port, sizeof(port))); 79 ASSERT_TRUE(LoggingReadFile(ReadPipeHandle(), &port, sizeof(port)));
79 80
80 // Then the parent will tell the web server what response code to send 81 // Then the parent will tell the web server what response code to send
81 // for the HTTP request. 82 // for the HTTP request.
82 ASSERT_TRUE(LoggingWriteFile( 83 ASSERT_TRUE(LoggingWriteFile(
83 WritePipeHandle(), &response_code_, sizeof(response_code_))); 84 WritePipeHandle(), &response_code_, sizeof(response_code_)));
84 85
86 // The parent will also tell the web server what response body to send back.
87 // The web server will only send the response body if the response code is
88 // 200.
89 std::string expect_response_body;
90 for (size_t index = 0; index < 8; ++index) {
91 expect_response_body += static_cast<char>(base::RandInt(' ', '~'));
92 }
93
94 ASSERT_TRUE(LoggingWriteFile(WritePipeHandle(),
95 expect_response_body.c_str(),
96 expect_response_body.size()));
97
85 // Now execute the HTTP request. 98 // Now execute the HTTP request.
86 scoped_ptr<HTTPTransport> transport(HTTPTransport::Create()); 99 scoped_ptr<HTTPTransport> transport(HTTPTransport::Create());
87 transport->SetMethod("POST"); 100 transport->SetMethod("POST");
88 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));
89 for (const auto& pair : headers_) { 102 for (const auto& pair : headers_) {
90 transport->SetHeader(pair.first, pair.second); 103 transport->SetHeader(pair.first, pair.second);
91 } 104 }
92 transport->SetBodyStream(body_stream_.Pass()); 105 transport->SetBodyStream(body_stream_.Pass());
93 106
94 EXPECT_EQ(transport->ExecuteSynchronously(), (response_code_ == 200)); 107 std::string response_body;
108 bool success = transport->ExecuteSynchronously(&response_body);
109 if (response_code_ == 200) {
110 EXPECT_TRUE(success);
111 expect_response_body += "\r\n";
112 EXPECT_EQ(expect_response_body, response_body);
113 } else {
114 EXPECT_FALSE(success);
115 EXPECT_TRUE(response_body.empty());
116 }
95 117
96 // Read until the child's stdout closes. 118 // Read until the child's stdout closes.
97 std::string request; 119 std::string request;
98 char buf[32]; 120 char buf[32];
99 ssize_t bytes_read; 121 ssize_t bytes_read;
100 while ((bytes_read = ReadFile(ReadPipeHandle(), buf, sizeof(buf))) != 0) { 122 while ((bytes_read = ReadFile(ReadPipeHandle(), buf, sizeof(buf))) != 0) {
101 ASSERT_GE(bytes_read, 0); 123 ASSERT_GE(bytes_read, 0);
102 request.append(buf, bytes_read); 124 request.append(buf, bytes_read);
103 } 125 }
104 126
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 headers[kContentLength] = base::StringPrintf("%" PRIuS, strlen(kTextBody)); 271 headers[kContentLength] = base::StringPrintf("%" PRIuS, strlen(kTextBody));
250 272
251 HTTPTransportTestFixture test(headers, body_stream.Pass(), 200, 273 HTTPTransportTestFixture test(headers, body_stream.Pass(), 200,
252 &UnchunkedPlainText); 274 &UnchunkedPlainText);
253 test.Run(); 275 test.Run();
254 } 276 }
255 277
256 } // namespace 278 } // namespace
257 } // namespace test 279 } // namespace test
258 } // namespace crashpad 280 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/net/http_transport_mac.mm ('k') | util/net/http_transport_test_server.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698