transfer.sh/server/handlers_test.go

111 lines
2.5 KiB
Go
Raw Normal View History

package server
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
. "gopkg.in/check.v1"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
var (
2021-08-19 22:21:31 +02:00
_ = Suite(&suiteRedirectWithForceHTTPS{})
_ = Suite(&suiteRedirectWithoutForceHTTPS{})
)
2021-08-19 22:21:31 +02:00
type suiteRedirectWithForceHTTPS struct {
handler http.HandlerFunc
}
2021-08-19 22:21:31 +02:00
func (s *suiteRedirectWithForceHTTPS) SetUpTest(c *C) {
srvr, err := New(ForceHTTPS())
c.Assert(err, IsNil)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, client")
})
s.handler = srvr.RedirectHandler(handler)
}
2021-08-19 22:21:31 +02:00
func (s *suiteRedirectWithForceHTTPS) TestHTTPs(c *C) {
req := httptest.NewRequest("GET", "https://test/test", nil)
w := httptest.NewRecorder()
s.handler(w, req)
resp := w.Result()
c.Assert(resp.StatusCode, Equals, http.StatusOK)
}
2021-08-19 22:21:31 +02:00
func (s *suiteRedirectWithForceHTTPS) TestOnion(c *C) {
req := httptest.NewRequest("GET", "http://test.onion/test", nil)
w := httptest.NewRecorder()
s.handler(w, req)
resp := w.Result()
c.Assert(resp.StatusCode, Equals, http.StatusOK)
}
2021-08-19 22:21:31 +02:00
func (s *suiteRedirectWithForceHTTPS) TestXForwardedFor(c *C) {
req := httptest.NewRequest("GET", "http://127.0.0.1/test", nil)
req.Header.Set("X-Forwarded-Proto", "https")
w := httptest.NewRecorder()
s.handler(w, req)
resp := w.Result()
c.Assert(resp.StatusCode, Equals, http.StatusOK)
}
2021-08-19 22:21:31 +02:00
func (s *suiteRedirectWithForceHTTPS) TestHTTP(c *C) {
req := httptest.NewRequest("GET", "http://127.0.0.1/test", nil)
w := httptest.NewRecorder()
s.handler(w, req)
resp := w.Result()
c.Assert(resp.StatusCode, Equals, http.StatusPermanentRedirect)
c.Assert(resp.Header.Get("Location"), Equals, "https://127.0.0.1/test")
}
2021-08-19 22:21:31 +02:00
type suiteRedirectWithoutForceHTTPS struct {
handler http.HandlerFunc
}
2021-08-19 22:21:31 +02:00
func (s *suiteRedirectWithoutForceHTTPS) SetUpTest(c *C) {
srvr, err := New()
c.Assert(err, IsNil)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, client")
})
s.handler = srvr.RedirectHandler(handler)
}
2021-08-19 22:21:31 +02:00
func (s *suiteRedirectWithoutForceHTTPS) TestHTTP(c *C) {
req := httptest.NewRequest("GET", "http://127.0.0.1/test", nil)
w := httptest.NewRecorder()
s.handler(w, req)
resp := w.Result()
c.Assert(resp.StatusCode, Equals, http.StatusOK)
}
2021-08-19 22:21:31 +02:00
func (s *suiteRedirectWithoutForceHTTPS) TestHTTPs(c *C) {
req := httptest.NewRequest("GET", "https://127.0.0.1/test", nil)
w := httptest.NewRecorder()
s.handler(w, req)
resp := w.Result()
c.Assert(resp.StatusCode, Equals, http.StatusOK)
}