2019-04-20 22:15:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetRedirectGet(t *testing.T) {
|
|
|
|
// Constructed URL to match a nginx redirect:
|
|
|
|
// `return 302 https://login.luzifer.io/login?go=$scheme://$http_host$request_uri;`
|
|
|
|
testURL := "https://example.com/login?go=https://example.com/inner?foo=bar&bar=foo"
|
|
|
|
expectURL := "https://example.com/inner?bar=foo&foo=bar"
|
|
|
|
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, testURL, nil)
|
|
|
|
|
2019-04-21 01:22:45 +00:00
|
|
|
rURL, err := getRedirectURL(req, "")
|
2019-04-20 22:15:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("getRedirectURL caused an error in GET: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expectURL != rURL {
|
|
|
|
t.Errorf("Result did not match expected URL: %q != %q", rURL, expectURL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:22:45 +00:00
|
|
|
func TestGetRedirectFallback(t *testing.T) {
|
2019-04-20 22:15:36 +00:00
|
|
|
testURL := "https://example.com/login"
|
2019-04-21 01:22:45 +00:00
|
|
|
expectURL := "https://example.com/default"
|
2019-04-20 22:15:36 +00:00
|
|
|
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, testURL, nil)
|
|
|
|
|
2019-04-21 01:22:45 +00:00
|
|
|
rURL, err := getRedirectURL(req, expectURL)
|
2019-04-20 22:15:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("getRedirectURL caused an error in GET: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expectURL != rURL {
|
|
|
|
t.Errorf("Result did not match expected URL: %q != %q", rURL, expectURL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRedirectPost(t *testing.T) {
|
|
|
|
testURL := "https://example.com/login"
|
|
|
|
expectURL := "https://example.com/inner?foo=bar"
|
|
|
|
|
|
|
|
body := url.Values{
|
|
|
|
"go": []string{expectURL},
|
|
|
|
"other": []string{"param"},
|
|
|
|
}
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, testURL, nil)
|
|
|
|
req.Form = body // Force-set the form values to emulate parsed form
|
|
|
|
|
2019-04-21 01:22:45 +00:00
|
|
|
rURL, err := getRedirectURL(req, "")
|
2019-04-20 22:15:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("getRedirectURL caused an error in POST: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expectURL != rURL {
|
|
|
|
t.Errorf("Result did not match expected URL: %q != %q", rURL, expectURL)
|
|
|
|
}
|
|
|
|
}
|