Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Łukasz Nowak
caddy
Commits
3440f5cf
Commit
3440f5cf
authored
Oct 15, 2015
by
makpoc
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add tests for context.Cookie() and context.IP()
parent
f7e3ed13
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
136 additions
and
6 deletions
+136
-6
middleware/context_test.go
middleware/context_test.go
+136
-6
No files found.
middleware/context_test.go
View file @
3440f5cf
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
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment