2017-03-22 18:09:21 +01:00
|
|
|
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:45:30 +02:00
|
|
|
_ = Suite(&suiteRedirectWithForceHTTPS{})
|
|
|
|
_ = Suite(&suiteRedirectWithoutForceHTTPS{})
|
2017-03-22 18:09:21 +01:00
|
|
|
)
|
|
|
|
|
2021-08-19 22:45:30 +02:00
|
|
|
type suiteRedirectWithForceHTTPS struct {
|
2017-03-22 18:09:21 +01:00
|
|
|
handler http.HandlerFunc
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:45:30 +02:00
|
|
|
func (s *suiteRedirectWithForceHTTPS) SetUpTest(c *C) {
|
|
|
|
srvr, err := New(ForceHTTPS())
|
2017-03-22 18:09:21 +01:00
|
|
|
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:45:30 +02:00
|
|
|
func (s *suiteRedirectWithForceHTTPS) TestHTTPs(c *C) {
|
2017-03-22 18:09:21 +01:00
|
|
|
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:45:30 +02:00
|
|
|
func (s *suiteRedirectWithForceHTTPS) TestOnion(c *C) {
|
2017-03-22 18:09:21 +01:00
|
|
|
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:45:30 +02:00
|
|
|
func (s *suiteRedirectWithForceHTTPS) TestXForwardedFor(c *C) {
|
2017-03-22 18:09:21 +01:00
|
|
|
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:45:30 +02:00
|
|
|
func (s *suiteRedirectWithForceHTTPS) TestHTTP(c *C) {
|
2017-03-22 18:09:21 +01:00
|
|
|
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:45:30 +02:00
|
|
|
type suiteRedirectWithoutForceHTTPS struct {
|
2017-03-22 18:09:21 +01:00
|
|
|
handler http.HandlerFunc
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:45:30 +02:00
|
|
|
func (s *suiteRedirectWithoutForceHTTPS) SetUpTest(c *C) {
|
2017-03-22 18:09:21 +01:00
|
|
|
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:45:30 +02:00
|
|
|
func (s *suiteRedirectWithoutForceHTTPS) TestHTTP(c *C) {
|
2017-03-22 18:09:21 +01:00
|
|
|
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:45:30 +02:00
|
|
|
func (s *suiteRedirectWithoutForceHTTPS) TestHTTPs(c *C) {
|
2017-03-22 18:09:21 +01:00
|
|
|
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)
|
|
|
|
}
|