| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/string_split.h" | 9 #include "base/string_split.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 115 |
| 116 const std::vector<Entry>& entries() const { return entries_; } | 116 const std::vector<Entry>& entries() const { return entries_; } |
| 117 | 117 |
| 118 private: | 118 private: |
| 119 friend class base::RefCountedThreadSafe<MockCookieStore>; | 119 friend class base::RefCountedThreadSafe<MockCookieStore>; |
| 120 virtual ~MockCookieStore() {} | 120 virtual ~MockCookieStore() {} |
| 121 | 121 |
| 122 std::vector<Entry> entries_; | 122 std::vector<Entry> entries_; |
| 123 }; | 123 }; |
| 124 | 124 |
| 125 class MockCookiePolicy : public CookiePolicy, | 125 class MockCookiePolicy : public CookiePolicy { |
| 126 public base::RefCountedThreadSafe<MockCookiePolicy> { | |
| 127 public: | 126 public: |
| 128 MockCookiePolicy() : allow_all_cookies_(true), callback_(NULL) {} | 127 MockCookiePolicy() : allow_all_cookies_(true) {} |
| 128 virtual ~MockCookiePolicy() {} |
| 129 | 129 |
| 130 void set_allow_all_cookies(bool allow_all_cookies) { | 130 void set_allow_all_cookies(bool allow_all_cookies) { |
| 131 allow_all_cookies_ = allow_all_cookies; | 131 allow_all_cookies_ = allow_all_cookies; |
| 132 } | 132 } |
| 133 | 133 |
| 134 virtual int CanGetCookies(const GURL& url, | 134 virtual int CanGetCookies(const GURL& url, |
| 135 const GURL& first_party_for_cookies, | 135 const GURL& first_party_for_cookies) const { |
| 136 CompletionCallback* callback) { | 136 if (allow_all_cookies_) |
| 137 DCHECK(!callback_); | 137 return OK; |
| 138 callback_ = callback; | 138 return ERR_ACCESS_DENIED; |
| 139 MessageLoop::current()->PostTask( | |
| 140 FROM_HERE, NewRunnableMethod(this, &MockCookiePolicy::OnCanGetCookies)); | |
| 141 return ERR_IO_PENDING; | |
| 142 } | 139 } |
| 143 | 140 |
| 144 virtual int CanSetCookie(const GURL& url, | 141 virtual int CanSetCookie(const GURL& url, |
| 145 const GURL& first_party_for_cookies, | 142 const GURL& first_party_for_cookies, |
| 146 const std::string& cookie_line, | 143 const std::string& cookie_line) const { |
| 147 CompletionCallback* callback) { | 144 if (allow_all_cookies_) |
| 148 DCHECK(!callback_); | 145 return OK; |
| 149 callback_ = callback; | 146 return ERR_ACCESS_DENIED; |
| 150 MessageLoop::current()->PostTask( | |
| 151 FROM_HERE, NewRunnableMethod(this, &MockCookiePolicy::OnCanSetCookie)); | |
| 152 return ERR_IO_PENDING; | |
| 153 } | 147 } |
| 154 | 148 |
| 155 private: | 149 private: |
| 156 friend class base::RefCountedThreadSafe<MockCookiePolicy>; | |
| 157 virtual ~MockCookiePolicy() {} | |
| 158 | |
| 159 void OnCanGetCookies() { | |
| 160 CompletionCallback* callback = callback_; | |
| 161 callback_ = NULL; | |
| 162 if (allow_all_cookies_) | |
| 163 callback->Run(OK); | |
| 164 else | |
| 165 callback->Run(ERR_ACCESS_DENIED); | |
| 166 } | |
| 167 void OnCanSetCookie() { | |
| 168 CompletionCallback* callback = callback_; | |
| 169 callback_ = NULL; | |
| 170 if (allow_all_cookies_) | |
| 171 callback->Run(OK); | |
| 172 else | |
| 173 callback->Run(ERR_ACCESS_DENIED); | |
| 174 } | |
| 175 | |
| 176 bool allow_all_cookies_; | 150 bool allow_all_cookies_; |
| 177 CompletionCallback* callback_; | |
| 178 }; | 151 }; |
| 179 | 152 |
| 180 class MockURLRequestContext : public URLRequestContext { | 153 class MockURLRequestContext : public URLRequestContext { |
| 181 public: | 154 public: |
| 182 MockURLRequestContext(CookieStore* cookie_store, | 155 MockURLRequestContext(CookieStore* cookie_store, |
| 183 CookiePolicy* cookie_policy) { | 156 CookiePolicy* cookie_policy) { |
| 184 set_cookie_store(cookie_store); | 157 set_cookie_store(cookie_store); |
| 185 set_cookie_policy(cookie_policy); | 158 set_cookie_policy(cookie_policy); |
| 186 } | 159 } |
| 187 | 160 |
| 188 private: | 161 private: |
| 189 friend class base::RefCountedThreadSafe<MockURLRequestContext>; | 162 friend class base::RefCountedThreadSafe<MockURLRequestContext>; |
| 190 virtual ~MockURLRequestContext() {} | 163 virtual ~MockURLRequestContext() {} |
| 191 }; | 164 }; |
| 192 | 165 |
| 193 class WebSocketJobTest : public PlatformTest { | 166 class WebSocketJobTest : public PlatformTest { |
| 194 public: | 167 public: |
| 195 virtual void SetUp() { | 168 virtual void SetUp() { |
| 196 cookie_store_ = new MockCookieStore; | 169 cookie_store_ = new MockCookieStore; |
| 197 cookie_policy_ = new MockCookiePolicy; | 170 cookie_policy_.reset(new MockCookiePolicy); |
| 198 context_ = new MockURLRequestContext( | 171 context_ = new MockURLRequestContext( |
| 199 cookie_store_.get(), cookie_policy_.get()); | 172 cookie_store_.get(), cookie_policy_.get()); |
| 200 } | 173 } |
| 201 virtual void TearDown() { | 174 virtual void TearDown() { |
| 202 cookie_store_ = NULL; | 175 cookie_store_ = NULL; |
| 203 cookie_policy_ = NULL; | 176 cookie_policy_.reset(); |
| 204 context_ = NULL; | 177 context_ = NULL; |
| 205 websocket_ = NULL; | 178 websocket_ = NULL; |
| 206 socket_ = NULL; | 179 socket_ = NULL; |
| 207 } | 180 } |
| 208 protected: | 181 protected: |
| 209 void InitWebSocketJob(const GURL& url, MockSocketStreamDelegate* delegate) { | 182 void InitWebSocketJob(const GURL& url, MockSocketStreamDelegate* delegate) { |
| 210 websocket_ = new WebSocketJob(delegate); | 183 websocket_ = new WebSocketJob(delegate); |
| 211 socket_ = new MockSocketStream(url, websocket_.get()); | 184 socket_ = new MockSocketStream(url, websocket_.get()); |
| 212 websocket_->InitSocketStream(socket_.get()); | 185 websocket_->InitSocketStream(socket_.get()); |
| 213 websocket_->set_context(context_.get()); | 186 websocket_->set_context(context_.get()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 231 if (websocket_->socket_) { | 204 if (websocket_->socket_) { |
| 232 websocket_->socket_->DetachDelegate(); | 205 websocket_->socket_->DetachDelegate(); |
| 233 WebSocketThrottle::GetInstance()->RemoveFromQueue(websocket_); | 206 WebSocketThrottle::GetInstance()->RemoveFromQueue(websocket_); |
| 234 } | 207 } |
| 235 websocket_->state_ = WebSocketJob::CLOSED; | 208 websocket_->state_ = WebSocketJob::CLOSED; |
| 236 websocket_->delegate_ = NULL; | 209 websocket_->delegate_ = NULL; |
| 237 websocket_->socket_ = NULL; | 210 websocket_->socket_ = NULL; |
| 238 } | 211 } |
| 239 | 212 |
| 240 scoped_refptr<MockCookieStore> cookie_store_; | 213 scoped_refptr<MockCookieStore> cookie_store_; |
| 241 scoped_refptr<MockCookiePolicy> cookie_policy_; | 214 scoped_ptr<MockCookiePolicy> cookie_policy_; |
| 242 scoped_refptr<MockURLRequestContext> context_; | 215 scoped_refptr<MockURLRequestContext> context_; |
| 243 scoped_refptr<WebSocketJob> websocket_; | 216 scoped_refptr<WebSocketJob> websocket_; |
| 244 scoped_refptr<MockSocketStream> socket_; | 217 scoped_refptr<MockSocketStream> socket_; |
| 245 }; | 218 }; |
| 246 | 219 |
| 247 TEST_F(WebSocketJobTest, SimpleHandshake) { | 220 TEST_F(WebSocketJobTest, SimpleHandshake) { |
| 248 GURL url("ws://example.com/demo"); | 221 GURL url("ws://example.com/demo"); |
| 249 MockSocketStreamDelegate delegate; | 222 MockSocketStreamDelegate delegate; |
| 250 InitWebSocketJob(url, &delegate); | 223 InitWebSocketJob(url, &delegate); |
| 251 | 224 |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 EXPECT_EQ(2U, cookie_store_->entries().size()); | 488 EXPECT_EQ(2U, cookie_store_->entries().size()); |
| 516 EXPECT_EQ(cookieUrl, cookie_store_->entries()[0].url); | 489 EXPECT_EQ(cookieUrl, cookie_store_->entries()[0].url); |
| 517 EXPECT_EQ("CR-test=1", cookie_store_->entries()[0].cookie_line); | 490 EXPECT_EQ("CR-test=1", cookie_store_->entries()[0].cookie_line); |
| 518 EXPECT_EQ(cookieUrl, cookie_store_->entries()[1].url); | 491 EXPECT_EQ(cookieUrl, cookie_store_->entries()[1].url); |
| 519 EXPECT_EQ("CR-test-httponly=1", cookie_store_->entries()[1].cookie_line); | 492 EXPECT_EQ("CR-test-httponly=1", cookie_store_->entries()[1].cookie_line); |
| 520 | 493 |
| 521 CloseWebSocketJob(); | 494 CloseWebSocketJob(); |
| 522 } | 495 } |
| 523 | 496 |
| 524 } // namespace net | 497 } // namespace net |
| OLD | NEW |