OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 8 #include "base/bind.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 const std::string& language, | 38 const std::string& language, |
39 int status, | 39 int status, |
40 const std::string& response) | 40 const std::string& response) |
41 : net::TestURLFetcher(0, url, d), | 41 : net::TestURLFetcher(0, url, d), |
42 version_(base::StringPrintf("v%d", version)), | 42 version_(base::StringPrintf("v%d", version)), |
43 language_(language.empty() ? std::string("en") : language), | 43 language_(language.empty() ? std::string("en") : language), |
44 text_(text) { | 44 text_(text) { |
45 set_response_code(status); | 45 set_response_code(status); |
46 SetResponseString(response); | 46 SetResponseString(response); |
47 } | 47 } |
48 virtual ~TestSpellingURLFetcher() { | 48 ~TestSpellingURLFetcher() override {} |
49 } | |
50 | 49 |
51 virtual void SetUploadData(const std::string& upload_content_type, | 50 void SetUploadData(const std::string& upload_content_type, |
52 const std::string& upload_content) override { | 51 const std::string& upload_content) override { |
53 // Verify the given content type is JSON. (The Spelling service returns an | 52 // Verify the given content type is JSON. (The Spelling service returns an |
54 // internal server error when this content type is not JSON.) | 53 // internal server error when this content type is not JSON.) |
55 EXPECT_EQ("application/json", upload_content_type); | 54 EXPECT_EQ("application/json", upload_content_type); |
56 | 55 |
57 // Parse the JSON to be sent to the service, and verify its parameters. | 56 // Parse the JSON to be sent to the service, and verify its parameters. |
58 scoped_ptr<base::DictionaryValue> value(static_cast<base::DictionaryValue*>( | 57 scoped_ptr<base::DictionaryValue> value(static_cast<base::DictionaryValue*>( |
59 base::JSONReader::Read(upload_content, | 58 base::JSONReader::Read(upload_content, |
60 base::JSON_ALLOW_TRAILING_COMMAS))); | 59 base::JSON_ALLOW_TRAILING_COMMAS))); |
61 ASSERT_TRUE(!!value.get()); | 60 ASSERT_TRUE(!!value.get()); |
62 std::string method; | 61 std::string method; |
63 EXPECT_TRUE(value->GetString("method", &method)); | 62 EXPECT_TRUE(value->GetString("method", &method)); |
64 EXPECT_EQ("spelling.check", method); | 63 EXPECT_EQ("spelling.check", method); |
65 std::string version; | 64 std::string version; |
66 EXPECT_TRUE(value->GetString("apiVersion", &version)); | 65 EXPECT_TRUE(value->GetString("apiVersion", &version)); |
67 EXPECT_EQ(version_, version); | 66 EXPECT_EQ(version_, version); |
68 std::string text; | 67 std::string text; |
69 EXPECT_TRUE(value->GetString("params.text", &text)); | 68 EXPECT_TRUE(value->GetString("params.text", &text)); |
70 EXPECT_EQ(text_, text); | 69 EXPECT_EQ(text_, text); |
71 std::string language; | 70 std::string language; |
72 EXPECT_TRUE(value->GetString("params.language", &language)); | 71 EXPECT_TRUE(value->GetString("params.language", &language)); |
73 EXPECT_EQ(language_, language); | 72 EXPECT_EQ(language_, language); |
74 ASSERT_TRUE(GetExpectedCountry(language, &country_)); | 73 ASSERT_TRUE(GetExpectedCountry(language, &country_)); |
75 std::string country; | 74 std::string country; |
76 EXPECT_TRUE(value->GetString("params.originCountry", &country)); | 75 EXPECT_TRUE(value->GetString("params.originCountry", &country)); |
77 EXPECT_EQ(country_, country); | 76 EXPECT_EQ(country_, country); |
78 | 77 |
79 net::TestURLFetcher::SetUploadData(upload_content_type, upload_content); | 78 net::TestURLFetcher::SetUploadData(upload_content_type, upload_content); |
80 } | 79 } |
81 | 80 |
82 virtual void Start() override { | 81 void Start() override { |
83 // Verify that this client does not either send cookies to the Spelling | 82 // Verify that this client does not either send cookies to the Spelling |
84 // service or accept cookies from it. | 83 // service or accept cookies from it. |
85 EXPECT_EQ(net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES, | 84 EXPECT_EQ(net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES, |
86 GetLoadFlags()); | 85 GetLoadFlags()); |
87 } | 86 } |
88 | 87 |
89 private: | 88 private: |
90 bool GetExpectedCountry(const std::string& language, std::string* country) { | 89 bool GetExpectedCountry(const std::string& language, std::string* country) { |
91 static const struct { | 90 static const struct { |
92 const char* language; | 91 const char* language; |
(...skipping 22 matching lines...) Expand all Loading... |
115 // this test can use TestSpellingURLFetcher. This class also lets tests access | 114 // this test can use TestSpellingURLFetcher. This class also lets tests access |
116 // the ParseResponse method. | 115 // the ParseResponse method. |
117 class TestingSpellingServiceClient : public SpellingServiceClient { | 116 class TestingSpellingServiceClient : public SpellingServiceClient { |
118 public: | 117 public: |
119 TestingSpellingServiceClient() | 118 TestingSpellingServiceClient() |
120 : request_type_(0), | 119 : request_type_(0), |
121 response_status_(0), | 120 response_status_(0), |
122 success_(false), | 121 success_(false), |
123 fetcher_(NULL) { | 122 fetcher_(NULL) { |
124 } | 123 } |
125 virtual ~TestingSpellingServiceClient() { | 124 ~TestingSpellingServiceClient() override {} |
126 } | |
127 | 125 |
128 void SetHTTPRequest(int type, | 126 void SetHTTPRequest(int type, |
129 const std::string& text, | 127 const std::string& text, |
130 const std::string& language) { | 128 const std::string& language) { |
131 request_type_ = type; | 129 request_type_ = type; |
132 request_text_ = text; | 130 request_text_ = text; |
133 request_language_ = language; | 131 request_language_ = language; |
134 } | 132 } |
135 | 133 |
136 void SetHTTPResponse(int status, const char* data) { | 134 void SetHTTPResponse(int status, const char* data) { |
(...skipping 24 matching lines...) Expand all Loading... |
161 } | 159 } |
162 EXPECT_EQ(corrected_text_, text); | 160 EXPECT_EQ(corrected_text_, text); |
163 } | 161 } |
164 | 162 |
165 bool ParseResponseSuccess(const std::string& data) { | 163 bool ParseResponseSuccess(const std::string& data) { |
166 std::vector<SpellCheckResult> results; | 164 std::vector<SpellCheckResult> results; |
167 return ParseResponse(data, &results); | 165 return ParseResponse(data, &results); |
168 } | 166 } |
169 | 167 |
170 private: | 168 private: |
171 virtual net::URLFetcher* CreateURLFetcher(const GURL& url) override { | 169 net::URLFetcher* CreateURLFetcher(const GURL& url) override { |
172 EXPECT_EQ("https://www.googleapis.com/rpc", url.spec()); | 170 EXPECT_EQ("https://www.googleapis.com/rpc", url.spec()); |
173 fetcher_ = new TestSpellingURLFetcher(0, url, this, | 171 fetcher_ = new TestSpellingURLFetcher(0, url, this, |
174 request_type_, request_text_, | 172 request_type_, request_text_, |
175 request_language_, | 173 request_language_, |
176 response_status_, response_data_); | 174 response_status_, response_data_); |
177 return fetcher_; | 175 return fetcher_; |
178 } | 176 } |
179 | 177 |
180 int request_type_; | 178 int request_type_; |
181 std::string request_text_; | 179 std::string request_text_; |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 } | 375 } |
378 #endif // !defined(OS_MACOSX) | 376 #endif // !defined(OS_MACOSX) |
379 } | 377 } |
380 | 378 |
381 // Verify that an error in JSON response from spelling service will result in | 379 // Verify that an error in JSON response from spelling service will result in |
382 // ParseResponse returning false. | 380 // ParseResponse returning false. |
383 TEST_F(SpellingServiceClientTest, ResponseErrorTest) { | 381 TEST_F(SpellingServiceClientTest, ResponseErrorTest) { |
384 EXPECT_TRUE(client_.ParseResponseSuccess("{\"result\": {}}")); | 382 EXPECT_TRUE(client_.ParseResponseSuccess("{\"result\": {}}")); |
385 EXPECT_FALSE(client_.ParseResponseSuccess("{\"error\": {}}")); | 383 EXPECT_FALSE(client_.ParseResponseSuccess("{\"error\": {}}")); |
386 } | 384 } |
OLD | NEW |