mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2025-03-19 21:27:58 +02:00
Add SSL check and test no redirect when HTTPS
Signed-off-by: Josh Michielsen <github@mickey.dev>
This commit is contained in:
parent
271efe776e
commit
bed0336608
2
http.go
2
http.go
@ -155,7 +155,7 @@ func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
|
||||
|
||||
func redirectToHTTPS(opts *Options, h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if opts.ForceHTTPS {
|
||||
if opts.ForceHTTPS && r.TLS == nil {
|
||||
http.Redirect(w, r, opts.HTTPSAddress, http.StatusPermanentRedirect)
|
||||
}
|
||||
|
||||
|
21
http_test.go
21
http_test.go
@ -135,3 +135,24 @@ func TestRedirectToHTTPSFalse(t *testing.T) {
|
||||
|
||||
assert.Equal(t, http.StatusOK, rw.Code, "status code should be %d, got: %d", http.StatusOK, rw.Code)
|
||||
}
|
||||
|
||||
func TestRedirectNotWhenHTTPS(t *testing.T) {
|
||||
opts := NewOptions()
|
||||
opts.ForceHTTPS = true
|
||||
handler := func(w http.ResponseWriter, req *http.Request) {
|
||||
w.Write([]byte("test"))
|
||||
}
|
||||
|
||||
h := redirectToHTTPS(opts, http.HandlerFunc(handler))
|
||||
s := httptest.NewTLSServer(h)
|
||||
defer s.Close()
|
||||
|
||||
opts.HTTPSAddress = s.URL
|
||||
client := s.Client()
|
||||
res, err := client.Get(s.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("request to test server failed with error: %v", err)
|
||||
}
|
||||
|
||||
assert.Equal(t, http.StatusOK, res.StatusCode, "status code should be %d, got: %d", http.StatusOK, res.StatusCode)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user