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
nexedi
caddy
Commits
bd4d9c6f
Commit
bd4d9c6f
authored
9 years ago
by
makpoc
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add tests for context.Header,IP,URL,Host,Port,Method,PathMatches
parent
3440f5cf
master
nxd-v0.11.0
nxd-v0.11.1
nxd-v0.11.5
nxd-v1.0.3
v1.0.0
v1.0.0-beta2
v1.0.0-beta1
v0.11.5
v0.11.4
v0.11.3
v0.11.2
v0.11.1
v0.11.1-4-g527de1864b6c33dac07c50694e07c50cb0abc3eb
v0.11.1-3-g5490ff205fed11f0972fde025934855d3a719d77
v0.11.0
v0.10.14
v0.10.13
v0.10.12
v0.10.11
v0.10.10
v0.10.9
v0.10.8
v0.10.7
v0.10.6
v0.10.5
v0.10.4
v0.10.3
v0.10.2
v0.10.1
v0.10.0
v0.9.5
v0.9.4
v0.9.3
v0.9.2
v0.9.1
v0.9.0
v0.9-beta.2
v0.9-beta.1
v0.8.3
v0.8.2
v0.8.1
v0.8.0
v0.8-beta.4
v0.8-beta.3
v0.8-beta.2
v0.8-beta.1
nxd-v1.0.3-1-g2c11cedc
nxd-v1.0.3-1-03fba31bf
nxd-v0.11.5-4-g9d3151db
nxd-v0.11.1-5-gdd393ce3a67e6a773be87185528a00f2e0a9eb26
nxd-v0.11.1-4-g527de1864b6c33dac07c50694e07c50cb0abc3eb
nxd-v0.11.1-3-g5490ff205fed11f0972fde025934855d3a719d77
nxd-v0.11.0-3-g12438f6cff8c15f307631151eb064cec579b7605
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
205 additions
and
21 deletions
+205
-21
middleware/context_test.go
middleware/context_test.go
+205
-21
No files found.
middleware/context_test.go
View file @
bd4d9c6f
...
...
@@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
...
...
@@ -13,10 +14,7 @@ import (
)
func
TestInclude
(
t
*
testing
.
T
)
{
context
,
err
:=
initTestContext
()
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to prepare test context"
)
}
context
:=
getContextOrFail
(
t
)
inputFilename
:=
"test_file"
absInFilePath
:=
filepath
.
Join
(
fmt
.
Sprintf
(
"%s"
,
context
.
Root
),
inputFilename
)
...
...
@@ -86,12 +84,9 @@ func TestInclude(t *testing.T) {
}
func
TestIncludeNotExisting
(
t
*
testing
.
T
)
{
context
,
err
:=
initTestContext
()
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to prepare test context"
)
}
context
:=
getContextOrFail
(
t
)
_
,
err
=
context
.
Include
(
"not_existing"
)
_
,
err
:
=
context
.
Include
(
"not_existing"
)
if
err
==
nil
{
t
.
Errorf
(
"Expected error but found nil!"
)
}
...
...
@@ -134,10 +129,8 @@ func TestCookie(t *testing.T) {
testPrefix
:=
getTestPrefix
(
i
)
// reinitialize the context for each test
context
,
err
:=
initTestContext
()
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to prepare test context"
)
}
context
:=
getContextOrFail
(
t
)
context
.
Req
.
AddCookie
(
test
.
cookie
)
actualCookieVal
:=
context
.
Cookie
(
test
.
cookieName
)
...
...
@@ -149,10 +142,7 @@ func TestCookie(t *testing.T) {
}
func
TestCookieMultipleCookies
(
t
*
testing
.
T
)
{
context
,
err
:=
initTestContext
()
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to prepare test context"
)
}
context
:=
getContextOrFail
(
t
)
cookieNameBase
,
cookieValueBase
:=
"cookieName"
,
"cookieValue"
...
...
@@ -170,12 +160,26 @@ func TestCookieMultipleCookies(t *testing.T) {
}
}
func
TestIP
(
t
*
testing
.
T
)
{
context
,
err
:=
initTestContext
()
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to prepare test context"
)
func
TestHeader
(
t
*
testing
.
T
)
{
context
:=
getContextOrFail
(
t
)
headerKey
,
headerVal
:=
"Header1"
,
"HeaderVal1"
context
.
Req
.
Header
.
Add
(
headerKey
,
headerVal
)
actualHeaderVal
:=
context
.
Header
(
headerKey
)
if
actualHeaderVal
!=
headerVal
{
t
.
Errorf
(
"Expected header %s, found %s"
,
headerVal
,
actualHeaderVal
)
}
missingHeaderVal
:=
context
.
Header
(
"not-existing"
)
if
missingHeaderVal
!=
""
{
t
.
Errorf
(
"Expected empty header value, found %s"
,
missingHeaderVal
)
}
}
func
TestIP
(
t
*
testing
.
T
)
{
context
:=
getContextOrFail
(
t
)
tests
:=
[]
struct
{
inputRemoteAddr
string
expectedIP
string
...
...
@@ -210,6 +214,178 @@ func TestIP(t *testing.T) {
}
}
func
TestURL
(
t
*
testing
.
T
)
{
context
:=
getContextOrFail
(
t
)
inputURL
:=
"http://localhost"
context
.
Req
.
RequestURI
=
inputURL
if
inputURL
!=
context
.
URI
()
{
t
.
Errorf
(
"Expected url %s, found %s"
,
inputURL
,
context
.
URI
())
}
}
func
TestHost
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
input
string
expectedHost
string
shouldErr
bool
}{
{
input
:
"localhost:123"
,
expectedHost
:
"localhost"
,
shouldErr
:
false
,
},
{
input
:
"localhost"
,
expectedHost
:
""
,
shouldErr
:
true
,
// missing port in address
},
}
for
_
,
test
:=
range
tests
{
testHostOrPort
(
t
,
true
,
test
.
input
,
test
.
expectedHost
,
test
.
shouldErr
)
}
}
func
TestPort
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
input
string
expectedPort
string
shouldErr
bool
}{
{
input
:
"localhost:123"
,
expectedPort
:
"123"
,
shouldErr
:
false
,
},
{
input
:
"localhost"
,
expectedPort
:
""
,
shouldErr
:
true
,
// missing port in address
},
}
for
_
,
test
:=
range
tests
{
testHostOrPort
(
t
,
false
,
test
.
input
,
test
.
expectedPort
,
test
.
shouldErr
)
}
}
func
testHostOrPort
(
t
*
testing
.
T
,
isTestingHost
bool
,
input
,
expectedResult
string
,
shouldErr
bool
)
{
context
:=
getContextOrFail
(
t
)
context
.
Req
.
Host
=
input
var
actualResult
,
testedObject
string
var
err
error
if
isTestingHost
{
actualResult
,
err
=
context
.
Host
()
testedObject
=
"host"
}
else
{
actualResult
,
err
=
context
.
Port
()
testedObject
=
"port"
}
if
shouldErr
&&
err
==
nil
{
t
.
Errorf
(
"Expected error, found nil!"
)
return
}
if
!
shouldErr
&&
err
!=
nil
{
t
.
Errorf
(
"Expected no error, found %s"
,
err
)
return
}
if
actualResult
!=
expectedResult
{
t
.
Errorf
(
"Expected %s %s, found %s"
,
testedObject
,
expectedResult
,
actualResult
)
}
}
func
TestMethod
(
t
*
testing
.
T
)
{
context
:=
getContextOrFail
(
t
)
method
:=
"POST"
context
.
Req
.
Method
=
method
if
method
!=
context
.
Method
()
{
t
.
Errorf
(
"Expected method %s, found %s"
,
method
,
context
.
Method
())
}
}
func
TestPathMatches
(
t
*
testing
.
T
)
{
context
:=
getContextOrFail
(
t
)
tests
:=
[]
struct
{
urlStr
string
pattern
string
shouldMatch
bool
}{
// Test 0
{
urlStr
:
"http://caddy.com/"
,
pattern
:
""
,
shouldMatch
:
true
,
},
// Test 1
{
urlStr
:
"http://caddy.com"
,
pattern
:
""
,
shouldMatch
:
true
,
},
// Test 1
{
urlStr
:
"http://caddy.com/"
,
pattern
:
"/"
,
shouldMatch
:
true
,
},
// Test 3
{
urlStr
:
"http://caddy.com/?param=val"
,
pattern
:
"/"
,
shouldMatch
:
true
,
},
// Test 4
{
urlStr
:
"http://caddy.com/dir1/dir2"
,
pattern
:
"/dir2"
,
shouldMatch
:
false
,
},
// Test 5
{
urlStr
:
"http://caddy.com/dir1/dir2"
,
pattern
:
"/dir1"
,
shouldMatch
:
true
,
},
// Test 6
{
urlStr
:
"http://caddy.com:444/dir1/dir2"
,
pattern
:
"/dir1"
,
shouldMatch
:
true
,
},
// Test 7
{
urlStr
:
"http://caddy.com/dir1/dir2"
,
pattern
:
"*/dir2"
,
shouldMatch
:
false
,
},
}
for
i
,
test
:=
range
tests
{
testPrefix
:=
getTestPrefix
(
i
)
var
err
error
context
.
Req
.
URL
,
err
=
url
.
Parse
(
test
.
urlStr
)
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to prepare test URL from string %s! Error was: %s"
,
test
.
urlStr
,
err
)
}
matches
:=
context
.
PathMatches
(
test
.
pattern
)
if
matches
!=
test
.
shouldMatch
{
t
.
Errorf
(
testPrefix
+
"Expected and actual result differ: expected to match [%t], actual matches [%t]"
,
test
.
shouldMatch
,
matches
)
}
}
}
func
initTestContext
()
(
Context
,
error
)
{
rootDir
:=
getTestFilesFolder
()
body
:=
bytes
.
NewBufferString
(
"request body"
)
...
...
@@ -220,6 +396,14 @@ func initTestContext() (Context, error) {
return
Context
{
Root
:
http
.
Dir
(
rootDir
),
Req
:
request
},
nil
}
func
getContextOrFail
(
t
*
testing
.
T
)
Context
{
context
,
err
:=
initTestContext
()
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to prepare test context"
)
}
return
context
}
func
getTestFilesFolder
()
string
{
return
os
.
TempDir
()
}
...
...
This diff is collapsed.
Click to expand it.
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