git.iko.soy/typeable/oauth2-proxy
on master help sign in
Add SSL check and test no redirect when HTTPS

Signed-off-by: Josh Michielsen <github@mickey.dev>

commitbed0336608ca139cd5617be1949ae05aa6f6dc0e authorJosh Michielsen <github@mickey.dev> dateThu 17 Oct 2019, 22:04 (7 years ago) parent271efe7
2 files changed+22−1
http.go+1−1 http_test.go+21
http.go+1−1
@@ -155,7 +155,7 @@ func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
155155
156156func redirectToHTTPS(opts *Options, h http.Handler) http.Handler {
157157 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
158− if opts.ForceHTTPS {
158+ if opts.ForceHTTPS && r.TLS == nil {
159159 http.Redirect(w, r, opts.HTTPSAddress, http.StatusPermanentRedirect)
160160 }
161161
http_test.go+21
@@ -135,3 +135,24 @@ func TestRedirectToHTTPSFalse(t *testing.T) {
135135
136136 assert.Equal(t, http.StatusOK, rw.Code, "status code should be %d, got: %d", http.StatusOK, rw.Code)
137137}
138+
139+func TestRedirectNotWhenHTTPS(t *testing.T) {
140+ opts := NewOptions()
141+ opts.ForceHTTPS = true
142+ handler := func(w http.ResponseWriter, req *http.Request) {
143+ w.Write([]byte("test"))
144+ }
145+
146+ h := redirectToHTTPS(opts, http.HandlerFunc(handler))
147+ s := httptest.NewTLSServer(h)
148+ defer s.Close()
149+
150+ opts.HTTPSAddress = s.URL
151+ client := s.Client()
152+ res, err := client.Get(s.URL)
153+ if err != nil {
154+ t.Fatalf("request to test server failed with error: %v", err)
155+ }
156+
157+ assert.Equal(t, http.StatusOK, res.StatusCode, "status code should be %d, got: %d", http.StatusOK, res.StatusCode)
158+}