2022-02-08 15:30:26 +00:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"goauthentik.io/internal/outpost/proxyv2/constants"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProxy_ModifyRequest(t *testing.T) {
|
|
|
|
a := newTestApplication()
|
|
|
|
req, _ := http.NewRequest("GET", "http://frontend/foo", nil)
|
|
|
|
u, err := url.Parse("http://backend:8012")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
a.proxyModifyRequest(u)(req)
|
|
|
|
|
2022-02-10 22:09:55 +00:00
|
|
|
assert.Equal(t, "frontend", req.Header.Get("X-Forwarded-Host"))
|
2022-02-08 15:30:26 +00:00
|
|
|
assert.Equal(t, "/foo", req.URL.Path)
|
|
|
|
assert.Equal(t, "backend:8012", req.URL.Host)
|
2022-02-14 11:39:16 +00:00
|
|
|
assert.Equal(t, "frontend", req.Host)
|
2022-02-08 15:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxy_ModifyRequest_Claims(t *testing.T) {
|
|
|
|
a := newTestApplication()
|
|
|
|
req, _ := http.NewRequest("GET", "http://frontend/foo", nil)
|
|
|
|
u, err := url.Parse("http://backend:8012")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
|
2022-05-20 08:10:26 +00:00
|
|
|
s, _ := a.sessions.Get(req, constants.SessionName)
|
2022-02-08 15:30:26 +00:00
|
|
|
s.Values[constants.SessionClaims] = Claims{
|
|
|
|
Sub: "foo",
|
|
|
|
Proxy: &ProxyClaims{
|
|
|
|
BackendOverride: "http://other-backend:8123",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = a.sessions.Save(req, rr, s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.proxyModifyRequest(u)(req)
|
|
|
|
|
|
|
|
assert.Equal(t, "/foo", req.URL.Path)
|
|
|
|
assert.Equal(t, "other-backend:8123", req.URL.Host)
|
2022-02-14 11:39:16 +00:00
|
|
|
assert.Equal(t, "frontend", req.Host)
|
2022-02-08 15:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxy_ModifyRequest_Claims_Invalid(t *testing.T) {
|
|
|
|
a := newTestApplication()
|
|
|
|
req, _ := http.NewRequest("GET", "http://frontend/foo", nil)
|
|
|
|
u, err := url.Parse("http://backend:8012")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
|
2022-05-20 08:10:26 +00:00
|
|
|
s, _ := a.sessions.Get(req, constants.SessionName)
|
2022-02-08 15:30:26 +00:00
|
|
|
s.Values[constants.SessionClaims] = Claims{
|
|
|
|
Sub: "foo",
|
|
|
|
Proxy: &ProxyClaims{
|
|
|
|
BackendOverride: ":qewr",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = a.sessions.Save(req, rr, s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.proxyModifyRequest(u)(req)
|
|
|
|
|
|
|
|
assert.Equal(t, "/foo", req.URL.Path)
|
|
|
|
assert.Equal(t, "backend:8012", req.URL.Host)
|
2022-02-14 11:39:16 +00:00
|
|
|
assert.Equal(t, "frontend", req.Host)
|
2022-02-08 15:30:26 +00:00
|
|
|
}
|