Merge lp:~dobey/unity-scope-click/accept-language into lp:unity-scope-click/devel

Proposed by dobey
Status: Merged
Approved by: Paweł Stołowski
Approved revision: 232
Merged at revision: 235
Proposed branch: lp:~dobey/unity-scope-click/accept-language
Merge into: lp:unity-scope-click/devel
Diff against target: 186 lines (+88/-11)
6 files modified
scope/click/configuration.cpp (+16/-0)
scope/click/configuration.h (+1/-0)
scope/click/webclient.cpp (+5/-0)
scope/click/webclient.h (+1/-0)
scope/tests/test_configuration.cpp (+38/-11)
scope/tests/test_webclient.cpp (+27/-0)
To merge this branch: bzr merge lp:~dobey/unity-scope-click/accept-language
Reviewer Review Type Date Requested Status
Paweł Stołowski (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+217984@code.launchpad.net

Commit message

Create a valid string for the Accept-Language HTTP header.
Add the Accept-Language header by default to all HTTP requests.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Paweł Stołowski (stolowski) wrote :

Nice, looks good!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'scope/click/configuration.cpp'
2--- scope/click/configuration.cpp 2014-04-28 15:02:17 +0000
3+++ scope/click/configuration.cpp 2014-05-01 21:06:15 +0000
4@@ -34,6 +34,7 @@
5 #include <QProcess>
6
7 #include <boost/algorithm/string.hpp>
8+#include <boost/algorithm/string/replace.hpp>
9
10 #include "configuration.h"
11
12@@ -105,4 +106,19 @@
13 return lang_parts[0];
14 }
15
16+std::string Configuration::get_accept_languages()
17+{
18+ std::string language = get_language();
19+ std::vector<std::string> lang_parts;
20+ boost::split(lang_parts, language, boost::is_any_of("_"));
21+ std::string result;
22+ if (lang_parts.size() > 1) {
23+ boost::replace_first(language, "_", "-");
24+ result = language + ", " + get_language_base();
25+ } else {
26+ result = language;
27+ }
28+ return result;
29+}
30+
31 } // namespace click
32
33=== modified file 'scope/click/configuration.h'
34--- scope/click/configuration.h 2014-04-25 17:17:22 +0000
35+++ scope/click/configuration.h 2014-05-01 21:06:15 +0000
36@@ -49,6 +49,7 @@
37
38 virtual std::string get_language_base();
39 virtual std::string get_language();
40+ virtual std::string get_accept_languages();
41
42 virtual ~Configuration() {}
43 protected:
44
45=== modified file 'scope/click/webclient.cpp'
46--- scope/click/webclient.cpp 2014-04-29 03:38:05 +0000
47+++ scope/click/webclient.cpp 2014-05-01 21:06:15 +0000
48@@ -30,6 +30,7 @@
49 #include <QBuffer>
50 #include <QDebug>
51
52+#include "configuration.h"
53 #include "webclient.h"
54 #include "smartconnect.h"
55
56@@ -93,6 +94,10 @@
57 QSharedPointer<QBuffer> buffer(new QBuffer());
58 buffer->setData(data.c_str(), data.length());
59
60+ // Set the Accept-Language header for all requests.
61+ request->setRawHeader(ACCEPT_LANGUAGE_HEADER.c_str(),
62+ Configuration().get_accept_languages().c_str());
63+
64 for (const auto& kv : headers) {
65 QByteArray header_name(kv.first.c_str(), kv.first.length());
66 QByteArray header_value(kv.second.c_str(), kv.second.length());
67
68=== modified file 'scope/click/webclient.h'
69--- scope/click/webclient.h 2014-04-29 03:17:38 +0000
70+++ scope/click/webclient.h 2014-05-01 21:06:15 +0000
71@@ -49,6 +49,7 @@
72 namespace web
73 {
74
75+const std::string ACCEPT_LANGUAGE_HEADER ="Accept-Language";
76 const std::string AUTHORIZATION_HEADER = "Authorization";
77 const std::string CONTENT_TYPE_HEADER = "Content-Type";
78
79
80=== modified file 'scope/tests/test_configuration.cpp'
81--- scope/tests/test_configuration.cpp 2014-04-28 15:02:17 +0000
82+++ scope/tests/test_configuration.cpp 2014-05-01 21:06:15 +0000
83@@ -132,16 +132,43 @@
84 ASSERT_EQ(Configuration().get_language_base(), "C");
85 }
86
87-TEST(Configuration, getLanguageBasNoCharseteCorrect)
88-{
89- ASSERT_EQ(setenv(Configuration::LANGUAGE_ENVVAR, "en_US", 1), 0);
90- EXPECT_EQ(Configuration().get_language_base(), "en");
91- ASSERT_EQ(unsetenv(Configuration::LANGUAGE_ENVVAR), 0);
92-}
93-
94-TEST(Configuration, getLanguageBasNoRegionOrCharseteCorrect)
95-{
96- ASSERT_EQ(setenv(Configuration::LANGUAGE_ENVVAR, "en", 1), 0);
97- EXPECT_EQ(Configuration().get_language_base(), "en");
98+TEST(Configuration, getLanguageBaseNoCharseteCorrect)
99+{
100+ ASSERT_EQ(setenv(Configuration::LANGUAGE_ENVVAR, "en_US", 1), 0);
101+ EXPECT_EQ(Configuration().get_language_base(), "en");
102+ ASSERT_EQ(unsetenv(Configuration::LANGUAGE_ENVVAR), 0);
103+}
104+
105+TEST(Configuration, getLanguageBaseNoRegionOrCharseteCorrect)
106+{
107+ ASSERT_EQ(setenv(Configuration::LANGUAGE_ENVVAR, "en", 1), 0);
108+ EXPECT_EQ(Configuration().get_language_base(), "en");
109+ ASSERT_EQ(unsetenv(Configuration::LANGUAGE_ENVVAR), 0);
110+}
111+
112+TEST(Configuration, getAcceptLanguagesCorrect)
113+{
114+ ASSERT_EQ(setenv(Configuration::LANGUAGE_ENVVAR, "en_US.UTF-8", 1), 0);
115+ EXPECT_EQ(Configuration().get_accept_languages(), "en-US, en");
116+ ASSERT_EQ(unsetenv(Configuration::LANGUAGE_ENVVAR), 0);
117+}
118+
119+TEST(Configuration, getAcceptLanguagesUnsetFallback)
120+{
121+ ASSERT_EQ(unsetenv(Configuration::LANGUAGE_ENVVAR), 0);
122+ ASSERT_EQ(Configuration().get_accept_languages(), "C");
123+}
124+
125+TEST(Configuration, getAcceptLanguagesNoCharseteCorrect)
126+{
127+ ASSERT_EQ(setenv(Configuration::LANGUAGE_ENVVAR, "en_US", 1), 0);
128+ EXPECT_EQ(Configuration().get_accept_languages(), "en-US, en");
129+ ASSERT_EQ(unsetenv(Configuration::LANGUAGE_ENVVAR), 0);
130+}
131+
132+TEST(Configuration, getAcceptLanguagesNoRegionOrCharseteCorrect)
133+{
134+ ASSERT_EQ(setenv(Configuration::LANGUAGE_ENVVAR, "en", 1), 0);
135+ EXPECT_EQ(Configuration().get_accept_languages(), "en");
136 ASSERT_EQ(unsetenv(Configuration::LANGUAGE_ENVVAR), 0);
137 }
138
139=== modified file 'scope/tests/test_webclient.cpp'
140--- scope/tests/test_webclient.cpp 2014-04-29 03:38:05 +0000
141+++ scope/tests/test_webclient.cpp 2014-05-01 21:06:15 +0000
142@@ -28,6 +28,7 @@
143 */
144 #include <QDebug>
145
146+#include "click/configuration.h"
147 #include "click/webclient.h"
148
149 #include "mock_network_access_manager.h"
150@@ -50,6 +51,12 @@
151 .startsWith("OAuth ");
152 }
153
154+MATCHER_P(IsCorrectAcceptLanguageHeader, refAcceptLanguages, "")
155+{
156+ return arg.hasRawHeader(click::web::ACCEPT_LANGUAGE_HEADER.c_str()) &&
157+ arg.rawHeader(click::web::ACCEPT_LANGUAGE_HEADER.c_str()) == refAcceptLanguages;
158+}
159+
160 MATCHER_P(IsCorrectCookieHeader, refCookie, "")
161 {
162 return arg.hasRawHeader("Cookie") && arg.rawHeader("Cookie") == refCookie;
163@@ -333,3 +340,23 @@
164 EXPECT_CALL(*reply, abort()).Times(1);
165 response->abort();
166 }
167+
168+TEST_F(WebClientTest, testAcceptLanguageSetCorrectly)
169+{
170+ using namespace ::testing;
171+
172+ auto reply = new NiceMock<MockNetworkReply>();
173+ ON_CALL(*reply, readAll()).WillByDefault(Return("HOLA"));
174+ QSharedPointer<click::network::Reply> replyPtr(reply);
175+
176+ click::web::Client wc(namPtr);
177+
178+ ASSERT_EQ(setenv(click::Configuration::LANGUAGE_ENVVAR,
179+ "en_US.UTF-8", 1), 0);
180+ EXPECT_CALL(nam, sendCustomRequest(IsCorrectAcceptLanguageHeader("en-US, en"), _, _))
181+ .Times(1)
182+ .WillOnce(Return(replyPtr));
183+
184+ auto wr = wc.call(FAKE_SERVER + FAKE_PATH);
185+ ASSERT_EQ(unsetenv(click::Configuration::LANGUAGE_ENVVAR), 0);
186+}

Subscribers

People subscribed via source and target branches

to all changes: