http.test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2004-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. "use strict";
  17. import http from "../lib/http.js";
  18. import { expect } from "chai";
  19. import { fake, assert } from "sinon";
  20. describe("http", () => {
  21. beforeEach(() => {
  22. global.fetch = fake.resolves({ ok: true });
  23. });
  24. afterEach(() => {
  25. delete global.fetch;
  26. });
  27. describe("post", () => {
  28. it("calls fetch with headers", async () => {
  29. const url = "https://example.com/some/path";
  30. const headers = { "x-custom": "some-value" };
  31. const resp = await http.post(url, headers);
  32. expect(resp.ok).to.be.true;
  33. assert.calledOnceWithExactly(global.fetch, url, {
  34. method: "POST",
  35. headers: {
  36. "Content-Type": "application/json",
  37. ...headers,
  38. },
  39. });
  40. });
  41. it("sends the body as a JSON string", async () => {
  42. const body = { foo: "bar", baz: 42 };
  43. const url = "https://example.com/some/path";
  44. const resp = await http.post(url, {}, body);
  45. expect(resp.ok).to.be.true;
  46. assert.calledOnceWithExactly(global.fetch, url, {
  47. method: "POST",
  48. headers: {
  49. "Content-Type": "application/json",
  50. },
  51. body: `{"foo":"bar","baz":42}`,
  52. });
  53. });
  54. });
  55. });