Skip to content

Commit

Permalink
External tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codyebberson committed Aug 31, 2022
1 parent 0361f4c commit 504883b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 deletions.
16 changes: 11 additions & 5 deletions test/external/imports/default-import/ts-cjs/source/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import rateLimit, {
IncrementResponse,
} from 'express-rate-limit'

class TestStore implements Store {
export class TestStore implements Store {
hits: Record<string, number> = {}

async increment(key: string): Promise<IncrementResponse> {
Expand All @@ -28,19 +28,25 @@ class TestStore implements Store {
async resetKey(key: string): Promise<void> {
delete this.hits[key]
}

async shutdown(): Promise<void> {
console.log('Shutdown successful')
}
}

const app = createServer()
export const app = createServer()

export const store = Math.floor(Math.random() * 2)
? new TestStore()
: new MemoryStore()

app.use(
rateLimit({
max: 2,
legacyHeaders: false,
standardHeaders: true,
store: Math.floor(Math.random() * 2) ? new TestStore() : new MemoryStore(),
store,
}),
)

app.get('/', (request, response) => response.send('Hello!'))

export default app
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// /source/index.ts
// Run the server

import app from './app'
import { app } from './app'

app.listen(8080, () =>
console.log('Make a GET request to http://localhost:8080!'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
// /test/server-test.ts
// Tests the server's rate limiting middleware

import { jest } from '@jest/globals'
import { agent as request } from 'supertest'

import app from '../source/app.js'
import { app, store, TestStore } from '../source/app.js'

test('rate limiting middleware', async () => {
jest.spyOn(global.console, 'log').mockImplementation(() => undefined)
await request(app).get('/').expect(200)
await request(app).get('/').expect(200)
await request(app).get('/').expect(429)
await store.shutdown()
if (store instanceof TestStore) {
expect(console.log).toHaveBeenCalledWith('Shutdown successful')
}
})
16 changes: 11 additions & 5 deletions test/external/imports/default-import/ts-esm/source/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import rateLimit, {
IncrementResponse,
} from 'express-rate-limit'

class TestStore implements Store {
export class TestStore implements Store {
hits: Record<string, number> = {}

async increment(key: string): Promise<IncrementResponse> {
Expand All @@ -28,19 +28,25 @@ class TestStore implements Store {
async resetKey(key: string): Promise<void> {
delete this.hits[key]
}

async shutdown(): Promise<void> {
console.log('Shutdown successful')
}
}

const app = createServer()
export const app = createServer()

export const store = Math.floor(Math.random() * 2)
? new TestStore()
: new MemoryStore()

app.use(
rateLimit({
max: 2,
legacyHeaders: false,
standardHeaders: true,
store: Math.floor(Math.random() * 2) ? new TestStore() : new MemoryStore(),
store,
}),
)

app.get('/', (request, response) => response.send('Hello!'))

export default app
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// /source/index.ts
// Run the server

import app from './app.js'
import { app } from './app.js'

app.listen(8080, () =>
console.log('Make a GET request to http://localhost:8080!'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
// /test/server-test.ts
// Tests the server's rate limiting middleware

import { jest } from '@jest/globals'
import { agent as request } from 'supertest'

import app from '../source/app.js'
import { app, store, TestStore } from '../source/app.js'

test('rate limiting middleware', async () => {
jest.spyOn(global.console, 'log').mockImplementation(() => undefined)
await request(app).get('/').expect(200)
await request(app).get('/').expect(200)
await request(app).get('/').expect(429)
await store.shutdown()
if (store instanceof TestStore) {
expect(console.log).toHaveBeenCalledWith('Shutdown successful')
}
})

0 comments on commit 504883b

Please sign in to comment.