Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
henrygd committed Jan 28, 2024
1 parent 1dbfbf5 commit 1c1c73d
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { app } from '../src/index'
import { describe, expect, it } from 'bun:test'

describe('General', () => {
it('home route redirects to github', async () => {
const response = await app.handle(new Request('http://localhost/'))
expect(response.headers.get('Location')).toBe('https://github.com/henrygd/ncaa-api')
})
it('invalid route returns 400', async () => {
const response = await app.handle(new Request('http://localhost/invalid'))
expect(response.status).toBe(400)
})
it('invalid page param returns 400', async () => {
const response = await app.handle(new Request('http://localhost/stats/test?page=invalid'))
expect(response.status).toBe(400)
})
it('valid route returns good data', async () => {
const response = await app.handle(
new Request('http://localhost/rankings/football/fbs/associated-press')
)
expect(response.status).toBe(200)
const { data } = await response.json()
expect(data.length).toBeGreaterThan(0)
expect(data[0]).toContainKeys(['RANK', 'SCHOOL'])
})
})

describe('Header validation', () => {
// Custom header tests (must be last due to env var)
it('valid custom header returns 200', async () => {
Bun.env.NCAA_HEADER_KEY = 'valid'
const response = await app.handle(
new Request('http://localhost/rankings/football/fbs/associated-press', {
headers: { 'x-ncaa-key': 'valid' },
})
)
expect(response.status).toBe(200)
})
it('invalid custom header returns 401', async () => {
const response = await app.handle(
new Request('http://localhost/test', { headers: { 'x-ncaa-key': 'invalid' } })
)
expect(response.status).toBe(401)
})
it('lack of custom header returns 401', async () => {
const response = await app.handle(new Request('http://localhost/stats/test'))
expect(response.status).toBe(401)
})
})

0 comments on commit 1c1c73d

Please sign in to comment.