Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

github.test.ts

49 lines1.6 KB

1import { describe, expect, it } from 'vitest'
2​
3import { githubRequestInit } from './github.ts'
4​
5const API =
6 'https://api.github.com/repos/cloudflare/cloudflare-docs/contents/src/content/catalog-models?ref=production'
7const RAW =
8 'https://raw.githubusercontent.com/byteplus-sdk/byteplus-go-sdk-v2/main/service/arkruntime/model/chat_completion.go'
9​
10function authorization(init: RequestInit): string | null {
11 return new Headers(init.headers).get('Authorization')
13​
14describe('githubRequestInit', () => {
15 it('sends the bearer token only to api.github.com', () => {
16 const authed = githubRequestInit(API, { GITHUB_TOKEN: 'ghp_test' })
17 expect(authorization(authed)).toBe('Bearer ghp_test')
18 expect(new Headers(authed.headers).get('Accept')).toBe(
19 'application/vnd.github+json',
20 )
21 expect(new Headers(authed.headers).get('User-Agent')).toBe('modelschemas')
22​
23 const raw = githubRequestInit(
24 RAW,
25 { GITHUB_TOKEN: 'ghp_test' },
26 {
27 headers: { 'User-Agent': 'modelschemas' },
28 },
29 )
30 expect(authorization(raw)).toBeNull()
31 expect(new Headers(raw.headers).get('User-Agent')).toBe('modelschemas')
32 })
33​
34 it('stays anonymous when the token is missing or blank', () => {
35 expect(authorization(githubRequestInit(API, {}))).toBeNull()
36 expect(authorization(githubRequestInit(API, { GITHUB_TOKEN: ' ' }))).toBe(
37 null,
38 )
39 })
40​
41 it('does not replace an Authorization header the caller already set', () => {
42 const init = githubRequestInit(
43 API,
44 { GITHUB_TOKEN: 'ghp_test' },
45 { headers: { Authorization: 'Bearer other' } },
46 )
47 expect(authorization(init)).toBe('Bearer other')
48 })
49})