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

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

Issue 681303003: Add HTTPMultipartBuilder and its test. (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Address comments Created 6 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
OLDNEW
(Empty)
1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/net/http_multipart_builder.h"
16
17 #include <vector>
18
19 #include "gtest/gtest.h"
20 #include "util/net/http_body.h"
21 #include "util/net/http_body_test_util.h"
22
23 namespace crashpad {
24 namespace test {
25 namespace {
26
27 std::vector<std::string> SplitCRLF(const std::string& string) {
28 std::vector<std::string> lines;
29 size_t last_line = 0;
30 for (size_t i = 0; i < string.length(); ++i) {
31 if (string[i] == '\r' && i+1 < string.length() && string[i+1] == '\n') {
32 lines.push_back(string.substr(last_line, i - last_line));
33 last_line = i + 2;
34 ++i;
35 }
36 }
37 // Append any remainder.
38 if (last_line < string.length()) {
39 lines.push_back(string.substr(last_line));
40 }
41 return lines;
42 }
43
44 // In the tests below, the form data pairs don’t appear in the order they were
45 // added. The current implementation uses a std::map which sorts keys, so the
46 // entires appear in alphabetical order. However, this is an implementation
47 // detail, and it’s OK if the writer stops sorting in this order. Testing for
48 // a specific order is just the easiest way to write this test while the writer
49 // will output things in a known order.
50
51 TEST(HTTPMultipartBuilder, ThreeStringFields) {
52 HTTPMultipartBuilder builder;
53
54 const char kKey1[] = "key1";
55 const char kValue1[] = "test";
56 builder.SetFormData(kKey1, kValue1);
57
58 const char kKey2[] = "key2";
59 const char kValue2[] = "This is another test.";
60 builder.SetFormData(kKey2, kValue2);
61
62 const char kKey3[] = "key-three";
63 const char kValue3[] = "More tests";
64 builder.SetFormData(kKey3, kValue3);
65
66 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream());
67 ASSERT_TRUE(body.get());
68 std::string contents = ReadStreamToString(body.get());
69 auto lines = SplitCRLF(contents);
70 auto lines_it = lines.begin();
71
72 // The first line is the boundary. All subsequent boundaries must match this.
73 const std::string& boundary = *lines_it++;
74 EXPECT_GE(boundary.length(), 1u);
75 EXPECT_LE(boundary.length(), 70u);
76
77 EXPECT_EQ("Content-Disposition: form-data; name=\"key-three\"", *lines_it++);
78 EXPECT_EQ("", *lines_it++);
79 EXPECT_EQ(kValue3, *lines_it++);
80
81 EXPECT_EQ(boundary, *lines_it++);
82 EXPECT_EQ("Content-Disposition: form-data; name=\"key1\"", *lines_it++);
83 EXPECT_EQ("", *lines_it++);
84 EXPECT_EQ(kValue1, *lines_it++);
85
86 EXPECT_EQ(boundary, *lines_it++);
87 EXPECT_EQ("Content-Disposition: form-data; name=\"key2\"", *lines_it++);
88 EXPECT_EQ("", *lines_it++);
89 EXPECT_EQ(kValue2, *lines_it++);
90
91 // The initial boundary is preceded by "--", whereas this final boundary ends
92 // with "--".
Mark Mentovai 2014/10/29 21:29:14 See the comment in the other file. I think that fi
93 EXPECT_EQ(boundary.substr(2) + "--", *lines_it++);
94
95 EXPECT_EQ(lines.end(), lines_it);
96 }
97
98 TEST(HTTPMultipartBuilder, ThreeFileAttachments) {
99 HTTPMultipartBuilder builder;
100 // TODO(rsesek): Use a more robust mechanism to locate testdata
101 // <https://code.google.com/p/crashpad/issues/detail?id=4>.
102 builder.SetFileAttachment("first", "minidump.dmp",
103 base::FilePath("util/net/testdata/ascii_http_body.txt"), "");
104 builder.SetFileAttachment("second", "minidump.dmp",
105 base::FilePath("util/net/testdata/ascii_http_body.txt"), "text/plain");
106 builder.SetFileAttachment("\"third 50% silly\"", "test.txt",
107 base::FilePath("util/net/testdata/ascii_http_body.txt"), "text/plain");
108
109 const char kFileContents[] = "This is a test.\n";
110
111 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream());
112 ASSERT_TRUE(body.get());
113 std::string contents = ReadStreamToString(body.get());
114 auto lines = SplitCRLF(contents);
115 auto lines_it = lines.begin();
116
117 const std::string& boundary = *lines_it++;
118 EXPECT_GE(boundary.length(), 1u);
119 EXPECT_LE(boundary.length(), 70u);
120
121 EXPECT_EQ("Content-Disposition: form-data; "
122 "name=\"%22third 50%25 silly%22\"; filename=\"test.txt\"",
123 *lines_it++);
124 EXPECT_EQ("Content-Type: text/plain", *lines_it++);
125 EXPECT_EQ("", *lines_it++);
126 EXPECT_EQ(kFileContents, *lines_it++);
127
128 EXPECT_EQ(boundary, *lines_it++);
129 EXPECT_EQ("Content-Disposition: form-data; "
130 "name=\"first\"; filename=\"minidump.dmp\"",
131 *lines_it++);
132 EXPECT_EQ("Content-Type: application/octet-stream", *lines_it++);
133 EXPECT_EQ("", *lines_it++);
134 EXPECT_EQ(kFileContents, *lines_it++);
135
136 EXPECT_EQ(boundary, *lines_it++);
137 EXPECT_EQ("Content-Disposition: form-data; "
138 "name=\"second\"; filename=\"minidump.dmp\"",
139 *lines_it++);
140 EXPECT_EQ("Content-Type: text/plain", *lines_it++);
141 EXPECT_EQ("", *lines_it++);
142 EXPECT_EQ(kFileContents, *lines_it++);
143
144 // The initial boundary is preceded by "--", whereas this final boundary ends
145 // with "--".
146 EXPECT_EQ(boundary.substr(2) + "--", *lines_it++);
147
148 EXPECT_EQ(lines.end(), lines_it);
149 }
150
151 TEST(HTTPMultipartBuilder, OverwriteFormDataWithEscapedKey) {
152 HTTPMultipartBuilder builder;
153 const char kKey[] = "a 100% \"silly\"\r\ntest";
154 builder.SetFormData(kKey, "some dummy value");
155 builder.SetFormData(kKey, "overwrite");
156 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream());
157 ASSERT_TRUE(body.get());
158 std::string contents = ReadStreamToString(body.get());
159 auto lines = SplitCRLF(contents);
160 auto lines_it = lines.begin();
161
162 const std::string& boundary = *lines_it++;
163 EXPECT_GE(boundary.length(), 1u);
164 EXPECT_LE(boundary.length(), 70u);
165
166 EXPECT_EQ(
167 "Content-Disposition: form-data; name=\"a 100%25 %22silly%22%0d%0atest\"",
168 *lines_it++);
169 EXPECT_EQ("", *lines_it++);
170 EXPECT_EQ("overwrite", *lines_it++);
171 // The initial boundary is preceded by "--", whereas this final boundary ends
172 // with "--".
173 EXPECT_EQ(boundary.substr(2) + "--", *lines_it++);
174 EXPECT_EQ(lines.end(), lines_it);
175 }
176
177 TEST(HTTPMultipartBuilder, OverwriteFileAttachment) {
178 HTTPMultipartBuilder builder;
179 const char kValue[] = "1 2 3 test";
180 builder.SetFormData("a key", kValue);
181 // TODO(rsesek): Use a more robust mechanism to locate testdata
182 // <https://code.google.com/p/crashpad/issues/detail?id=4>.
183 builder.SetFileAttachment("minidump", "minidump.dmp",
184 base::FilePath("util/net/testdata/binary_http_body.dat"), "");
185 builder.SetFileAttachment("minidump2", "minidump.dmp",
186 base::FilePath("util/net/testdata/binary_http_body.dat"), "");
187 builder.SetFileAttachment("minidump", "minidump.dmp",
188 base::FilePath("util/net/testdata/ascii_http_body.txt"), "text/plain");
189 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream());
190 ASSERT_TRUE(body.get());
191 std::string contents = ReadStreamToString(body.get());
192 auto lines = SplitCRLF(contents);
193 auto lines_it = lines.begin();
194
195 const std::string& boundary = *lines_it++;
196 EXPECT_GE(boundary.length(), 1u);
197 EXPECT_LE(boundary.length(), 70u);
198
199 EXPECT_EQ("Content-Disposition: form-data; name=\"a key\"", *lines_it++);
200 EXPECT_EQ("", *lines_it++);
201 EXPECT_EQ(kValue, *lines_it++);
202
203 EXPECT_EQ(boundary, *lines_it++);
204 EXPECT_EQ("Content-Disposition: form-data; "
205 "name=\"minidump\"; filename=\"minidump.dmp\"",
206 *lines_it++);
207 EXPECT_EQ("Content-Type: text/plain", *lines_it++);
208 EXPECT_EQ("", *lines_it++);
209 EXPECT_EQ("This is a test.\n", *lines_it++);
210
211 EXPECT_EQ(boundary, *lines_it++);
212 EXPECT_EQ("Content-Disposition: form-data; "
213 "name=\"minidump2\"; filename=\"minidump.dmp\"",
214 *lines_it++);
215 EXPECT_EQ("Content-Type: application/octet-stream", *lines_it++);
216 EXPECT_EQ("", *lines_it++);
217 EXPECT_EQ("\xFE\xED\xFA\xCE\xA1\x1A\x15", *lines_it++);
218
219 // The initial boundary is preceded by "--", whereas this final boundary ends
220 // with "--".
221 EXPECT_EQ(boundary.substr(2) + "--", *lines_it++);
222
223 EXPECT_EQ(lines.end(), lines_it);
224 }
225
226 TEST(HTTPMultipartBuilder, SharedFormDataAndAttachmentKeyNamespace) {
227 HTTPMultipartBuilder builder;
228 const char kValue1[] = "11111";
229 builder.SetFormData("one", kValue1);
230 builder.SetFileAttachment("minidump", "minidump.dmp",
231 base::FilePath("util/net/testdata/ascii_http_body.txt"), "");
232 const char kValue2[] = "this is not a file";
233 builder.SetFormData("minidump", kValue2);
234
235 scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream());
236 ASSERT_TRUE(body.get());
237 std::string contents = ReadStreamToString(body.get());
238 auto lines = SplitCRLF(contents);
239 auto lines_it = lines.begin();
240
241 const std::string& boundary = *lines_it++;
242 EXPECT_GE(boundary.length(), 1u);
243 EXPECT_LE(boundary.length(), 70u);
244
245 EXPECT_EQ("Content-Disposition: form-data; name=\"minidump\"", *lines_it++);
246 EXPECT_EQ("", *lines_it++);
247 EXPECT_EQ(kValue2, *lines_it++);
248
249 EXPECT_EQ(boundary, *lines_it++);
250 EXPECT_EQ("Content-Disposition: form-data; name=\"one\"", *lines_it++);
251 EXPECT_EQ("", *lines_it++);
252 EXPECT_EQ(kValue1, *lines_it++);
253
254 // The initial boundary is preceded by "--", whereas this final boundary ends
255 // with "--".
256 EXPECT_EQ(boundary.substr(2) + "--", *lines_it++);
257
258 EXPECT_EQ(lines.end(), lines_it);
259 }
260
261 } // namespace
262 } // namespace test
263 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698