0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

add tests for context.Cookie() and context.IP()

This commit is contained in:
makpoc 2015-10-15 18:26:13 +03:00
parent f7e3ed13f9
commit 3440f5cfbe

View file

@ -1,6 +1,7 @@
package middleware
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
@ -8,10 +9,14 @@ import (
"path/filepath"
"strings"
"testing"
"time"
)
func TestInclude(t *testing.T) {
context := initTempContext()
context, err := initTestContext()
if err != nil {
t.Fatalf("Failed to prepare test context")
}
inputFilename := "test_file"
absInFilePath := filepath.Join(fmt.Sprintf("%s", context.Root), inputFilename)
@ -52,7 +57,7 @@ func TestInclude(t *testing.T) {
}
for i, test := range tests {
testPrefix := fmt.Sprintf("Test [%d]: ", i)
testPrefix := getTestPrefix(i)
// WriteFile truncates the contentt
err := ioutil.WriteFile(absInFilePath, []byte(test.fileContent), os.ModePerm)
@ -81,19 +86,144 @@ func TestInclude(t *testing.T) {
}
func TestIncludeNotExisting(t *testing.T) {
context := initTempContext()
context, err := initTestContext()
if err != nil {
t.Fatalf("Failed to prepare test context")
}
_, err := context.Include("not_existing")
_, err = context.Include("not_existing")
if err == nil {
t.Errorf("Expected error but found nil!")
}
}
func initTempContext() Context {
func TestCookie(t *testing.T) {
tests := []struct {
cookie *http.Cookie
cookieName string
expectedValue string
}{
// Test 0 - happy path
{
cookie: &http.Cookie{Name: "cookieName", Value: "cookieValue"},
cookieName: "cookieName",
expectedValue: "cookieValue",
},
// Test 1 - try to get a non-existing cookie
{
cookie: &http.Cookie{Name: "cookieName", Value: "cookieValue"},
cookieName: "notExisting",
expectedValue: "",
},
// Test 2 - partial name match
{
cookie: &http.Cookie{Name: "cookie", Value: "cookieValue"},
cookieName: "cook",
expectedValue: "",
},
// Test 3 - cookie with optional fields
{
cookie: &http.Cookie{Name: "cookie", Value: "cookieValue", Path: "/path", Domain: "https://caddy.com", Expires: (time.Now().Add(10 * time.Minute)), MaxAge: 120},
cookieName: "cookie",
expectedValue: "cookieValue",
},
}
for i, test := range tests {
testPrefix := getTestPrefix(i)
// reinitialize the context for each test
context, err := initTestContext()
if err != nil {
t.Fatalf("Failed to prepare test context")
}
context.Req.AddCookie(test.cookie)
actualCookieVal := context.Cookie(test.cookieName)
if actualCookieVal != test.expectedValue {
t.Errorf(testPrefix+"Expected cookie value [%s] but found [%s] for cookie with name %s", test.expectedValue, actualCookieVal, test.cookieName)
}
}
}
func TestCookieMultipleCookies(t *testing.T) {
context, err := initTestContext()
if err != nil {
t.Fatalf("Failed to prepare test context")
}
cookieNameBase, cookieValueBase := "cookieName", "cookieValue"
// make sure that there's no state and multiple requests for different cookies return the correct result
for i := 0; i < 10; i++ {
context.Req.AddCookie(&http.Cookie{Name: fmt.Sprintf("%s%d", cookieNameBase, i), Value: fmt.Sprintf("%s%d", cookieValueBase, i)})
}
for i := 0; i < 10; i++ {
expectedCookieVal := fmt.Sprintf("%s%d", cookieValueBase, i)
actualCookieVal := context.Cookie(fmt.Sprintf("%s%d", cookieNameBase, i))
if actualCookieVal != expectedCookieVal {
t.Fatalf("Expected cookie value %s, found %s", expectedCookieVal, actualCookieVal)
}
}
}
func TestIP(t *testing.T) {
context, err := initTestContext()
if err != nil {
t.Fatalf("Failed to prepare test context")
}
tests := []struct {
inputRemoteAddr string
expectedIP string
}{
// Test 0 - ipv4 with port
{"1.1.1.1:1111", "1.1.1.1"},
// Test 1 - ipv4 without port
{"1.1.1.1", "1.1.1.1"},
// Test 2 - ipv6 with port
{"[::1]:11", "::1"},
// Test 3 - ipv6 without port and brackets
{"[2001:db8:a0b:12f0::1]", "[2001:db8:a0b:12f0::1]"},
// Test 4 - ipv6 with zone and port
{`[fe80:1::3%eth0]:44`, `fe80:1::3%eth0`},
// Test 5 - ipv6 without port with brackets
// {"[:fe:2]", ":fe:2"}, // TODO - failing (error in SplitHostPort) returns the host with brackets
// Test 6 - invalid address
// {":::::::::::::", ""}, // TODO - failing (error in SplitHostPort) returns the invalid address
// Test 7 - invalid address
// {"[::1][]", ""}, // TODO - failing (error in SplitHostPort) returns the invalid address
}
for i, test := range tests {
testPrefix := getTestPrefix(i)
context.Req.RemoteAddr = test.inputRemoteAddr
actualIP := context.IP()
if actualIP != test.expectedIP {
t.Errorf(testPrefix+"Expected IP %s, found %s", test.expectedIP, actualIP)
}
}
}
func initTestContext() (Context, error) {
rootDir := getTestFilesFolder()
return Context{Root: http.Dir(rootDir)}
body := bytes.NewBufferString("request body")
request, err := http.NewRequest("GET", "https://caddy.com", body)
if err != nil {
return Context{}, err
}
return Context{Root: http.Dir(rootDir), Req: request}, nil
}
func getTestFilesFolder() string {
return os.TempDir()
}
func getTestPrefix(testN int) string {
return fmt.Sprintf("Test [%d]: ", testN)
}