diff --git a/packages/react-dev-utils/WebpackDevServerUtils.js b/packages/react-dev-utils/WebpackDevServerUtils.js index 07a1ac873ee..2cdae20d59f 100644 --- a/packages/react-dev-utils/WebpackDevServerUtils.js +++ b/packages/react-dev-utils/WebpackDevServerUtils.js @@ -422,10 +422,11 @@ function prepareProxy(proxy, appPublicFolder, servedPathname) { // If this heuristic doesn’t work well for you, use `src/setupProxy.js`. context: function(pathname, req) { return ( - req.method !== 'GET' || - (mayProxy(pathname) && + !( + req.method === 'GET' && req.headers.accept && - req.headers.accept.indexOf('text/html') === -1) + req.headers.accept.indexOf('text/html') !== -1 + ) && mayProxy(pathname) ); }, onProxyReq: proxyReq => { diff --git a/packages/react-dev-utils/__tests__/prepareProxy.test.js b/packages/react-dev-utils/__tests__/prepareProxy.test.js new file mode 100644 index 00000000000..ae18f98173f --- /dev/null +++ b/packages/react-dev-utils/__tests__/prepareProxy.test.js @@ -0,0 +1,77 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +'use strict'; + +const { prepareProxy } = require('../WebpackDevServerUtils'); + +const requests = [ + { + pathname: '/', + req: { + method: 'GET', + headers: { + accept: 'text/html', + }, + }, + expect: false, + }, + { + pathname: '/about', + req: { + method: 'GET', + headers: { + accept: 'text/html', + }, + }, + expect: false, + }, + { + pathname: '/api/fetch', + req: { + method: 'GET', + headers: { + accept: '*/*', + }, + }, + expect: true, + }, + { + pathname: '/api/create', + req: { + method: 'POST', + headers: { + accept: 'application/json', + }, + }, + expect: true, + }, + { + pathname: '/socket/proxy', + req: { + method: 'GET', + headers: {}, + }, + expect: true, + }, +]; + +describe('follows proxy heuristics', () => { + let proxy = null; + + beforeAll(() => { + proxy = prepareProxy('http://localhost:3001', '/public', '/')[0]; + }); + + requests.forEach(t => { + test(`proxies ${t.pathname}`, () => { + const filter = proxy.context; + const actual = filter(t.pathname, t.req); + expect(actual).toBe(t.expect); + }); + }); +});