Skip to content

Commit

Permalink
fix: respect the bypass option with target/router options for p…
Browse files Browse the repository at this point in the history
…roxy
  • Loading branch information
alexander-akait authored Aug 19, 2021
1 parent 11d99c0 commit b5dd568
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 64 deletions.
16 changes: 8 additions & 8 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,15 +879,17 @@ class Server {
const { createProxyMiddleware } = require("http-proxy-middleware");

const getProxyMiddleware = (proxyConfig) => {
const context = proxyConfig.context || proxyConfig.path;

// It is possible to use the `bypass` method without a `target`.
// It is possible to use the `bypass` method without a `target` or `router`.
// However, the proxy middleware has no use in this case, and will fail to instantiate.
if (context) {
if (proxyConfig.target) {
const context = proxyConfig.context || proxyConfig.path;

return createProxyMiddleware(context, proxyConfig);
}

return createProxyMiddleware(proxyConfig);
if (proxyConfig.router) {
return createProxyMiddleware(proxyConfig);
}
};
/**
* Assume a proxy configuration specified as:
Expand All @@ -913,9 +915,7 @@ class Server {
? proxyConfigOrCallback()
: proxyConfigOrCallback;

if (!proxyConfig.bypass) {
proxyMiddleware = getProxyMiddleware(proxyConfig);
}
proxyMiddleware = getProxyMiddleware(proxyConfig);

if (proxyConfig.ws) {
this.webSocketProxies.push(proxyMiddleware);
Expand Down
79 changes: 23 additions & 56 deletions test/server/proxy-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ const proxyOptionPathsAsProperties = {
}
},
},
"/bypass-with-target": {
target: `http://localhost:${port1}`,
changeOrigin: true,
secure: false,
bypass(req) {
if (/\.(html)$/i.test(req.url)) {
return req.url;
}
},
},
};

const proxyOption = {
Expand Down Expand Up @@ -223,6 +233,19 @@ describe("proxy option", () => {
expect(response.status).toEqual(200);
expect(response.text).toContain("proxy async response");
});

it("should work with the 'target' option", async () => {
const response = await req.get("/bypass-with-target/foo.js");

expect(response.status).toEqual(404);
});

it("should work with the 'target' option #2", async () => {
const response = await req.get("/bypass-with-target/index.html");

expect(response.status).toEqual(200);
expect(response.text).toContain("Hello");
});
});
});

Expand All @@ -235,10 +258,6 @@ describe("proxy option", () => {

server = new Server(
{
static: {
directory: staticDirectory,
watch: false,
},
proxy: proxyOption,
port: port3,
},
Expand Down Expand Up @@ -274,10 +293,6 @@ describe("proxy option", () => {

server = new Server(
{
static: {
directory: staticDirectory,
watch: false,
},
proxy: proxyWithString,
port: port3,
},
Expand Down Expand Up @@ -313,10 +328,6 @@ describe("proxy option", () => {

server = new Server(
{
static: {
directory: staticDirectory,
watch: false,
},
proxy: proxyWithPath,
port: port3,
},
Expand Down Expand Up @@ -352,10 +363,6 @@ describe("proxy option", () => {

server = new Server(
{
static: {
directory: staticDirectory,
watch: false,
},
proxy: proxyWithRouterAsObject,
port: port3,
},
Expand Down Expand Up @@ -391,10 +398,6 @@ describe("proxy option", () => {

server = new Server(
{
static: {
directory: staticDirectory,
watch: false,
},
proxy: proxyOptionOfArray,
port: port3,
},
Expand Down Expand Up @@ -444,10 +447,6 @@ describe("proxy option", () => {

server = new Server(
{
static: {
directory: staticDirectory,
watch: false,
},
proxy: proxyOptionOfArrayWithoutTarget,
port: port3,
},
Expand Down Expand Up @@ -488,10 +487,6 @@ describe("proxy option", () => {

server = new Server(
{
static: {
directory: staticDirectory,
watch: false,
},
proxy: {
"/proxy1": proxyTarget,
"/proxy2": proxyTarget,
Expand Down Expand Up @@ -553,10 +548,6 @@ describe("proxy option", () => {

server = new Server(
{
static: {
directory: staticDirectory,
watch: false,
},
webSocketServer: webSocketServerType,
proxy: [
{
Expand Down Expand Up @@ -623,10 +614,6 @@ describe("proxy option", () => {

server = new Server(
{
static: {
directory: staticDirectory,
watch: false,
},
proxy: {
"**": proxyTarget,
},
Expand Down Expand Up @@ -755,10 +742,6 @@ describe("proxy option", () => {

server = new Server(
{
static: {
directory: staticDirectory,
watch: false,
},
proxy: {
"*": {
context: () => true,
Expand Down Expand Up @@ -808,10 +791,6 @@ describe("proxy option", () => {

server = new Server(
{
static: {
directory: staticDirectory,
watch: false,
},
proxy: {
"/my-path": {
target: "http://unknown:1234",
Expand Down Expand Up @@ -863,10 +842,6 @@ describe("proxy option", () => {

server = new Server(
{
static: {
directory: staticDirectory,
watch: false,
},
proxy: {
"/my-path": {
target: "http://unknown:1234",
Expand Down Expand Up @@ -921,10 +896,6 @@ describe("proxy option", () => {

server = new Server(
{
static: {
directory: staticDirectory,
watch: false,
},
proxy: {
"/my-path": {
target: "http://unknown:1234",
Expand Down Expand Up @@ -978,10 +949,6 @@ describe("proxy option", () => {

server = new Server(
{
static: {
directory: staticDirectory,
watch: false,
},
proxy: {
"/my-path": {
target: "http://unknown:1234",
Expand Down

0 comments on commit b5dd568

Please sign in to comment.