diff --git a/src/Utils/__test__/metaphysics.test.ts b/src/Utils/__test__/metaphysics.test.ts new file mode 100644 index 0000000000..d15d226b64 --- /dev/null +++ b/src/Utils/__test__/metaphysics.test.ts @@ -0,0 +1,22 @@ +jest.mock("isomorphic-fetch") +import metaphysics from "../metaphysics" + +declare const global: any +global.fetch = jest.fn(() => + Promise.resolve({ + status: 200, + json: () => Promise.resolve({ data: {} }), + }) +) + +it("Adds a user agent for reaction", () => { + expect.assertions(1) + + return metaphysics("query {}").then(() => { + expect(global.fetch).toBeCalledWith(undefined, { + body: '{"query":"query {}"}', + headers: { "Content-Type": "application/json", "User-Agent": "Reaction" }, + method: "POST", + }) + }) +}) diff --git a/src/Utils/metaphysics.ts b/src/Utils/metaphysics.ts index 4f1c73fbae..0809232d21 100644 --- a/src/Utils/metaphysics.ts +++ b/src/Utils/metaphysics.ts @@ -7,15 +7,19 @@ export function metaphysics( user?: User, checkStatus: boolean = true ): Promise { + const headers = { + "Content-Type": "application/json", + "User-Agent": "Reaction", + } return fetch(sharify.data.METAPHYSICS_ENDPOINT, { method: "POST", headers: !!user ? { - "Content-Type": "application/json", + ...headers, "X-USER-ID": user && user.id, "X-ACCESS-TOKEN": user && user.accessToken, } - : { "Content-Type": "application/json" }, + : headers, body: JSON.stringify(payload), }) .then(response => {