mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-29 02:16:28 +02:00
proxy: add test to confirm prefix routing behaves as expected
This commit is contained in:
parent
9e66471c07
commit
5be8265e62
1 changed files with 63 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -349,3 +350,65 @@ func TestRouteMatcherFuncFromPolicy(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPolicyPrefixRouting(t *testing.T) {
|
||||||
|
adminServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
io.WriteString(w, "admin: "+r.URL.Path)
|
||||||
|
}))
|
||||||
|
defer adminServer.Close()
|
||||||
|
|
||||||
|
publicServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
io.WriteString(w, "public: "+r.URL.Path)
|
||||||
|
}))
|
||||||
|
defer publicServer.Close()
|
||||||
|
|
||||||
|
opts := testOptions(t)
|
||||||
|
opts.Policies = []config.Policy{
|
||||||
|
{
|
||||||
|
From: "https://from.example.com",
|
||||||
|
To: "http://" + adminServer.Listener.Addr().String(),
|
||||||
|
Prefix: "/admin",
|
||||||
|
AllowPublicUnauthenticatedAccess: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
From: "https://from.example.com",
|
||||||
|
To: "http://" + publicServer.Listener.Addr().String(),
|
||||||
|
AllowPublicUnauthenticatedAccess: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
p, err := New(opts)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error creating proxy: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("admin", func(t *testing.T) {
|
||||||
|
req, err := http.NewRequest("GET", "https://from.example.com/admin/path", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error creating http request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
p.ServeHTTP(rr, req)
|
||||||
|
rr.Flush()
|
||||||
|
|
||||||
|
if rr.Body.String() != "admin: /admin/path" {
|
||||||
|
t.Errorf("expected admin request to go to the admin backend")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("non-admin", func(t *testing.T) {
|
||||||
|
req, err := http.NewRequest("GET", "https://from.example.com/nonadmin/path", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error creating http request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
p.ServeHTTP(rr, req)
|
||||||
|
rr.Flush()
|
||||||
|
|
||||||
|
if rr.Body.String() != "public: /nonadmin/path" {
|
||||||
|
t.Errorf("expected non-admin request to go to the public backend")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue