Skip to content

Commit

Permalink
Add a test for progress events when sending Buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed Dec 7, 2023
1 parent b9cd634 commit 0e41fd2
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/spec/test-node-specific.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ const stream = require('stream')
const temp = require('temp')
const fs = require('fs')
const https = require('https')
const http = require('http')
const crypto = require('crypto')
const intoStream = require('into-stream')
const { once } = require('events')
const tus = require('../..')
const assertUrlStorage = require('./helpers/assertUrlStorage')
const { TestHttpStack, waitableFunction } = require('./helpers/utils')
Expand Down Expand Up @@ -400,6 +403,43 @@ describe('tus', () => {
expect(req.getUnderlyingObject().agent).toBe(customAgent)
expect(req.getUnderlyingObject().agent).not.toBe(https.globalAgent)
})

it('should emit progress events when sending a Buffer', async () => {
// Start a simple HTTP server on a random port that accepts POST requests.
const server = http.createServer((req, res) => {
if (req.method === 'POST') {
req.on('data', () => {})
req.on('end', () => {
res.writeHead(200)
res.end('Data received')
})
} else {
res.writeHead(404)
res.end('Not found')
}
})

server.listen(0)
await once(server, 'listening')
const { port } = server.address()

const progressEvents = []

// Send POST request with 20MB of random data
const randomData = crypto.randomBytes(20 * 1024 * 1024)
const stack = new tus.DefaultHttpStack({})
const req = stack.createRequest('POST', `http://localhost:${port}`)
req.setProgressHandler((bytesSent) => progressEvents.push(bytesSent))
await req.send(randomData)

server.close()

// We should have received progress events and at least one event should not be for 0% or 100%.
expect(progressEvents.length).toBeGreaterThan(0)
expect(
progressEvents.some((bytesSent) => bytesSent !== 0 && bytesSent !== randomData.length),
).toBeTrue()
})
})

describe('#StreamSource', () => {
Expand Down

0 comments on commit 0e41fd2

Please sign in to comment.