From b3683c4f60f8bf183984ed2d8826482a65c5874e Mon Sep 17 00:00:00 2001 From: Anton Kosourov Date: Sun, 9 Dec 2018 07:46:10 +0300 Subject: [PATCH] add tests for ssl mode (#226) --- backend/app/rest/api/rest_test.go | 91 +++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/backend/app/rest/api/rest_test.go b/backend/app/rest/api/rest_test.go index e10cb527..7525e9c4 100644 --- a/backend/app/rest/api/rest_test.go +++ b/backend/app/rest/api/rest_test.go @@ -2,6 +2,7 @@ package api import ( "bytes" + "crypto/tls" "encoding/json" "io/ioutil" "net/http" @@ -87,6 +88,96 @@ func TestRest_filterComments(t *testing.T) { assert.Equal(t, 2, len(r), "one comment filtered") } +func TestRest_RunStaticSSLMode(t *testing.T) { + srv := Rest{ + Authenticator: auth.Authenticator{}, + AvatarProxy: &proxy.Avatar{ + Store: avatar.NewLocalFS("/tmp", 300), + RoutePath: "/api/v1/avatar", + }, + ImageProxy: &proxy.Image{}, + SSLConfig: SSLConfig{ + SSLMode: Static, + Port: 8443, + Key: "../../cmd/testdata/key.pem", + Cert: "../../cmd/testdata/cert.pem", + }, + RemarkURL: "https://localhost:8443", + } + + go func() { + srv.Run(8080) + }() + + time.Sleep(100 * time.Millisecond) // let server start + + 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{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + }, + } + + resp, err := client.Get("http://localhost:8080/blah?param=1") + require.Nil(t, err) + defer resp.Body.Close() + assert.Equal(t, 307, resp.StatusCode) + assert.Equal(t, "https://localhost:8443/blah?param=1", resp.Header.Get("Location")) + + resp, err = client.Get("https://localhost:8443/ping") + require.Nil(t, err) + defer resp.Body.Close() + assert.Equal(t, 200, resp.StatusCode) + body, err := ioutil.ReadAll(resp.Body) + assert.Nil(t, err) + assert.Equal(t, "pong", string(body)) + + srv.Shutdown() +} + +func TestRest_RunAutocertModeHTTPOnly(t *testing.T) { + srv := Rest{ + Authenticator: auth.Authenticator{}, + AvatarProxy: &proxy.Avatar{ + Store: avatar.NewLocalFS("/tmp", 300), + RoutePath: "/api/v1/avatar", + }, + ImageProxy: &proxy.Image{}, + SSLConfig: SSLConfig{ + SSLMode: Auto, + Port: 8443, + }, + RemarkURL: "https://localhost:8443", + } + + go func() { + // can't check https server locally, just only http server + srv.Run(8080) + }() + + time.Sleep(100 * time.Millisecond) // let server start + + client := http.Client{ + // prevent http redirect + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + }, + } + + resp, err := client.Get("http://localhost:8080/blah?param=1") + require.Nil(t, err) + defer resp.Body.Close() + assert.Equal(t, 307, resp.StatusCode) + assert.Equal(t, "https://localhost:8443/blah?param=1", resp.Header.Get("Location")) + + srv.Shutdown() +} + func prep(t *testing.T) (srv *Rest, ts *httptest.Server) { b, err := engine.NewBoltDB(bolt.Options{}, engine.BoltSite{FileName: testDb, SiteID: "radio-t"}) require.Nil(t, err)