Index: net/websockets/websocket_stream_test.cc |
diff --git a/net/websockets/websocket_stream_test.cc b/net/websockets/websocket_stream_test.cc |
index 03a593de0e9a4b7c5f98f4fed36b65ed793a7554..64868ded1c0457354998fbf57ba6475a40057701 100644 |
--- a/net/websockets/websocket_stream_test.cc |
+++ b/net/websockets/websocket_stream_test.cc |
@@ -124,6 +124,23 @@ class WebSocketStreamCreateTest : public ::testing::Test { |
CreateAndConnectStream(socket_url, sub_protocols, origin, timer.Pass()); |
} |
+ void CreateAndConnectCustomResponseWithCookies( |
+ const std::string& socket_url, |
+ const std::string& socket_host, |
+ const std::string& socket_path, |
+ const std::vector<std::string>& sub_protocols, |
+ const std::string& origin, |
+ const std::string& cookies, |
+ const std::string& extra_request_headers, |
+ const std::string& response_body, |
+ scoped_ptr<base::Timer> timer = scoped_ptr<base::Timer>()) { |
+ url_request_context_host_.SetExpectations( |
+ WebSocketStandardRequestWithCookies(socket_path, socket_host, origin, |
+ cookies, extra_request_headers), |
+ response_body); |
+ CreateAndConnectStream(socket_url, sub_protocols, origin, timer.Pass()); |
+ } |
+ |
// |extra_request_headers| and |extra_response_headers| must end in "\r\n" or |
// errors like "Unable to perform synchronous IO while stopped" will occur. |
void CreateAndConnectStandard( |
@@ -141,6 +158,25 @@ class WebSocketStreamCreateTest : public ::testing::Test { |
WebSocketStandardResponse(extra_response_headers), timer.Pass()); |
} |
+ // |cookies| must be empty or end with "\r\n". |
+ // |extra_request_headers| and |extra_response_headers| must end in "\r\n" or |
+ // errors like "Unable to perform synchronous IO while stopped" will occur. |
+ void CreateAndConnectStandardWithCookies( |
+ const std::string& socket_url, |
+ const std::string& socket_host, |
+ const std::string& socket_path, |
+ const std::vector<std::string>& sub_protocols, |
+ const std::string& origin, |
+ const std::string& cookies, |
+ const std::string& extra_request_headers, |
+ const std::string& extra_response_headers, |
+ scoped_ptr<base::Timer> timer = scoped_ptr<base::Timer>()) { |
+ CreateAndConnectCustomResponseWithCookies( |
+ socket_url, socket_host, socket_path, sub_protocols, origin, cookies, |
+ extra_request_headers, |
+ WebSocketStandardResponse(extra_response_headers), timer.Pass()); |
+ } |
+ |
void CreateAndConnectRawExpectations( |
const std::string& socket_url, |
const std::vector<std::string>& sub_protocols, |
@@ -192,6 +228,61 @@ class WebSocketStreamCreateTest : public ::testing::Test { |
return std::vector<std::string>(); |
} |
+ struct WaitTask { |
Adam Rice
2015/01/23 07:31:27
You don't need this class. You can do the same thi
yhirano
2015/01/23 10:16:32
Done.
|
+ WaitTask() : done_(false) {} |
+ void Run() const { |
+ if (!done_) { |
+ base::MessageLoop::current()->PostTask( |
+ FROM_HERE, base::Bind(&WaitTask::Run, base::Unretained(this))); |
+ } |
+ } |
+ |
+ // Accept any arguments and ignore them. |
+ template <typename... T> |
+ void Stop(T... args) { |
+ done_ = true; |
+ } |
+ |
+ private: |
+ bool done_; |
+ }; |
+ |
+ void SetCookie(CookieStore* store, |
+ const GURL& url, |
+ const std::string& cookie_line) { |
+ WaitTask task; |
+ |
+ task.Run(); |
+ store->SetCookieWithOptionsAsync( |
+ url, cookie_line, CookieOptions(), |
+ base::Bind(&WaitTask::Stop<bool>, base::Unretained(&task))); |
+ WebSocketStreamCreateTest::RunUntilIdle(); |
+ } |
+ |
+ std::string GetCookies(CookieStore* store, const GURL& url) { |
+ struct GetCookies { |
+ void RunTask() { task_.Run(); } |
+ void Run(const std::string& cookies) { |
+ cookies_ = cookies; |
+ task_.Stop(); |
+ } |
+ |
+ const std::string& cookies() const { return cookies_; } |
+ |
+ private: |
+ WaitTask task_; |
+ std::string cookies_; |
+ }; |
+ GetCookies callback; |
+ callback.RunTask(); |
+ |
+ store->GetCookiesWithOptionsAsync( |
+ url, CookieOptions(), |
+ base::Bind(&GetCookies::Run, base::Unretained(&callback))); |
+ WebSocketStreamCreateTest::RunUntilIdle(); |
+ return callback.cookies(); |
+ } |
+ |
const std::string& failure_message() const { return failure_message_; } |
bool has_failed() const { return has_failed_; } |
@@ -1239,6 +1330,125 @@ TEST_F(WebSocketStreamCreateTest, SelfSignedCertificateFailure) { |
EXPECT_TRUE(has_failed()); |
} |
+TEST_F(WebSocketStreamCreateTest, UseCookie) { |
Adam Rice
2015/01/23 07:31:27
You should try to only test one thing per test cas
yhirano
2015/01/23 10:16:32
Done.
|
+ CookieStore* store = |
+ url_request_context_host_.GetURLRequestContext()->cookie_store(); |
+ |
+ SetCookie(store, GURL("http://www.example.com/"), "test-http-cookie=1"); |
+ SetCookie(store, GURL("https://www.example.com/"), "test-https-cookie=1"); |
+ SetCookie(store, GURL("https://www.example.com/"), |
+ "test-https-secure-cookie=1; secure"); |
+ SetCookie(store, GURL("ws://www.example.com/"), "test-ws-cookie=1"); |
+ SetCookie(store, GURL("wss://www.example.com/"), "test-wss-cookie=1"); |
+ SetCookie(store, GURL("wss://www.example.com/"), |
+ "test-wss-secure-cookie=1; secure"); |
+ |
+ // We use "www.example.com" to verify if the cookie domain is correctly |
+ // handled. |
+ std::string cookies = |
+ "Cookie: test-http-cookie=1; test-https-cookie=1; test-ws-cookie=1; " |
+ "test-wss-cookie=1\r\n"; |
+ CreateAndConnectStandardWithCookies( |
+ "ws://www.example.com/testing_path", "www.example.com", "/testing_path", |
+ NoSubProtocols(), "http://www.example.com", cookies, "", ""); |
+ |
+ RunUntilIdle(); |
+ EXPECT_FALSE(has_failed()); |
+} |
+ |
+TEST_F(WebSocketStreamCreateTest, UseCookieSecure) { |
+ ssl_data_.push_back(new SSLSocketDataProvider(ASYNC, OK)); |
+ |
+ CookieStore* store = |
+ url_request_context_host_.GetURLRequestContext()->cookie_store(); |
+ |
+ SetCookie(store, GURL("http://www.example.com/"), "test-http-cookie=1"); |
+ SetCookie(store, GURL("https://www.example.com/"), "test-https-cookie=1"); |
+ SetCookie(store, GURL("https://www.example.com/"), |
+ "test-https-secure-cookie=1; secure"); |
+ SetCookie(store, GURL("ws://www.example.com/"), "test-ws-cookie=1"); |
+ SetCookie(store, GURL("wss://www.example.com/"), "test-wss-cookie=1"); |
+ SetCookie(store, GURL("wss://www.example.com/"), |
+ "test-wss-secure-cookie=1; secure"); |
+ |
+ // We use "www.example.com" to verify if the cookie domain is correctly |
+ // handled. |
+ std::string cookies = |
+ "Cookie: test-http-cookie=1; test-https-cookie=1; " |
+ "test-https-secure-cookie=1; test-ws-cookie=1; test-wss-cookie=1; " |
+ "test-wss-secure-cookie=1\r\n"; |
+ CreateAndConnectStandardWithCookies( |
+ "wss://www.example.com/testing_path", "www.example.com", "/testing_path", |
+ NoSubProtocols(), "http://www.example.com", cookies, "", ""); |
+ |
+ RunUntilIdle(); |
+ EXPECT_FALSE(has_failed()); |
+} |
+ |
+TEST_F(WebSocketStreamCreateTest, SetCookie) { |
+ static const char kResponse[] = |
+ "HTTP/1.1 101 Switching Protocols\r\n" |
+ "Upgrade: websocket\r\n" |
+ "Connection: Upgrade\r\n" |
+ "Set-Cookie: test-ws-cookie=1\r\n" |
+ "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" |
+ "\r\n"; |
+ |
+ CookieStore* store = |
+ url_request_context_host_.GetURLRequestContext()->cookie_store(); |
+ |
+ // We use "www.example.com" to verify if the cookie domain is correctly |
+ // handled. |
+ CreateAndConnectCustomResponse( |
+ "ws://www.example.com/testing_path", "www.example.com", "/testing_path", |
+ NoSubProtocols(), "http://www.example.com", "", kResponse); |
+ |
+ RunUntilIdle(); |
+ EXPECT_FALSE(has_failed()); |
+ |
+ EXPECT_EQ("test-ws-cookie=1", |
+ GetCookies(store, GURL("http://www.example.com/"))); |
+ EXPECT_EQ("test-ws-cookie=1", |
+ GetCookies(store, GURL("https://www.example.com/"))); |
+ EXPECT_EQ("test-ws-cookie=1", |
+ GetCookies(store, GURL("ws://www.example.com/"))); |
+ EXPECT_EQ("test-ws-cookie=1", |
+ GetCookies(store, GURL("wss://www.example.com/"))); |
+} |
+ |
+TEST_F(WebSocketStreamCreateTest, SetCookieSecure) { |
+ static const char kResponse[] = |
+ "HTTP/1.1 101 Switching Protocols\r\n" |
+ "Upgrade: websocket\r\n" |
+ "Connection: Upgrade\r\n" |
+ "Set-Cookie: test-wss-cookie=1\r\n" |
+ "Set-Cookie: test-wss-secure-cookie=1; secure\r\n" |
+ "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" |
+ "\r\n"; |
+ |
+ ssl_data_.push_back(new SSLSocketDataProvider(ASYNC, OK)); |
+ CookieStore* store = |
+ url_request_context_host_.GetURLRequestContext()->cookie_store(); |
+ |
+ // We use "www.example.com" to verify if the cookie domain is correctly |
+ // handled. |
+ CreateAndConnectCustomResponse( |
+ "wss://www.example.com/testing_path", "www.example.com", "/testing_path", |
+ NoSubProtocols(), "http://www.example.com", "", kResponse); |
+ |
+ RunUntilIdle(); |
+ EXPECT_FALSE(has_failed()); |
+ |
+ EXPECT_EQ("test-wss-cookie=1", |
+ GetCookies(store, GURL("http://www.example.com/"))); |
+ EXPECT_EQ("test-wss-cookie=1; test-wss-secure-cookie=1", |
+ GetCookies(store, GURL("https://www.example.com/"))); |
+ EXPECT_EQ("test-wss-cookie=1", |
+ GetCookies(store, GURL("ws://www.example.com/"))); |
+ EXPECT_EQ("test-wss-cookie=1; test-wss-secure-cookie=1", |
+ GetCookies(store, GURL("wss://www.example.com/"))); |
+} |
+ |
TEST_F(WebSocketStreamCreateTest, SelfSignedCertificateSuccess) { |
scoped_ptr<SSLSocketDataProvider> ssl_data( |
new SSLSocketDataProvider(ASYNC, ERR_CERT_AUTHORITY_INVALID)); |