2021-04-02 03:13:49 -05:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSSL_Redirect(t *testing.T) {
|
|
|
|
p := Http{}
|
|
|
|
|
|
|
|
ts := httptest.NewServer(p.httpToHTTPSRouter())
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client := http.Client{
|
|
|
|
// prevent http redirect
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
|
|
|
|
// allow self-signed certificate
|
|
|
|
Transport: &http.Transport{
|
2021-04-03 01:00:09 -05:00
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //nolint
|
2021-04-02 03:13:49 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// check http to https redirect response
|
|
|
|
resp, err := client.Get(strings.Replace(ts.URL, "127.0.0.1", "localhost", 1) + "/blah?param=1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
assert.Equal(t, 307, resp.StatusCode)
|
|
|
|
assert.Equal(t, "https://localhost:443/blah?param=1", resp.Header.Get("Location"))
|
|
|
|
}
|