Skip to content

Commit

Permalink
Attach default request headers to HTTP class.
Browse files Browse the repository at this point in the history
  • Loading branch information
n1k0 committed Jul 18, 2015
1 parent 6abe27a commit 4415425
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
15 changes: 8 additions & 7 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import { getUnixTime } from "./utils";
import ERROR_CODES from "./errors.js";

// TODO: attach as static property of the HTTP class
export const DEFAULT_REQUEST_HEADERS = {
"Accept": "application/json",
"Content-Type": "application/json",
};

export default class HTTP {
static get DEFAULT_REQUEST_HEADERS() {
return {
"Accept": "application/json",
"Content-Type": "application/json",
};
}

constructor(options={}) {
this._backoffRelease = options.backoffRelease || null;
}
Expand Down Expand Up @@ -38,7 +39,7 @@ export default class HTTP {
}
var response, status, statusText, headers;
// Ensure default request headers are always set
options.headers = Object.assign({}, DEFAULT_REQUEST_HEADERS, options.headers);
options.headers = Object.assign({}, HTTP.DEFAULT_REQUEST_HEADERS, options.headers);
return fetch(url, options)
.then(res => {
response = res;
Expand Down
2 changes: 0 additions & 2 deletions test/api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import sinon from "sinon";
import { quote } from "../src/utils";
import { fakeServerResponse } from "./test_utils.js";
import Api, { SUPPORTED_PROTOCOL_VERSION as SPV, cleanRecord } from "../src/api";
import { DEFAULT_REQUEST_HEADERS as DRH } from "../src/http.js";


chai.use(chaiAsPromised);
chai.should();
Expand Down
4 changes: 2 additions & 2 deletions test/http_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import chai, { expect } from "chai";
import chaiAsPromised from "chai-as-promised";
import sinon from "sinon";
import { DEFAULT_REQUEST_HEADERS as DRH } from "../src/http.js";
import { fakeServerResponse } from "./test_utils.js";
import HTTP from "../src/http.js";

Expand Down Expand Up @@ -37,7 +36,8 @@ describe("HTTP class", () => {
it("should set default headers", () => {
new HTTP().request("/");

expect(fetch.firstCall.args[1].headers).eql(DRH);
expect(fetch.firstCall.args[1].headers)
.eql(HTTP.DEFAULT_REQUEST_HEADERS);
});

it("should merge custom headers with default ones", () => {
Expand Down

0 comments on commit 4415425

Please sign in to comment.