OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "remoting/test/access_token_fetcher.h" | 5 #include "remoting/test/access_token_fetcher.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 namespace remoting { | 49 namespace remoting { |
50 namespace test { | 50 namespace test { |
51 | 51 |
52 // Provides base functionality for the AccessTokenFetcher Tests below. The | 52 // Provides base functionality for the AccessTokenFetcher Tests below. The |
53 // FakeURLFetcherFactory allows us to override the response data and payload for | 53 // FakeURLFetcherFactory allows us to override the response data and payload for |
54 // specified URLs. We use this to stub out network calls made by the | 54 // specified URLs. We use this to stub out network calls made by the |
55 // AccessTokenFetcher. This fixture also creates an IO MessageLoop, if | 55 // AccessTokenFetcher. This fixture also creates an IO MessageLoop, if |
56 // necessary, for use by the AccessTokenFetcher. | 56 // necessary, for use by the AccessTokenFetcher. |
57 class AccessTokenFetcherTest : public ::testing::Test { | 57 class AccessTokenFetcherTest : public ::testing::Test { |
58 public: | 58 public: |
59 AccessTokenFetcherTest() : | 59 AccessTokenFetcherTest(); |
60 url_fetcher_factory_(nullptr) {} | 60 ~AccessTokenFetcherTest() override; |
61 ~AccessTokenFetcherTest() override {} | |
62 | 61 |
63 void OnAccessTokenRetrieved( | 62 void OnAccessTokenRetrieved( |
64 base::Closure done_closure, | 63 base::Closure done_closure, |
65 const std::string& access_token, | 64 const std::string& access_token, |
66 const std::string& refresh_token) { | 65 const std::string& refresh_token); |
67 access_token_retrieved_ = access_token; | |
68 refresh_token_retrieved_ = refresh_token; | |
69 | |
70 done_closure.Run(); | |
71 } | |
72 | 66 |
73 protected: | 67 protected: |
74 // Test interface. | 68 // Test interface. |
75 void SetUp() override { | 69 void SetUp() override; |
76 if (!base::MessageLoop::current()) { | |
77 // Create a temporary message loop if the current thread does not already | |
78 // have one so we can use its task runner to create a request object. | |
79 message_loop_.reset(new base::MessageLoopForIO); | |
80 } | |
81 } | |
82 | 70 |
83 void SetFakeResponse(const GURL& url, | 71 void SetFakeResponse( |
84 const std::string& data, | 72 const GURL& url, |
85 net::HttpStatusCode code, | 73 const std::string& data, |
86 net::URLRequestStatus::Status status) { | 74 net::HttpStatusCode code, |
87 url_fetcher_factory_.SetFakeResponse(url, data, code, status); | 75 net::URLRequestStatus::Status status); |
88 } | |
89 | 76 |
90 // Used for result verification | 77 // Used for result verification |
91 std::string access_token_retrieved_; | 78 std::string access_token_retrieved_; |
92 std::string refresh_token_retrieved_; | 79 std::string refresh_token_retrieved_; |
93 | 80 |
94 private: | 81 private: |
95 net::FakeURLFetcherFactory url_fetcher_factory_; | 82 net::FakeURLFetcherFactory url_fetcher_factory_; |
96 scoped_ptr<base::MessageLoopForIO> message_loop_; | 83 scoped_ptr<base::MessageLoopForIO> message_loop_; |
97 | 84 |
98 DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcherTest); | 85 DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcherTest); |
99 }; | 86 }; |
100 | 87 |
| 88 AccessTokenFetcherTest::AccessTokenFetcherTest() : |
| 89 url_fetcher_factory_(nullptr) {} |
| 90 |
| 91 AccessTokenFetcherTest::~AccessTokenFetcherTest() {} |
| 92 |
| 93 void AccessTokenFetcherTest::OnAccessTokenRetrieved( |
| 94 base::Closure done_closure, |
| 95 const std::string& access_token, |
| 96 const std::string& refresh_token) { |
| 97 access_token_retrieved_ = access_token; |
| 98 refresh_token_retrieved_ = refresh_token; |
| 99 |
| 100 done_closure.Run(); |
| 101 } |
| 102 |
| 103 void AccessTokenFetcherTest::SetUp() { |
| 104 if (!base::MessageLoop::current()) { |
| 105 // Create a temporary message loop if the current thread does not already |
| 106 // have one so we can use its task runner to create a request object. |
| 107 message_loop_.reset(new base::MessageLoopForIO); |
| 108 } |
| 109 } |
| 110 |
| 111 void AccessTokenFetcherTest::SetFakeResponse( |
| 112 const GURL& url, |
| 113 const std::string& data, |
| 114 net::HttpStatusCode code, |
| 115 net::URLRequestStatus::Status status) { |
| 116 url_fetcher_factory_.SetFakeResponse(url, data, code, status); |
| 117 } |
| 118 |
101 TEST_F(AccessTokenFetcherTest, ExchangeAuthCodeForAccessToken) { | 119 TEST_F(AccessTokenFetcherTest, ExchangeAuthCodeForAccessToken) { |
102 SetFakeResponse( | 120 SetFakeResponse( |
103 GaiaUrls::GetInstance()->oauth2_token_url(), | 121 GaiaUrls::GetInstance()->oauth2_token_url(), |
104 kAuthCodeExchangeValidResponse, | 122 kAuthCodeExchangeValidResponse, |
105 net::HTTP_OK, | 123 net::HTTP_OK, |
106 net::URLRequestStatus::SUCCESS); | 124 net::URLRequestStatus::SUCCESS); |
107 | 125 |
108 SetFakeResponse( | 126 SetFakeResponse( |
109 GaiaUrls::GetInstance()->oauth2_token_info_url(), | 127 GaiaUrls::GetInstance()->oauth2_token_info_url(), |
110 kValidTokenInfoResponse, | 128 kValidTokenInfoResponse, |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 | 459 |
442 run_loop.Run(); | 460 run_loop.Run(); |
443 | 461 |
444 // Our callback should have been called with empty strings. | 462 // Our callback should have been called with empty strings. |
445 EXPECT_TRUE(access_token_retrieved_.empty()); | 463 EXPECT_TRUE(access_token_retrieved_.empty()); |
446 EXPECT_TRUE(refresh_token_retrieved_.empty()); | 464 EXPECT_TRUE(refresh_token_retrieved_.empty()); |
447 } | 465 } |
448 | 466 |
449 } // namespace test | 467 } // namespace test |
450 } // namespace remoting | 468 } // namespace remoting |
OLD | NEW |