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
a0eca497
Commit
a0eca497
authored
9 years ago
by
Michael Schöbel
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #70 from mschoebel/internal_middleware_tests
Internal middleware: added tests
parents
42725365
e0173ec4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
64 additions
and
0 deletions
+64
-0
middleware/internal/internal_test.go
middleware/internal/internal_test.go
+64
-0
No files found.
middleware/internal/internal_test.go
0 → 100644
View file @
a0eca497
package
internal
import
(
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/mholt/caddy/middleware"
)
func
TestInternal
(
t
*
testing
.
T
)
{
im
:=
Internal
{
Next
:
middleware
.
HandlerFunc
(
internalTestHandlerFunc
),
Paths
:
[]
string
{
"/internal"
},
}
tests
:=
[]
struct
{
url
string
expectedCode
int
expectedBody
string
}{
{
"/internal"
,
http
.
StatusNotFound
,
""
},
{
"/public"
,
0
,
"/public"
},
{
"/public/internal"
,
0
,
"/public/internal"
},
{
"/redirect"
,
0
,
"/internal"
},
{
"/cycle"
,
http
.
StatusInternalServerError
,
""
},
}
for
i
,
test
:=
range
tests
{
req
,
err
:=
http
.
NewRequest
(
"GET"
,
test
.
url
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"Test %d: Could not create HTTP request: %v"
,
i
,
err
)
}
rec
:=
httptest
.
NewRecorder
()
code
,
err
:=
im
.
ServeHTTP
(
rec
,
req
)
if
code
!=
test
.
expectedCode
{
t
.
Errorf
(
"Test %d: Expected status code %d for %s, but got %d"
,
i
,
test
.
expectedCode
,
test
.
url
,
code
)
}
if
rec
.
Body
.
String
()
!=
test
.
expectedBody
{
t
.
Errorf
(
"Test %d: Expected body '%s' for %s, but got '%s'"
,
i
,
test
.
expectedBody
,
test
.
url
,
rec
.
Body
.
String
())
}
}
}
func
internalTestHandlerFunc
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
switch
r
.
URL
.
Path
{
case
"/redirect"
:
w
.
Header
()
.
Set
(
"X-Accel-Redirect"
,
"/internal"
)
case
"/cycle"
:
w
.
Header
()
.
Set
(
"X-Accel-Redirect"
,
"/cycle"
)
}
w
.
WriteHeader
(
http
.
StatusOK
)
fmt
.
Fprintf
(
w
,
r
.
URL
.
String
())
return
0
,
nil
}
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