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
b717e6f2
Commit
b717e6f2
authored
Jun 09, 2015
by
Tw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add unit test for errors middleware
Signed-off-by:
Tw
<
tw19881113@gmail.com
>
parent
47415937
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
124 additions
and
0 deletions
+124
-0
middleware/errors/errors_test.go
middleware/errors/errors_test.go
+124
-0
No files found.
middleware/errors/errors_test.go
0 → 100644
View file @
b717e6f2
package
errors
import
(
"bytes"
"errors"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/mholt/caddy/middleware"
)
func
TestErrors
(
t
*
testing
.
T
)
{
// create a temporary page
path
:=
filepath
.
Join
(
os
.
TempDir
(),
"errors_test.html"
)
f
,
err
:=
os
.
Create
(
path
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
os
.
Remove
(
path
)
const
content
=
"This is a error page"
_
,
err
=
f
.
WriteString
(
content
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
f
.
Close
()
buf
:=
bytes
.
Buffer
{}
em
:=
ErrorHandler
{
ErrorPages
:
make
(
map
[
int
]
string
),
Log
:
log
.
New
(
&
buf
,
""
,
0
),
}
em
.
ErrorPages
[
http
.
StatusNotFound
]
=
path
em
.
ErrorPages
[
http
.
StatusForbidden
]
=
"not_exist_file"
_
,
notExistErr
:=
os
.
Open
(
"not_exist_file"
)
testErr
:=
errors
.
New
(
"test error"
)
tests
:=
[]
struct
{
next
middleware
.
Handler
expectedCode
int
expectedBody
string
expectedLog
string
expectedErr
error
}{
{
next
:
genErrorHandler
(
http
.
StatusOK
,
nil
,
"normal"
),
expectedCode
:
http
.
StatusOK
,
expectedBody
:
"normal"
,
expectedLog
:
""
,
expectedErr
:
nil
,
},
{
next
:
genErrorHandler
(
http
.
StatusMovedPermanently
,
testErr
,
""
),
expectedCode
:
http
.
StatusMovedPermanently
,
expectedBody
:
""
,
expectedLog
:
fmt
.
Sprintf
(
"[ERROR %d %s] %v
\n
"
,
http
.
StatusMovedPermanently
,
"/"
,
testErr
),
expectedErr
:
testErr
,
},
{
next
:
genErrorHandler
(
http
.
StatusBadRequest
,
nil
,
""
),
expectedCode
:
0
,
expectedBody
:
fmt
.
Sprintf
(
"%d %s
\n
"
,
http
.
StatusBadRequest
,
http
.
StatusText
(
http
.
StatusBadRequest
)),
expectedLog
:
""
,
expectedErr
:
nil
,
},
{
next
:
genErrorHandler
(
http
.
StatusNotFound
,
nil
,
""
),
expectedCode
:
0
,
expectedBody
:
content
,
expectedLog
:
""
,
expectedErr
:
nil
,
},
{
next
:
genErrorHandler
(
http
.
StatusForbidden
,
nil
,
""
),
expectedCode
:
0
,
expectedBody
:
fmt
.
Sprintf
(
"%d %s
\n
"
,
http
.
StatusForbidden
,
http
.
StatusText
(
http
.
StatusForbidden
)),
expectedLog
:
fmt
.
Sprintf
(
"HTTP %d could not load error page %s: %v
\n
"
,
http
.
StatusForbidden
,
"not_exist_file"
,
notExistErr
),
expectedErr
:
nil
,
},
}
req
,
err
:=
http
.
NewRequest
(
"GET"
,
"/"
,
nil
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
for
i
,
test
:=
range
tests
{
em
.
Next
=
test
.
next
buf
.
Reset
()
rec
:=
httptest
.
NewRecorder
()
code
,
err
:=
em
.
ServeHTTP
(
rec
,
req
)
if
err
!=
test
.
expectedErr
{
t
.
Errorf
(
"Test %d: Expected error %v, but got %v"
,
i
,
test
.
expectedErr
,
err
)
}
if
code
!=
test
.
expectedCode
{
t
.
Errorf
(
"Test %d: Expected status code %d, but got %d"
,
i
,
test
.
expectedCode
,
code
)
}
if
body
:=
rec
.
Body
.
String
();
body
!=
test
.
expectedBody
{
t
.
Errorf
(
"Test %d: Expected body %q, but got %q"
,
i
,
test
.
expectedBody
,
body
)
}
if
log
:=
buf
.
String
();
log
!=
test
.
expectedLog
{
t
.
Errorf
(
"Test %d: Expected log %q, but got %q"
,
i
,
test
.
expectedLog
,
log
)
}
}
}
func
genErrorHandler
(
status
int
,
err
error
,
body
string
)
middleware
.
Handler
{
return
middleware
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
fmt
.
Fprint
(
w
,
body
)
return
status
,
err
})
}
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