Skip to content

Commit

Permalink
Merge pull request #529 from gotify/redirect
Browse files Browse the repository at this point in the history
Fix Redirect to https
  • Loading branch information
jmattheis committed Dec 3, 2022
2 parents fe8a80d + 522d7fb commit c8f78e8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Expand Up @@ -24,10 +24,11 @@ jobs:
key: ${{ runner.os }}-node_modules-${{ hashFiles('**/yarn.lock') }}
restore-keys: ${{ runner.os }}-node_modules-
- uses: actions/checkout@v2
- uses: golangci/golangci-lint-action@v2
- uses: golangci/golangci-lint-action@v3
with:
version: v1.45
args: --timeout=5m
skip-cache: true
- run: go mod download
- run: make download-tools
- run: (cd ui && yarn)
Expand Down
7 changes: 6 additions & 1 deletion runner/runner.go
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"net/http"
"strconv"
"strings"
"time"

"github.com/gotify/server/v2/config"
Expand Down Expand Up @@ -75,7 +76,11 @@ func redirectToHTTPS(port string) http.HandlerFunc {
func changePort(hostPort, port string) string {
host, _, err := net.SplitHostPort(hostPort)
if err != nil {
return hostPort
// There is no exported error.
if !strings.Contains(err.Error(), "missing port") {
return hostPort
}
host = hostPort
}
return net.JoinHostPort(host, port)
}
35 changes: 35 additions & 0 deletions runner/runner_test.go
@@ -0,0 +1,35 @@
package runner

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRedirect(t *testing.T) {
cases := []struct {
Request string
TLS int
Expect string
}{
{Request: "http://gotify.net/meow", TLS: 443, Expect: "https://gotify.net:443/meow"},
{Request: "http://gotify.net:8080/meow", TLS: 443, Expect: "https://gotify.net:443/meow"},
{Request: "http://gotify.net:8080/meow", TLS: 8443, Expect: "https://gotify.net:8443/meow"},
}

for _, testCase := range cases {
name := fmt.Sprintf("%s -- %d -> %s", testCase.Request, testCase.TLS, testCase.Expect)
t.Run(name, func(t *testing.T) {
req := httptest.NewRequest("GET", testCase.Request, nil)
rec := httptest.NewRecorder()

redirectToHTTPS(fmt.Sprint(testCase.TLS)).ServeHTTP(rec, req)

assert.Equal(t, http.StatusFound, rec.Result().StatusCode)
assert.Equal(t, testCase.Expect, rec.Header().Get("location"))
})
}
}

0 comments on commit c8f78e8

Please sign in to comment.