mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-08 19:04:45 +00:00
Bug 1246938 - Allow extra headers to be passed via hawk requests. r=markh
--HG-- extra : commitid : 3QxWpv2vbXh
This commit is contained in:
parent
4f796c5adc
commit
cd2ee45865
@ -190,13 +190,16 @@ this.HawkClient.prototype = {
|
||||
* @param payloadObj
|
||||
* An object that can be encodable as JSON as the payload of the
|
||||
* request
|
||||
* @param extraHeaders
|
||||
* An object with header/value pairs to send with the request.
|
||||
* @return Promise
|
||||
* Returns a promise that resolves to the response of the API call,
|
||||
* or is rejected with an error. If the server response can be parsed
|
||||
* as JSON and contains an 'error' property, the promise will be
|
||||
* rejected with this JSON-parsed response.
|
||||
*/
|
||||
request: function(path, method, credentials=null, payloadObj={}, retryOK=true) {
|
||||
request: function(path, method, credentials=null, payloadObj={}, extraHeaders = {},
|
||||
retryOK=true) {
|
||||
method = method.toLowerCase();
|
||||
|
||||
let deferred = Promise.defer();
|
||||
@ -237,7 +240,7 @@ this.HawkClient.prototype = {
|
||||
// Clock offset is adjusted already in the top of this function.
|
||||
log.debug("Received 401 for " + path + ": retrying");
|
||||
return deferred.resolve(
|
||||
self.request(path, method, credentials, payloadObj, false));
|
||||
self.request(path, method, credentials, payloadObj, extraHeaders, false));
|
||||
}
|
||||
|
||||
// If the server returned a json error message, use it in the rejection
|
||||
@ -278,6 +281,7 @@ this.HawkClient.prototype = {
|
||||
let extra = {
|
||||
now: this.now(),
|
||||
localtimeOffsetMsec: this.localtimeOffsetMsec,
|
||||
headers: extraHeaders
|
||||
};
|
||||
|
||||
let request = this.newHAWKAuthenticatedRESTRequest(uri, credentials, extra);
|
||||
|
@ -42,7 +42,9 @@ const Prefs = new Preferences("services.common.rest.");
|
||||
* Valid properties are:
|
||||
*
|
||||
* now: <current time in milliseconds>,
|
||||
* localtimeOffsetMsec: <local clock offset vs server>
|
||||
* localtimeOffsetMsec: <local clock offset vs server>,
|
||||
* headers: <An object with header/value pairs to be sent
|
||||
* as headers on the request>
|
||||
*
|
||||
* extra.localtimeOffsetMsec is the value in milliseconds that must be added to
|
||||
* the local clock to make it agree with the server's clock. For instance, if
|
||||
@ -58,6 +60,7 @@ this.HAWKAuthenticatedRESTRequest =
|
||||
this.now = extra.now || Date.now();
|
||||
this.localtimeOffsetMsec = extra.localtimeOffsetMsec || 0;
|
||||
this._log.trace("local time, offset: " + this.now + ", " + (this.localtimeOffsetMsec));
|
||||
this.extraHeaders = extra.headers || {};
|
||||
|
||||
// Expose for testing
|
||||
this._intl = getIntl();
|
||||
@ -83,6 +86,10 @@ HAWKAuthenticatedRESTRequest.prototype = {
|
||||
this._log.trace("hawk auth header: " + header.field);
|
||||
}
|
||||
|
||||
for (let header in this.extraHeaders) {
|
||||
this.setHeader(header, this.extraHeaders[header]);
|
||||
}
|
||||
|
||||
this.setHeader("Content-Type", contentType);
|
||||
|
||||
this.setHeader("Accept-Language", this._intl.accept_languages);
|
||||
|
@ -98,6 +98,29 @@ add_task(function test_authenticated_patch_request() {
|
||||
check_authenticated_request("PATCH");
|
||||
});
|
||||
|
||||
add_task(function* test_extra_headers() {
|
||||
let server = httpd_setup({"/foo": (request, response) => {
|
||||
do_check_true(request.hasHeader("Authorization"));
|
||||
do_check_true(request.hasHeader("myHeader"));
|
||||
do_check_eq(request.getHeader("myHeader"), "fake");
|
||||
|
||||
response.setStatusLine(request.httpVersion, 200, "OK");
|
||||
response.setHeader("Content-Type", "application/json");
|
||||
response.bodyOutputStream.writeFrom(request.bodyInputStream, request.bodyInputStream.available());
|
||||
}
|
||||
});
|
||||
|
||||
let client = new HawkClient(server.baseURI);
|
||||
|
||||
let response = yield client.request("/foo", "POST", TEST_CREDS, {foo: "bar"},
|
||||
{"myHeader": "fake"});
|
||||
let result = JSON.parse(response.body);
|
||||
|
||||
do_check_eq("bar", result.foo);
|
||||
|
||||
yield deferredStop(server);
|
||||
});
|
||||
|
||||
add_task(function* test_credentials_optional() {
|
||||
let method = "GET";
|
||||
let server = httpd_setup({
|
||||
|
Loading…
Reference in New Issue
Block a user