mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
FastCGI: some simple tests for buildEnv
More tests are needed for the other environmental variables. These tests were specifically made for testing of IP addresses.
This commit is contained in:
parent
d8be787f39
commit
ac197f1694
1 changed files with 64 additions and 0 deletions
|
@ -1,6 +1,8 @@
|
||||||
package fastcgi
|
package fastcgi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -29,3 +31,65 @@ func TestRuleParseAddress(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BuildEnvSingle(r *http.Request, rule Rule, fpath string, envExpected map[string]string, t *testing.T) {
|
||||||
|
|
||||||
|
h := Handler{}
|
||||||
|
|
||||||
|
env, err := h.buildEnv(r, rule, fpath)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Unexpected error:", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range envExpected {
|
||||||
|
if env[k] != v {
|
||||||
|
t.Errorf("Unexpected %v. Got %v, expected %v", k, env[k], v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildEnv(t *testing.T) {
|
||||||
|
|
||||||
|
rule := Rule{}
|
||||||
|
url, err := url.Parse("http://localhost:2015/fgci_test.php?test=blabla")
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Unexpected error:", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
r := http.Request{
|
||||||
|
Method: "GET",
|
||||||
|
URL: url,
|
||||||
|
Proto: "HTTP/1.1",
|
||||||
|
ProtoMajor: 1,
|
||||||
|
ProtoMinor: 1,
|
||||||
|
Host: "localhost:2015",
|
||||||
|
RemoteAddr: "[2b02:1810:4f2d:9400:70ab:f822:be8a:9093]:51688",
|
||||||
|
RequestURI: "/fgci_test.php",
|
||||||
|
}
|
||||||
|
|
||||||
|
fpath := "/fgci_test.php"
|
||||||
|
|
||||||
|
var envExpected = map[string]string{
|
||||||
|
"REMOTE_ADDR": "[2b02:1810:4f2d:9400:70ab:f822:be8a:9093]",
|
||||||
|
"REMOTE_PORT": "51688",
|
||||||
|
"SERVER_PROTOCOL": "HTTP/1.1",
|
||||||
|
"QUERY_STRING": "test=blabla",
|
||||||
|
"REQUEST_METHOD": "GET",
|
||||||
|
"HTTP_HOST": "localhost:2015",
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. Test for full canonical IPv6 address
|
||||||
|
BuildEnvSingle(&r, rule, fpath, envExpected, t)
|
||||||
|
|
||||||
|
// 2. Test for shorthand notation of IPv6 address
|
||||||
|
r.RemoteAddr = "[::1]:51688"
|
||||||
|
envExpected["REMOTE_ADDR"] = "[::1]"
|
||||||
|
BuildEnvSingle(&r, rule, fpath, envExpected, t)
|
||||||
|
|
||||||
|
// 3. Test for IPv4 address
|
||||||
|
r.RemoteAddr = "192.168.0.10:51688"
|
||||||
|
envExpected["REMOTE_ADDR"] = "192.168.0.10"
|
||||||
|
BuildEnvSingle(&r, rule, fpath, envExpected, t)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue