(PE-18703) Pass negotiated locale in Accept-Language header

This commit passes the negotiated locale via the i18n/*locale* variable
through the Accept-Language header by default in a clj-http-client
request (only in the Clojure client, not the Java client).
This commit is contained in:
Andrew Roetker 2016-11-30 12:57:22 -08:00
parent 827dd2cfcf
commit 40d2cfb3c3
2 changed files with 33 additions and 6 deletions

View file

@ -14,11 +14,13 @@
(com.puppetlabs.http.client.impl JavaClient ResponseDeliveryDelegate)
(org.apache.http.client.utils URIBuilder)
(org.apache.http.nio.client HttpAsyncClient)
(com.codahale.metrics MetricRegistry))
(com.codahale.metrics MetricRegistry)
(java.util Locale))
(:require [puppetlabs.http.client.common :as common]
[schema.core :as schema]
[puppetlabs.http.client.metrics :as metrics]
[puppetlabs.i18n.core :refer [trs]])
[puppetlabs.i18n.core :as i18n :refer [trs]]
[clojure.string :as str])
(:refer-clojure :exclude (get)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -173,11 +175,17 @@
metric-registry :- (schema/maybe MetricRegistry)
metric-namespace :- (schema/maybe schema/Str)]
(let [result (promise)
defaults {:headers {}
:body nil
defaults {:body nil
:decompress-body true
:as :stream}
opts (merge defaults opts)
^Locale locale (i18n/user-locale)
;; lower-case the header names so that we don't end up with
;; Accept-Language *AND* accept-language in the headers
headers (into {"accept-language" (.toLanguageTag locale)}
(for [[header value] (:headers opts)]
[(str/lower-case header) value]))
opts (-> (merge defaults opts)
(assoc :headers headers))
java-request-options (clojure-options->java opts)
java-method (clojure-method->java opts)
response-delivery-delegate (get-response-delivery-delegate opts result)]

View file

@ -1,9 +1,11 @@
(ns puppetlabs.http.client.async-plaintext-test
(:import (com.puppetlabs.http.client Async RequestOptions ClientOptions)
(org.apache.http.impl.nio.client HttpAsyncClients)
(java.net URI SocketTimeoutException ServerSocket))
(java.net URI SocketTimeoutException ServerSocket)
(java.util Locale))
(:require [clojure.test :refer :all]
[puppetlabs.http.client.test-common :refer :all]
[puppetlabs.i18n.core :as i18n]
[puppetlabs.trapperkeeper.core :as tk]
[puppetlabs.trapperkeeper.testutils.bootstrap :as testutils]
[puppetlabs.trapperkeeper.testutils.logging :as testlogging]
@ -27,6 +29,11 @@
:status 200
:body "Hello, World!"})
(defn app-with-language-header-echo
[{{:strs [accept-language]} :headers}]
{:status 200
:body (str accept-language)})
(tk/defservice test-web-service
[[:WebserverService add-ring-handler]]
(init [this context]
@ -433,3 +440,15 @@
(with-open [client (async/create-client {})]
(let [response @(common/get client url {:as :text})]
(is (= 200 (:status response)))))))))))
(deftest accept-language-async
(testing "client passes on the user-locale in Accept-Language header"
(testlogging/with-test-logging
(testwebserver/with-test-webserver app-with-language-header-echo port
(i18n/with-user-locale (Locale. "es" "ES")
(let [url (str "http://localhost:" port "/hello")]
(testing "clojure persistent async client"
(with-open [client (async/create-client {})]
(let [response @(common/get client url {:as :text})]
(is (= 200 (:status response)))
(is (= "es-ES" (:body response))))))))))))