|
| 1 | +import getUserAgent from "universal-user-agent"; |
| 2 | +import fetchMock from "fetch-mock"; |
| 3 | + |
| 4 | +import { Octokit } from "../src"; |
| 5 | + |
| 6 | +const userAgent = `octokit-core.js/0.0.0-development ${getUserAgent()}`; |
| 7 | + |
| 8 | +describe("octokit.request()", () => { |
| 9 | + it("is a function", () => { |
| 10 | + const octokit = new Octokit(); |
| 11 | + expect(octokit.request).toBeInstanceOf(Function); |
| 12 | + }); |
| 13 | + |
| 14 | + it("GET /", () => { |
| 15 | + const mock = fetchMock.sandbox().getOnce( |
| 16 | + "https://api.github.com/", |
| 17 | + { ok: true }, |
| 18 | + { |
| 19 | + headers: { |
| 20 | + accept: "application/vnd.github.v3+json", |
| 21 | + "user-agent": userAgent |
| 22 | + } |
| 23 | + } |
| 24 | + ); |
| 25 | + |
| 26 | + const octokit = new Octokit({ |
| 27 | + request: { |
| 28 | + fetch: mock |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + return octokit.request("GET /").then(response => { |
| 33 | + expect(response.data).toStrictEqual({ ok: true }); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it("custom baseUrl", () => { |
| 38 | + const mock = fetchMock |
| 39 | + .sandbox() |
| 40 | + .getOnce("https://github.acme-inc.com/api/v3/orgs/octokit", { id: 123 }); |
| 41 | + |
| 42 | + const octokit = new Octokit({ |
| 43 | + baseUrl: "https://github.acme-inc.com/api/v3", |
| 44 | + request: { |
| 45 | + fetch: mock |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + return octokit.request("GET /orgs/:org", { |
| 50 | + org: "octokit" |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + it("custom user agent", () => { |
| 55 | + const mock = fetchMock.sandbox().getOnce( |
| 56 | + "https://api.github.com/", |
| 57 | + { ok: true }, |
| 58 | + { |
| 59 | + headers: { |
| 60 | + accept: "application/vnd.github.v3+json", |
| 61 | + "user-agent": `myApp/1.2.3 ${userAgent}` |
| 62 | + } |
| 63 | + } |
| 64 | + ); |
| 65 | + |
| 66 | + const octokit = new Octokit({ |
| 67 | + userAgent: "myApp/1.2.3", |
| 68 | + request: { |
| 69 | + fetch: mock |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + return octokit.request("GET /").then(response => { |
| 74 | + expect(response.data).toStrictEqual({ ok: true }); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it("previews", async () => { |
| 79 | + const mock = fetchMock |
| 80 | + .sandbox() |
| 81 | + .getOnce( |
| 82 | + "https://api.github.com/", |
| 83 | + {}, |
| 84 | + { |
| 85 | + headers: { |
| 86 | + accept: |
| 87 | + "application/vnd.github.foo-preview+json,application/vnd.github.bar-preview+json", |
| 88 | + "user-agent": userAgent |
| 89 | + } |
| 90 | + } |
| 91 | + ) |
| 92 | + .getOnce( |
| 93 | + "https://api.github.com/", |
| 94 | + {}, |
| 95 | + { |
| 96 | + headers: { |
| 97 | + accept: |
| 98 | + "application/vnd.github.foo-preview.raw,application/vnd.github.bar-preview.raw,application/vnd.github.baz-preview.raw", |
| 99 | + "user-agent": userAgent |
| 100 | + }, |
| 101 | + overwriteRoutes: false |
| 102 | + } |
| 103 | + ); |
| 104 | + |
| 105 | + const octokit = new Octokit({ |
| 106 | + previews: ["foo", "bar-preview"], |
| 107 | + request: { |
| 108 | + fetch: mock |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + await octokit.request("/"); |
| 113 | + await octokit.request("/", { |
| 114 | + mediaType: { |
| 115 | + previews: ["bar", "baz-preview"], |
| 116 | + format: "raw" |
| 117 | + } |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('octokit.request.endpoint("GET /")', () => { |
| 122 | + const octokit = new Octokit(); |
| 123 | + const requestOptions = octokit.request.endpoint("GET /"); |
| 124 | + |
| 125 | + expect(requestOptions).toStrictEqual({ |
| 126 | + method: "GET", |
| 127 | + url: "https://api.github.com/", |
| 128 | + headers: { |
| 129 | + accept: "application/vnd.github.v3+json", |
| 130 | + "user-agent": userAgent |
| 131 | + }, |
| 132 | + request: {} |
| 133 | + }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments