#include "base/strings/stringprintf.h"
#include "components/data_reduction_proxy/browser/data_reduction_proxy_params_test_utils.h"
#include "components/data_reduction_proxy/common/data_reduction_proxy_headers.h"
+#include "components/data_reduction_proxy/common/data_reduction_proxy_headers_test_utils.h"
#include "net/base/completion_callback.h"
#include "net/base/host_port_pair.h"
#include "net/base/load_flags.h"
using net::TestURLRequestContext;
-namespace {
-// Transform "normal"-looking headers (\n-separated) to the appropriate
-// input format for ParseRawHeaders (\0-separated).
-void HeadersToRaw(std::string* headers) {
- std::replace(headers->begin(), headers->end(), '\n', '\0');
- if (!headers->empty())
- *headers += '\0';
-}
-
-} // namespace
-
-
namespace data_reduction_proxy {
// A test network delegate that exercises the bypass logic of the data
DataReductionProxyParams::kFallbackAllowed |
DataReductionProxyParams::kPromoAllowed,
TestDataReductionProxyParams::HAS_EVERYTHING &
- ~TestDataReductionProxyParams::HAS_DEV_ORIGIN));
+ ~TestDataReductionProxyParams::HAS_DEV_ORIGIN &
+ ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN));
}
// Sets up the |TestURLRequestContext| with the provided |ProxyService| and
"Host: www.google.com\r\n"
"Proxy-Connection: keep-alive\r\n%s"
"User-Agent:\r\n"
- "Accept-Encoding: gzip,deflate\r\n\r\n",
+ "Accept-Encoding: gzip, deflate\r\n\r\n",
method, trailer.c_str());
MockWrite data_writes[] = {
MockWrite(request1.c_str()),
"Host: www.google.com\r\n"
"Connection: keep-alive\r\n%s"
"User-Agent:\r\n"
- "Accept-Encoding: gzip,deflate\r\n\r\n",
+ "Accept-Encoding: gzip, deflate\r\n\r\n",
method, trailer.c_str());
MockWrite data_writes2[] = {
MockWrite(request2.c_str()),
const std::string& value,
bool expected_retry) {
TestDelegate d;
- URLRequest r(GURL("http://www.google.com/"),
- net::DEFAULT_PRIORITY,
- &d,
- context_.get());
- r.set_method(method);
- r.SetLoadFlags(net::LOAD_NORMAL);
-
- r.Start();
+ scoped_ptr<URLRequest> r(context_->CreateRequest(
+ GURL("http://www.google.com/"),
+ net::DEFAULT_PRIORITY,
+ &d,
+ NULL));
+ r->set_method(method);
+ r->SetLoadFlags(net::LOAD_NORMAL);
+
+ r->Start();
base::RunLoop().Run();
- EXPECT_EQ(net::URLRequestStatus::SUCCESS, r.status().status());
- EXPECT_EQ(net::OK, r.status().error());
+ EXPECT_EQ(net::URLRequestStatus::SUCCESS, r->status().status());
+ EXPECT_EQ(net::OK, r->status().error());
if (expected_retry)
- EXPECT_EQ(2U, r.url_chain().size());
+ EXPECT_EQ(2U, r->url_chain().size());
else
- EXPECT_EQ(1U, r.url_chain().size());
+ EXPECT_EQ(1U, r->url_chain().size());
if (!header.empty()) {
// We also have a server header here that isn't set by the proxy.
- EXPECT_TRUE(r.response_headers()->HasHeaderValue(header, value));
+ EXPECT_TRUE(r->response_headers()->HasHeaderValue(header, value));
}
EXPECT_EQ(content, d.data_received());
if (duration_seconds == 0) {
expected_min_duration = base::TimeDelta::FromMinutes(1);
expected_max_duration = base::TimeDelta::FromMinutes(5);
- }
- else {
+ } else {
expected_min_duration = base::TimeDelta::FromSeconds(duration_seconds);
expected_max_duration = base::TimeDelta::FromSeconds(duration_seconds);
}
{ "CONNECT", false },
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
- net::TestURLRequest request(GURL("http://www.google.com/"),
- net::DEFAULT_PRIORITY,
- NULL,
- &context);
- request.set_method(tests[i].method);
- EXPECT_EQ(tests[i].expected_result, IsRequestIdempotent(&request));
+ scoped_ptr<net::URLRequest> request(
+ context.CreateRequest(GURL("http://www.google.com/"),
+ net::DEFAULT_PRIORITY,
+ NULL,
+ NULL));
+ request->set_method(tests[i].method);
+ EXPECT_EQ(tests[i].expected_result, IsRequestIdempotent(request.get()));
}
}
new HttpResponseHeaders(headers));
scoped_refptr<HttpResponseHeaders> override_response_headers;
TestDelegate test_delegate;
- net::TestURLRequest request(GURL("http://www.google.com/"),
- net::DEFAULT_PRIORITY,
- NULL,
- &context);
- OverrideResponseAsRedirect(&request,
- original_response_headers,
+ scoped_ptr<net::URLRequest> request(
+ context.CreateRequest(GURL("http://www.google.com/"),
+ net::DEFAULT_PRIORITY,
+ NULL,
+ NULL));
+ OverrideResponseAsRedirect(request.get(), original_response_headers.get(),
&override_response_headers);
int expected_flags = net::LOAD_DISABLE_CACHE | net::LOAD_BYPASS_PROXY;
- EXPECT_EQ(expected_flags, request.load_flags());
+ EXPECT_EQ(expected_flags, request->load_flags());
+ std::string override_headers;
+ override_response_headers->GetNormalizedHeaders(&override_headers);
+ EXPECT_EQ(std::string(tests[i].expected_headers), override_headers);
+ }
+}
+
+// Tests that the response is correctly overwritten as a redirect with CORS
+// headers when an Origin header is provided in the initial request.
+TEST_F(DataReductionProxyProtocolTest, OverrideResponseAsRedirectCORS) {
+ net::TestURLRequestContext context;
+ const struct {
+ const char* headers;
+ const char* expected_headers;
+ } tests[] = {
+ { "HTTP/1.1 200 0K\n"
+ "Chrome-Proxy: block=1\n"
+ "Via: 1.1 Chrome-Compression-Proxy\n",
+
+ "HTTP/1.1 302 Found\n"
+ "Chrome-Proxy: block=1\n"
+ "Via: 1.1 Chrome-Compression-Proxy\n"
+ "Location: http://www.google.com/\n"
+ "Access-Control-Allow-Origin: http://www.else.com\n"
+ "Access-Control-Allow-Credentials: true\n"
+ },
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
+ std::string headers(tests[i].headers);
+ HeadersToRaw(&headers);
+ scoped_refptr<HttpResponseHeaders> original_response_headers(
+ new HttpResponseHeaders(headers));
+ scoped_refptr<HttpResponseHeaders> override_response_headers;
+ TestDelegate test_delegate;
+ scoped_ptr<net::URLRequest> request(
+ context.CreateRequest(GURL("http://www.google.com/"),
+ net::DEFAULT_PRIORITY,
+ NULL,
+ NULL));
+ request->SetExtraRequestHeaderByName("Origin", "http://www.else.com", true);
+ OverrideResponseAsRedirect(request.get(), original_response_headers.get(),
+ &override_response_headers);
+ int expected_flags = net::LOAD_DISABLE_CACHE | net::LOAD_BYPASS_PROXY;
+ EXPECT_EQ(expected_flags, request->load_flags());
std::string override_headers;
override_response_headers->GetNormalizedHeaders(&override_headers);
EXPECT_EQ(std::string(tests[i].expected_headers), override_headers);
"Host: www.google.com\r\n"
"Connection: keep-alive\r\n"
"User-Agent:\r\n"
- "Accept-Encoding: gzip,deflate\r\n\r\n"),
+ "Accept-Encoding: gzip, deflate\r\n\r\n"),
};
StaticSocketDataProvider data1(data_reads, arraysize(data_reads),
data_writes, arraysize(data_writes));
mock_socket_factory_.AddSocketDataProvider(&data1);
TestDelegate d;
- URLRequest r(GURL("http://www.google.com/"),
- net::DEFAULT_PRIORITY,
- &d,
- context_.get());
- r.set_method("GET");
- r.SetLoadFlags(net::LOAD_NORMAL);
+ scoped_ptr<URLRequest> r(context_->CreateRequest(
+ GURL("http://www.google.com/"),
+ net::DEFAULT_PRIORITY,
+ &d,
+ NULL));
+ r->set_method("GET");
+ r->SetLoadFlags(net::LOAD_NORMAL);
- r.Start();
- base::RunLoop().Run();
+ r->Start();
+ base::RunLoop().Run();
- EXPECT_EQ(net::URLRequestStatus::SUCCESS, r.status().status());
- EXPECT_EQ(net::OK, r.status().error());
+ EXPECT_EQ(net::URLRequestStatus::SUCCESS, r->status().status());
+ EXPECT_EQ(net::OK, r->status().error());
EXPECT_EQ("Bypass message", d.data_received());
DataReductionProxyParams::kFallbackAllowed |
DataReductionProxyParams::kPromoAllowed,
TestDataReductionProxyParams::HAS_EVERYTHING &
- ~TestDataReductionProxyParams::HAS_DEV_ORIGIN);
+ ~TestDataReductionProxyParams::HAS_DEV_ORIGIN &
+ ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN);
// Data reduction proxy info
net::ProxyInfo data_reduction_proxy_info;
// With Finch trial set, should only bypass if LOAD flag is set and the
// effective proxy is the data compression proxy.
base::FieldTrialList field_trial_list(new BadEntropyProvider());
- base::FieldTrialList::CreateFieldTrial("DataCompressionProxyRollout",
- "Enabled");
base::FieldTrialList::CreateFieldTrial("DataCompressionProxyCriticalBypass",
"Enabled");
EXPECT_TRUE(