Browse Source

Support the same URL with multiple methods in redirects

James Allen 8 years ago
parent
commit
15103ac894

+ 2 - 3
services/web/app/coffee/infrastructure/RedirectManager.coffee

@@ -4,8 +4,7 @@ logger = require("logger-sharelatex")
 module.exports = RedirectManager =
 	apply: (webRouter) ->
 		for redirectUrl, target of settings.redirects
-			do (target) ->
-				method = target.method || 'get'
+			for method in (target.methods or ['get'])
 				webRouter[method] redirectUrl, RedirectManager.createRedirect(target)
 
 	createRedirect: (target) ->
@@ -14,7 +13,7 @@ module.exports = RedirectManager =
 			if typeof target is 'string'
 				url = target
 			else
-				if target.method == "post"
+				if req.method == "POST"
 					code = 307
 				if typeof target.url == "function"
 					url = target.url(req.params)

+ 5 - 2
services/web/test/acceptance/coffee/RedirectUrlsTests.coffee

@@ -24,5 +24,8 @@ describe "RedirectUrls", ->
 	it 'proxy URLs with baseUrl', (done) ->
 		assertRedirect 'get', '/redirect/base_url', 302, 'https://example.com/destination/base_url', done
 
-	it 'proxy URLs with POST', (done) ->
-		assertRedirect 'post', '/redirect/post', 307, '/destination/post', done
+	it 'proxy URLs with POST with a 307', (done) ->
+		assertRedirect 'post', '/redirect/get_and_post', 307, '/destination/get_and_post', done
+
+	it 'proxy URLs with multiple support methods', (done) ->
+		assertRedirect 'get', '/redirect/get_and_post', 302, '/destination/get_and_post', done

+ 7 - 3
services/web/test/acceptance/config/settings.test.coffee

@@ -105,15 +105,19 @@ module.exports =
 			path: (params) -> "/universities/list/#{params.id}"
 		'/institutions/domains': { baseUrl: v1Api.url, path: '/university/domains' }
 		'/proxy/missing/baseUrl': path: '/foo/bar'
+		'/proxy/get_and_post': {
+			methods: ['get', 'post'],
+			path: '/destination/get_and_post'
+		}
 
 	overleaf:
 		host: "http://overleaf.test:5000"
 
 	redirects:
 		'/redirect/one': '/destination/one',
-		'/redirect/post': {
-			method: 'post',
-			url: '/destination/post'
+		'/redirect/get_and_post': {
+			methods: ['get', 'post'],
+			url: '/destination/get_and_post'
 		},
 		'/redirect/base_url': {
 			baseUrl: 'https://example.com'