-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganise stuff into separate echo servers
TODO fix localhost hostname matching
- Loading branch information
1 parent
c02df8d
commit e7b3dd1
Showing
9 changed files
with
160 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package util | ||
|
||
import ( | ||
"github.com/labstack/echo" | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
) | ||
|
||
// var func to allow overriding in tests | ||
var serveProxy = func(proxy *httputil.ReverseProxy, req *http.Request, res http.ResponseWriter) { | ||
proxy.ServeHTTP(res, req) | ||
} | ||
|
||
func Proxy(c echo.Context, target *url.URL) { | ||
proxy := &httputil.ReverseProxy{ | ||
Director: func(req *http.Request) { | ||
// Change the URL | ||
req.URL = target | ||
req.Header.Set("X-Forwarded-Host", req.Host) | ||
req.Host = target.Host | ||
|
||
// Don't send our cookies to github | ||
req.Header.Del(echo.HeaderCookie) | ||
req.Header.Del(echo.HeaderAuthorization) | ||
}, | ||
} | ||
|
||
serveProxy(proxy, c.Request(), c.Response()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package util | ||
|
||
import ( | ||
"github.com/labstack/echo" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/http/httputil" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func TestProxy(t *testing.T) { | ||
// Override serveProxy and store what's passed into it | ||
var ( | ||
servedCount = 0 | ||
servedProxy *httputil.ReverseProxy | ||
servedReq *http.Request | ||
servedRes http.ResponseWriter | ||
) | ||
serveProxy = func(proxy *httputil.ReverseProxy, req *http.Request, res http.ResponseWriter) { | ||
servedCount++ | ||
servedProxy = proxy | ||
servedReq = req | ||
servedRes = res | ||
} | ||
|
||
// Setup the request | ||
e := echo.New() | ||
req := httptest.NewRequest(http.MethodGet, "http://foobar.host/changelog", nil) | ||
rec := httptest.NewRecorder() | ||
c := e.NewContext(req, rec) | ||
c.SetPath("/changelog") | ||
|
||
target, _ := url.Parse("https://impactdevelopment.github.io/Impact/changelog") | ||
|
||
// Run the handler | ||
Proxy(c, target) | ||
// Basic checks | ||
assert.Equal(t, 1, servedCount) | ||
assert.NotNil(t, servedProxy) | ||
assert.NotNil(t, servedReq) | ||
assert.NotNil(t, servedRes) | ||
|
||
// Request should be unchanged | ||
assert.Equal(t, "", servedReq.Header.Get("X-Forwarded-Host")) | ||
assert.Equal(t, "foobar.host", servedReq.Host) | ||
assert.Equal(t, "foobar.host", servedReq.URL.Host) | ||
assert.Equal(t, "/changelog", servedReq.URL.Path) | ||
assert.Equal(t, "", servedReq.URL.RawQuery) | ||
assert.Equal(t, "http://foobar.host/changelog", servedReq.URL.String()) | ||
|
||
// The Director function should mutate the request | ||
servedProxy.Director(servedReq) | ||
assert.Equal(t, "foobar.host", servedReq.Header.Get("X-Forwarded-Host")) | ||
assert.Equal(t, "impactdevelopment.github.io", servedReq.Host) | ||
assert.Equal(t, "https", servedReq.URL.Scheme) | ||
assert.Equal(t, "impactdevelopment.github.io", servedReq.URL.Host) | ||
assert.Equal(t, "/Impact/changelog", servedReq.URL.Path) | ||
assert.Equal(t, "", servedReq.URL.RawQuery) | ||
assert.Equal(t, target.String(), servedReq.URL.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.