1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Allow overriding Content-Type (#3242)

* Allow overriding Content-Type

* Mock getHeader
This commit is contained in:
Tim Neutkens 2017-11-05 20:17:03 +01:00 committed by GitHub
parent 828dac7d61
commit 1424b84c98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 7 deletions

View file

@ -185,7 +185,9 @@ export function sendHTML (req, res, html, method, { dev }) {
} }
res.setHeader('ETag', etag) res.setHeader('ETag', etag)
res.setHeader('Content-Type', 'text/html') if (!res.getHeader('Content-Type')) {
res.setHeader('Content-Type', 'text/html')
}
res.setHeader('Content-Length', Buffer.byteLength(html)) res.setHeader('Content-Length', Buffer.byteLength(html))
res.end(method === 'HEAD' ? null : html) res.end(method === 'HEAD' ? null : html)
} }

View file

@ -0,0 +1,13 @@
import React from 'react'
export default class extends React.Component {
static async getInitialProps ({res}) {
if (res) {
res.setHeader('Content-Type', 'text/html; charset=utf-8')
}
return {}
}
render () {
return null
}
}

View file

@ -3,6 +3,7 @@
import { join } from 'path' import { join } from 'path'
import { import {
renderViaHTTP, renderViaHTTP,
fetchViaHTTP,
findPort, findPort,
launchApp, launchApp,
killApp killApp
@ -55,7 +56,7 @@ describe('Basic Features', () => {
}) })
afterAll(() => killApp(context.server)) afterAll(() => killApp(context.server))
rendering(context, 'Rendering via HTTP', (p, q) => renderViaHTTP(context.appPort, p, q)) rendering(context, 'Rendering via HTTP', (p, q) => renderViaHTTP(context.appPort, p, q), (p, q) => fetchViaHTTP(context.appPort, p, q))
clientNavigation(context, (p, q) => renderViaHTTP(context.appPort, p, q)) clientNavigation(context, (p, q) => renderViaHTTP(context.appPort, p, q))
dynamic(context, (p, q) => renderViaHTTP(context.appPort, p, q)) dynamic(context, (p, q) => renderViaHTTP(context.appPort, p, q))
hmr(context, (p, q) => renderViaHTTP(context.appPort, p, q)) hmr(context, (p, q) => renderViaHTTP(context.appPort, p, q))

View file

@ -2,7 +2,7 @@
import cheerio from 'cheerio' import cheerio from 'cheerio'
export default function ({ app }, suiteName, render) { export default function ({ app }, suiteName, render, fetch) {
async function get$ (path, query) { async function get$ (path, query) {
const html = await render(path, query) const html = await render(path, query)
return cheerio.load(html) return cheerio.load(html)
@ -63,6 +63,16 @@ export default function ({ app }, suiteName, render) {
expect($('pre').text().includes(expectedErrorMessage)).toBeTruthy() expect($('pre').text().includes(expectedErrorMessage)).toBeTruthy()
}) })
test('default Content-Type', async () => {
const res = await fetch('/stateless')
expect(res.headers.get('Content-Type')).toMatch('text/html')
})
test('setting Content-Type in getInitialProps', async () => {
const res = await fetch('/custom-encoding')
expect(res.headers.get('Content-Type')).toMatch('text/html; charset=utf-8')
})
test('allows to import .json files', async () => { test('allows to import .json files', async () => {
const html = await render('/json') const html = await render('/json')
expect(html.includes('Zeit')).toBeTruthy() expect(html.includes('Zeit')).toBeTruthy()

View file

@ -117,6 +117,9 @@ describe('Production Usage', () => {
const req = { url: '/stateless', headers: {} } const req = { url: '/stateless', headers: {} }
const headers = {} const headers = {}
const res = { const res = {
getHeader (key) {
return headers[key]
},
setHeader (key, value) { setHeader (key, value) {
headers[key] = value headers[key] = value
}, },
@ -132,6 +135,9 @@ describe('Production Usage', () => {
const originalConfigValue = app.config.poweredByHeader const originalConfigValue = app.config.poweredByHeader
app.config.poweredByHeader = false app.config.poweredByHeader = false
const res = { const res = {
getHeader () {
return false
},
setHeader (key, value) { setHeader (key, value) {
if (key === 'X-Powered-By') { if (key === 'X-Powered-By') {
throw new Error('Should not set the X-Powered-By header') throw new Error('Should not set the X-Powered-By header')

View file

@ -23,8 +23,12 @@ export function renderViaAPI (app, pathname, query) {
} }
export function renderViaHTTP (appPort, pathname, query) { export function renderViaHTTP (appPort, pathname, query) {
return fetchViaHTTP(appPort, pathname, query).then((res) => res.text())
}
export function fetchViaHTTP (appPort, pathname, query) {
const url = `http://localhost:${appPort}${pathname}${query ? `?${qs.stringify(query)}` : ''}` const url = `http://localhost:${appPort}${pathname}${query ? `?${qs.stringify(query)}` : ''}`
return fetch(url).then((res) => res.text()) return fetch(url)
} }
export function findPort () { export function findPort () {

View file

@ -6071,9 +6071,9 @@ strip-json-comments@~2.0.1:
version "2.0.1" version "2.0.1"
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a" resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"
styled-jsx@2.1.1: styled-jsx@2.1.2:
version "2.1.1" version "2.1.2"
resolved "https://registry.yarnpkg.com/styled-jsx/-/styled-jsx-2.1.1.tgz#e7481c7554df50d605cdc84a4e53c58fec3449b5" resolved "https://registry.yarnpkg.com/styled-jsx/-/styled-jsx-2.1.2.tgz#c84823402678022355b090d98d959ba126b97911"
dependencies: dependencies:
babel-plugin-syntax-jsx "6.18.0" babel-plugin-syntax-jsx "6.18.0"
babel-types "6.23.0" babel-types "6.23.0"