-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_redirector_test.go
53 lines (43 loc) · 2.13 KB
/
http_redirector_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"github.com/sirupsen/logrus"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func responseFor(c *Config, url string) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
r, err := http.NewRequest("TEST", url, nil)
if err != nil {
logrus.SetOutput(os.Stderr)
logrus.Fatal(err)
}
NewHTTPRedirector(c).ServeHTTP(w, r)
return w
}
func shouldRedirect(t *testing.T, redirect string, status int, url string, newURL string) {
c := &Config{}
c.Redirect = redirect
c.Status = status
w := responseFor(c, url)
if w.Code != status {
t.Errorf("%v yields invalid response status", url)
}
if w.HeaderMap["Location"][0] != newURL {
t.Errorf("%v should redirect to %v, redirected to %v", url, newURL, w.HeaderMap["Location"][0])
}
}
func TestRedirect(t *testing.T) {
// logrus.SetOutput(ioutil.Discard)
shouldRedirect(t, "https://REQUEST_HOST:443", http.StatusMovedPermanently, "http://foo.com:8080/asd/fdfd?asd=123", "https://foo.com/asd/fdfd?asd=123")
shouldRedirect(t, ":443", http.StatusMovedPermanently, "http://foo.com/asd/fdfd?asd=123", "https://foo.com/asd/fdfd?asd=123")
shouldRedirect(t, ":80", http.StatusMovedPermanently, "https://foo.com/asd/fdfd?asd=123", "http://foo.com/asd/fdfd?asd=123")
shouldRedirect(t, "http://:80", http.StatusMovedPermanently, "https://foo.com/asd/fdfd?asd=123", "http://foo.com/asd/fdfd?asd=123")
shouldRedirect(t, "https://:443", http.StatusMovedPermanently, "http://foo.com/asd/fdfd?asd=123", "https://foo.com/asd/fdfd?asd=123")
shouldRedirect(t, "example.com:8080", http.StatusMovedPermanently, "http://foo.com/asd/fdfd?asd=123", "http://example.com:8080/asd/fdfd?asd=123")
shouldRedirect(t, "example.com:8080/test", http.StatusMovedPermanently, "http://foo.com/asd/fdfd?asd=123", "http://example.com:8080/asd/fdfd?asd=123")
shouldRedirect(t, "example.com/test", http.StatusMovedPermanently, "http://foo.com/asd/fdfd?asd=123", "http://example.com/asd/fdfd?asd=123")
shouldRedirect(t, "http://:81", http.StatusFound, "http://127.0.0.1?asd=123", "http://127.0.0.1:81?asd=123")
shouldRedirect(t, "192.168.0.1:8000", http.StatusFound, "http://127.0.0.1", "http://192.168.0.1:8000")
}