|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const request = require("supertest"); |
| 4 | +const path = require("path"); |
| 5 | +const express = require("express"); |
| 6 | +const helper = require("./helper"); |
| 7 | +const config = require("./fixtures/proxy-config/webpack.config"); |
| 8 | + |
| 9 | +const contentBase = path.join(__dirname, "fixtures/proxy-config"); |
| 10 | + |
| 11 | +const proxyOption = { |
| 12 | + "/proxy1": { |
| 13 | + target: "http://localhost:9000", |
| 14 | + }, |
| 15 | + "/api/proxy2": { |
| 16 | + target: "http://localhost:9001", |
| 17 | + pathRewrite: { "^/api": "" }, |
| 18 | + }, |
| 19 | + "/foo": { |
| 20 | + bypass: function(req) { |
| 21 | + if(/\.html$/.test(req.path)) { |
| 22 | + return "/index.html"; |
| 23 | + } |
| 24 | + }, |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +const proxyOptionOfArray = [ |
| 29 | + { context: "/proxy1", target: proxyOption["/proxy1"].target }, |
| 30 | + function() { |
| 31 | + return { |
| 32 | + context: "/api/proxy2", |
| 33 | + target: "http://localhost:9001", |
| 34 | + pathRewrite: { "^/api": "" }, |
| 35 | + }; |
| 36 | + }, |
| 37 | +]; |
| 38 | + |
| 39 | +function startProxyServers() { |
| 40 | + const listeners = []; |
| 41 | + const proxy1 = express(); |
| 42 | + const proxy2 = express(); |
| 43 | + proxy1.get("/proxy1", function(req, res) { |
| 44 | + res.send("from proxy1"); |
| 45 | + }); |
| 46 | + proxy1.get("/api", function(req, res) { |
| 47 | + res.send("api response from proxy1"); |
| 48 | + }); |
| 49 | + proxy2.get("/proxy2", function(req, res) { |
| 50 | + res.send("from proxy2"); |
| 51 | + }); |
| 52 | + listeners.push(proxy1.listen(9000)); |
| 53 | + listeners.push(proxy2.listen(9001)); |
| 54 | + // return a function to shutdown proxy servers |
| 55 | + return function() { |
| 56 | + listeners.forEach(function(listener) { |
| 57 | + listener.close(); |
| 58 | + }); |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +describe("Proxy", function() { |
| 63 | + context("proxy options is a object", function() { |
| 64 | + let server; |
| 65 | + let req; |
| 66 | + let closeProxyServers; |
| 67 | + |
| 68 | + before(function(done) { |
| 69 | + closeProxyServers = startProxyServers(); |
| 70 | + server = helper.start(config, { |
| 71 | + contentBase, |
| 72 | + proxy: proxyOption, |
| 73 | + }, done); |
| 74 | + req = request(server.app); |
| 75 | + }); |
| 76 | + |
| 77 | + after(function(done) { |
| 78 | + helper.close(function() { |
| 79 | + closeProxyServers(); |
| 80 | + done(); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe("target", function() { |
| 85 | + it("respects a proxy option when a request path is matched", function(done) { |
| 86 | + req.get("/proxy1") |
| 87 | + .expect(200, "from proxy1", done); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe("pathRewrite", function() { |
| 92 | + it("respects a pathRewrite option", function(done) { |
| 93 | + req.get("/api/proxy2") |
| 94 | + .expect(200, "from proxy2", done); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe("bypass", function() { |
| 99 | + it("can rewrite a request path", function(done) { |
| 100 | + req.get("/foo/bar.html") |
| 101 | + .expect(200, /Hello/, done); |
| 102 | + }); |
| 103 | + |
| 104 | + it("can rewrite a request path regardless of the target defined a bypass option", function(done) { |
| 105 | + req.get("/baz/hoge.html") |
| 106 | + .expect(200, /Hello/, done); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should pass through a proxy when a bypass function returns null", function(done) { |
| 110 | + req.get("/foo.js") |
| 111 | + .expect(200, /Hey/, done); |
| 112 | + }); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + context("proxy option is an array", function() { |
| 117 | + let server; |
| 118 | + let req; |
| 119 | + let closeProxyServers; |
| 120 | + |
| 121 | + before(function(done) { |
| 122 | + closeProxyServers = startProxyServers(); |
| 123 | + server = helper.start(config, { |
| 124 | + contentBase, |
| 125 | + proxy: proxyOptionOfArray, |
| 126 | + }, done); |
| 127 | + req = request(server.app); |
| 128 | + }); |
| 129 | + |
| 130 | + after(function(done) { |
| 131 | + helper.close(function() { |
| 132 | + closeProxyServers(); |
| 133 | + done(); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it("respects a proxy option", function(done) { |
| 138 | + req.get("/proxy1") |
| 139 | + .expect(200, "from proxy1", done); |
| 140 | + }); |
| 141 | + |
| 142 | + it("respects a proxy option of function", function(done) { |
| 143 | + req.get("/api/proxy2") |
| 144 | + .expect(200, "from proxy2", done); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments