summaryrefslogtreecommitdiff
path: root/chromium/net/url_request/redirect_util_unittest.cc
blob: 5019f2c86a83fb1d416b7ba21c0cdc3d49761302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/url_request/redirect_util.h"

#include <string>

#include "net/http/http_request_headers.h"
#include "net/url_request/redirect_info.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace net {
namespace {

TEST(RedirectUtilTest, UpdateHttpRequest) {
  const GURL original_url("https://www.example.com/test.php");
  const char kContentLengthValue[] = "100";
  const char kContentTypeValue[] = "text/plain; charset=utf-8";
  const char kContentEncoding[] = "Content-Encoding";
  const char kContentEncodingValue[] = "gzip";
  const char kContentLanguage[] = "Content-Language";
  const char kContentLanguageValue[] = "tlh";
  const char kContentLocation[] = "Content-Location";
  const char kContentLocationValue[] = "https://somewhere.test/";
  const char kCustomHeader[] = "Custom-Header-For-Test";
  const char kCustomHeaderValue[] = "custom header value";

  struct TestCase {
    const char* original_method;
    const char* new_method;
    const char* new_url;
    const struct {
      const char* name;
      const char* value;
    } modified_headers[2];
    bool expected_should_clear_upload;
    // nullptr if the origin header should not exist
    const char* expected_origin_header;
  };
  const TestCase kTests[] = {
      {
          "POST" /* original_method */,
          "POST" /* new_method */,
          "https://www.example.com/redirected.php" /* new_url */,
          {{"Header1", "Value1"}, {"Header2", "Value2"}} /* modified_headers */,
          false /* expected_should_clear_upload */,
          "https://origin.example.com" /* expected_origin_header */
      },
      {
          "POST" /* original_method */,
          "GET" /* new_method */,
          "https://www.example.com/redirected.php" /* new_url */,
          {{"Header1", "Value1"}, {"Header2", "Value2"}} /* modified_headers */,
          true /* expected_should_clear_upload */,
          nullptr /* expected_origin_header */
      },
      {
          "POST" /* original_method */,
          "POST" /* new_method */,
          "https://other.example.com/redirected.php" /* new_url */,
          {{"Header1", "Value1"}, {"Header2", "Value2"}} /* modified_headers */,
          false /* expected_should_clear_upload */,
          "null" /* expected_origin_header */
      },
      {
          "POST" /* original_method */,
          "GET" /* new_method */,
          "https://other.example.com/redirected.php" /* new_url */,
          {{"Header1", "Value1"}, {"Header2", "Value2"}} /* modified_headers */,
          true /* expected_should_clear_upload */,
          nullptr /* expected_origin_header */
      },
      {
          "PUT" /* original_method */,
          "GET" /* new_method */,
          "https://www.example.com/redirected.php" /* new_url */,
          {{"Header1", "Value1"}, {"Header2", "Value2"}} /* modified_headers */,
          true /* expected_should_clear_upload */,
          nullptr /* expected_origin_header */
      },
      {
          "FOOT" /* original_method */,
          "GET" /* new_method */,
          "https://www.example.com/redirected.php" /* new_url */,
          {{"Header1", "Value1"}, {"Header2", "Value2"}} /* modified_headers */,
          true /* expected_should_clear_upload */,
          nullptr /* expected_origin_header */
      },
  };

  for (const auto& test : kTests) {
    SCOPED_TRACE(::testing::Message()
                 << "original_method: " << test.original_method
                 << " new_method: " << test.new_method
                 << " new_url: " << test.new_url);
    RedirectInfo redirect_info;
    redirect_info.new_method = test.new_method;
    redirect_info.new_url = GURL(test.new_url);

    net::HttpRequestHeaders modified_headers;
    for (const auto& headers : test.modified_headers) {
      ASSERT_TRUE(!!headers.name);  // Currently all test case has this.
      modified_headers.SetHeader(headers.name, headers.value);
    }
    std::string expected_modified_header1, expected_modified_header2;
    modified_headers.GetHeader("Header1", &expected_modified_header1);
    modified_headers.GetHeader("Header2", &expected_modified_header2);

    HttpRequestHeaders request_headers;
    request_headers.SetHeader(HttpRequestHeaders::kOrigin,
                              "https://origin.example.com");
    request_headers.SetHeader(HttpRequestHeaders::kContentLength,
                              kContentLengthValue);
    request_headers.SetHeader(HttpRequestHeaders::kContentType,
                              kContentTypeValue);
    request_headers.SetHeader(kContentEncoding, kContentEncodingValue);
    request_headers.SetHeader(kContentLanguage, kContentLanguageValue);
    request_headers.SetHeader(kContentLocation, kContentLocationValue);
    request_headers.SetHeader(kCustomHeader, kCustomHeaderValue);
    request_headers.SetHeader("Header1", "Initial-Value1");

    bool should_clear_upload = !test.expected_should_clear_upload;

    RedirectUtil::UpdateHttpRequest(
        original_url, test.original_method, redirect_info,
        absl::nullopt /* removed_headers */, modified_headers, &request_headers,
        &should_clear_upload);
    EXPECT_EQ(test.expected_should_clear_upload, should_clear_upload);

    std::string content_length;
    EXPECT_EQ(!test.expected_should_clear_upload,
              request_headers.GetHeader(HttpRequestHeaders::kContentLength,
                                        &content_length));
    std::string content_type;
    EXPECT_EQ(!test.expected_should_clear_upload,
              request_headers.GetHeader(HttpRequestHeaders::kContentType,
                                        &content_type));
    std::string content_encoding;
    EXPECT_EQ(!test.expected_should_clear_upload,
              request_headers.GetHeader(kContentEncoding, &content_encoding));
    std::string content_language;
    EXPECT_EQ(!test.expected_should_clear_upload,
              request_headers.GetHeader(kContentLanguage, &content_language));
    std::string content_location;
    EXPECT_EQ(!test.expected_should_clear_upload,
              request_headers.GetHeader(kContentLocation, &content_location));
    if (!test.expected_should_clear_upload) {
      EXPECT_EQ(kContentLengthValue, content_length);
      EXPECT_EQ(kContentTypeValue, content_type);
      EXPECT_EQ(kContentEncodingValue, content_encoding);
      EXPECT_EQ(kContentLanguageValue, content_language);
      EXPECT_EQ(kContentLocationValue, content_location);
    }

    std::string custom_header;
    EXPECT_TRUE(request_headers.GetHeader(kCustomHeader, &custom_header));
    EXPECT_EQ(kCustomHeaderValue, custom_header);

    std::string origin_header_value;
    EXPECT_EQ(test.expected_origin_header != nullptr,
              request_headers.GetHeader(HttpRequestHeaders::kOrigin,
                                        &origin_header_value));
    if (test.expected_origin_header) {
      EXPECT_EQ(test.expected_origin_header, origin_header_value);
    }

    std::string modified_header1, modified_header2;
    EXPECT_TRUE(request_headers.GetHeader("Header1", &modified_header1));
    EXPECT_EQ(expected_modified_header1, modified_header1);
    EXPECT_TRUE(request_headers.GetHeader("Header2", &modified_header2));
    EXPECT_EQ(expected_modified_header2, modified_header2);
  }
}

TEST(RedirectUtilTest, RemovedHeaders) {
  struct TestCase {
    std::vector<const char*> initial_headers;
    std::vector<const char*> modified_headers;
    std::vector<const char*> removed_headers;
    std::vector<const char*> final_headers;
  };
  const TestCase kTests[] = {
      // Remove no headers (empty vector).
      {
          {},  // Initial headers
          {},  // Modified headers
          {},  // Removed headers
          {},  // Final headers
      },
      // Remove an existing header.
      {
          {"A:0"},  // Initial headers
          {},       // Modified headers
          {"A"},    // Removed headers
          {},       // Final headers
      },
      // Remove a missing header.
      {
          {},     // Initial headers
          {},     // Modified headers
          {"A"},  // Removed headers
          {},     // Final headers
      },
      // Remove two different headers.
      {
          {"A:0", "B:0"},  // Initial headers
          {},              // Modified headers
          {"A", "B"},      // Removed headers
          {},              // Final headers
      },
      // Remove two times the same headers.
      {
          {"A:0"},     // Initial headers
          {},          // Modified headers
          {"A", "A"},  // Removed headers
          {},          // Final headers
      },
      // Remove an existing header that is also modified.
      {
          {"A:0"},  // Initial headers
          {"A:1"},  // Modified headers
          {"A"},    // Removed headers
          {"A:1"},  // Final headers
      },
      // Some headers are removed, some aren't.
      {
          {"A:0", "B:0"},  // Initial headers
          {},              // Modified headers
          {"A"},           // Removed headers
          {"B:0"},         // Final headers
      },
  };

  for (const auto& test : kTests) {
    HttpRequestHeaders initial_headers, modified_headers, final_headers;
    std::vector<std::string> removed_headers;
    for (const char* header : test.initial_headers)
      initial_headers.AddHeaderFromString(header);
    for (const char* header : test.modified_headers)
      modified_headers.AddHeaderFromString(header);
    for (const char* header : test.removed_headers)
      removed_headers.push_back(header);
    for (const char* header : test.final_headers)
      final_headers.AddHeaderFromString(header);
    bool should_clear_upload(false);  // unused.

    RedirectUtil::UpdateHttpRequest(GURL(),         // original_url
                                    std::string(),  // original_method
                                    RedirectInfo(), removed_headers,
                                    modified_headers, &initial_headers,
                                    &should_clear_upload);

    // The initial_headers have been updated and should match the expected final
    // headers.
    EXPECT_EQ(initial_headers.ToString(), final_headers.ToString());
  }
}

// Test with removed_headers = absl::nullopt.
TEST(RedirectUtilTest, RemovedHeadersNullOpt) {
  HttpRequestHeaders initial_headers, final_headers;
  initial_headers.SetHeader("A", "0");
  final_headers.SetHeader("A", "0");
  absl::optional<std::vector<std::string>> removed_headers(absl::nullopt);
  absl::optional<HttpRequestHeaders> modified_headers(absl::in_place);
  bool should_clear_upload(false);  // unused.

  RedirectUtil::UpdateHttpRequest(GURL(),         // original_url
                                  std::string(),  // original_method
                                  RedirectInfo(), removed_headers,
                                  modified_headers, &initial_headers,
                                  &should_clear_upload);

  // The initial_headers have been updated and should match the expected final
  // headers.
  EXPECT_EQ(initial_headers.ToString(), final_headers.ToString());
}

// Test with modified_headers = absl::nullopt.
TEST(RedirectUtilTest, ModifyHeadersNullopt) {
  HttpRequestHeaders initial_headers, final_headers;
  initial_headers.SetHeader("A", "0");
  final_headers.SetHeader("A", "0");
  absl::optional<std::vector<std::string>> removed_headers(absl::in_place);
  absl::optional<HttpRequestHeaders> modified_headers(absl::nullopt);
  bool should_clear_upload(false);  // unused.

  RedirectUtil::UpdateHttpRequest(GURL(),         // original_url
                                  std::string(),  // original_method
                                  RedirectInfo(), removed_headers,
                                  modified_headers, &initial_headers,
                                  &should_clear_upload);

  // The initial_headers have been updated and should match the expected final
  // headers.
  EXPECT_EQ(initial_headers.ToString(), final_headers.ToString());
}

}  // namespace
}  // namespace net