Unverified Commit 7998b63a authored by Steven's avatar Steven Committed by GitHub

Fix missing `Content-Length` header from Image Optimization API (#36581)

Fixes #35514 
parent c838b5f5
...@@ -621,6 +621,7 @@ export function sendResponse( ...@@ -621,6 +621,7 @@ export function sendResponse(
contentSecurityPolicy contentSecurityPolicy
) )
if (!result.finished) { if (!result.finished) {
res.setHeader('Content-Length', Buffer.byteLength(buffer))
res.end(buffer) res.end(buffer)
} }
} }
......
...@@ -70,10 +70,13 @@ export async function fsToJson(dir, output = {}) { ...@@ -70,10 +70,13 @@ export async function fsToJson(dir, output = {}) {
return output return output
} }
export async function expectWidth(res, w) { export async function expectWidth(res, w, { expectAnimated = false } = {}) {
const buffer = await res.buffer() const buffer = await res.buffer()
const d = sizeOf(buffer) const d = sizeOf(buffer)
expect(d.width).toBe(w) expect(d.width).toBe(w)
const lengthStr = res.headers.get('Content-Length')
expect(lengthStr).toBe(Buffer.byteLength(buffer).toString())
expect(isAnimated(buffer)).toBe(expectAnimated)
} }
export const cleanImagesDir = async (ctx) => { export const cleanImagesDir = async (ctx) => {
...@@ -170,7 +173,7 @@ export function runTests(ctx) { ...@@ -170,7 +173,7 @@ export function runTests(ctx) {
expect(res.headers.get('Content-Disposition')).toBe( expect(res.headers.get('Content-Disposition')).toBe(
`inline; filename="animated.gif"` `inline; filename="animated.gif"`
) )
expect(isAnimated(await res.buffer())).toBe(true) await expectWidth(res, 50, { expectAnimated: true })
}) })
it('should maintain animated png', async () => { it('should maintain animated png', async () => {
...@@ -186,7 +189,7 @@ export function runTests(ctx) { ...@@ -186,7 +189,7 @@ export function runTests(ctx) {
expect(res.headers.get('Content-Disposition')).toBe( expect(res.headers.get('Content-Disposition')).toBe(
`inline; filename="animated.png"` `inline; filename="animated.png"`
) )
expect(isAnimated(await res.buffer())).toBe(true) await expectWidth(res, 100, { expectAnimated: true })
}) })
it('should maintain animated png 2', async () => { it('should maintain animated png 2', async () => {
...@@ -202,7 +205,7 @@ export function runTests(ctx) { ...@@ -202,7 +205,7 @@ export function runTests(ctx) {
expect(res.headers.get('Content-Disposition')).toBe( expect(res.headers.get('Content-Disposition')).toBe(
`inline; filename="animated2.png"` `inline; filename="animated2.png"`
) )
expect(isAnimated(await res.buffer())).toBe(true) await expectWidth(res, 1105, { expectAnimated: true })
}) })
it('should maintain animated webp', async () => { it('should maintain animated webp', async () => {
...@@ -218,7 +221,7 @@ export function runTests(ctx) { ...@@ -218,7 +221,7 @@ export function runTests(ctx) {
expect(res.headers.get('Content-Disposition')).toBe( expect(res.headers.get('Content-Disposition')).toBe(
`inline; filename="animated.webp"` `inline; filename="animated.webp"`
) )
expect(isAnimated(await res.buffer())).toBe(true) await expectWidth(res, 400, { expectAnimated: true })
}) })
if (ctx.dangerouslyAllowSVG) { if (ctx.dangerouslyAllowSVG) {
...@@ -227,6 +230,7 @@ export function runTests(ctx) { ...@@ -227,6 +230,7 @@ export function runTests(ctx) {
const opts = { headers: { accept: 'image/webp' } } const opts = { headers: { accept: 'image/webp' } }
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts) const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts)
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('Content-Length')).toBe('603')
expect(res.headers.get('Content-Type')).toContain('image/svg+xml') expect(res.headers.get('Content-Type')).toContain('image/svg+xml')
expect(res.headers.get('Cache-Control')).toBe( expect(res.headers.get('Cache-Control')).toBe(
`public, max-age=0, must-revalidate` `public, max-age=0, must-revalidate`
...@@ -329,6 +333,7 @@ export function runTests(ctx) { ...@@ -329,6 +333,7 @@ export function runTests(ctx) {
expect(res.headers.get('Content-Disposition')).toBe( expect(res.headers.get('Content-Disposition')).toBe(
`inline; filename="test.jpeg"` `inline; filename="test.jpeg"`
) )
await expectWidth(res, ctx.w)
}) })
if (!ctx.isOutdatedSharp) { if (!ctx.isOutdatedSharp) {
...@@ -348,6 +353,7 @@ export function runTests(ctx) { ...@@ -348,6 +353,7 @@ export function runTests(ctx) {
expect(res.headers.get('Content-Disposition')).toBe( expect(res.headers.get('Content-Disposition')).toBe(
`inline; filename="test.jpeg"` `inline; filename="test.jpeg"`
) )
await expectWidth(res, ctx.w)
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment