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
1cafb1ee
Commit
1cafb1ee
authored
Aug 31, 2015
by
Matt Holt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #226 from Karthic-Hackintosh/master
browse: Initial test for json response in browse.go
parents
d79d2611
e3cea042
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
2 deletions
+88
-2
middleware/browse/browse_test.go
middleware/browse/browse_test.go
+88
-2
No files found.
middleware/browse/browse_test.go
View file @
1cafb1ee
package
browse
import
(
"encoding/json"
"github.com/mholt/caddy/middleware"
"net/http"
"net/http/httptest"
"net/url"
"os"
"sort"
"testing"
"text/template"
"time"
"github.com/mholt/caddy/middleware"
)
// "sort" package has "IsSorted" function, but no "IsReversed";
...
...
@@ -154,4 +156,88 @@ func TestBrowseTemplate(t *testing.T) {
if
respBody
!=
expectedBody
{
t
.
Fatalf
(
"Expected body: %v got: %v"
,
expectedBody
,
respBody
)
}
}
func
TestBrowseJson
(
t
*
testing
.
T
)
{
b
:=
Browse
{
Next
:
middleware
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
t
.
Fatalf
(
"Next shouldn't be called"
)
return
0
,
nil
}),
Root
:
"./testdata"
,
Configs
:
[]
Config
{
Config
{
PathScope
:
"/photos"
,
},
},
}
req
,
err
:=
http
.
NewRequest
(
"GET"
,
"/photos/"
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"Test: Could not create HTTP request: %v"
,
err
)
}
req
.
Header
.
Set
(
"Accept"
,
"application/json"
)
rec
:=
httptest
.
NewRecorder
()
b
.
ServeHTTP
(
rec
,
req
)
if
rec
.
Code
!=
http
.
StatusOK
{
t
.
Fatalf
(
"Wrong status, expected %d, got %d"
,
http
.
StatusOK
,
rec
.
Code
)
}
if
rec
.
HeaderMap
.
Get
(
"Content-Type"
)
!=
"application/json; charset=utf-8"
{
t
.
Fatalf
(
"Expected Content type to be application/json; charset=utf-8, but got %s "
,
rec
.
HeaderMap
.
Get
(
"Content-Type"
))
}
actualJsonResponseString
:=
rec
.
Body
.
String
()
//generating the listing to compare it with the response body
file
,
err
:=
os
.
Open
(
b
.
Root
+
req
.
URL
.
Path
)
if
err
!=
nil
{
if
os
.
IsPermission
(
err
)
{
t
.
Fatalf
(
"Os Permission Error"
)
}
}
defer
file
.
Close
()
files
,
err
:=
file
.
Readdir
(
-
1
)
if
err
!=
nil
{
t
.
Fatalf
(
"Unable to Read Contents of the directory"
)
}
var
fileinfos
[]
FileInfo
for
_
,
f
:=
range
files
{
name
:=
f
.
Name
()
if
f
.
IsDir
()
{
name
+=
"/"
}
url
:=
url
.
URL
{
Path
:
name
}
fileinfos
=
append
(
fileinfos
,
FileInfo
{
IsDir
:
f
.
IsDir
(),
Name
:
f
.
Name
(),
Size
:
f
.
Size
(),
URL
:
url
.
String
(),
ModTime
:
f
.
ModTime
(),
Mode
:
f
.
Mode
(),
})
}
listing
:=
Listing
{
Items
:
fileinfos
,
}
listing
.
Sort
=
"name"
listing
.
Order
=
"asc"
listing
.
applySort
()
marsh
,
err
:=
json
.
Marshal
(
listing
.
Items
)
if
err
!=
nil
{
t
.
Fatalf
(
"Unable to Marshal the listing "
)
}
expectedJsonString
:=
string
(
marsh
)
if
actualJsonResponseString
!=
expectedJsonString
{
t
.
Errorf
(
"Json response string doesnt match the expected Json response "
)
}
}
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