From 730269743fc5a03df419afd0e187c109747d7fd3 Mon Sep 17 00:00:00 2001 From: Karthic Rao Date: Sat, 29 Aug 2015 08:04:01 +0530 Subject: [PATCH 1/2] Json response initial test for browse.go --- middleware/browse/browse_test.go | 93 +++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 2 deletions(-) diff --git a/middleware/browse/browse_test.go b/middleware/browse/browse_test.go index b0ff28db..13894a8d 100644 --- a/middleware/browse/browse_test.go +++ b/middleware/browse/browse_test.go @@ -1,14 +1,17 @@ package browse import ( + //"bytes" + "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 +157,90 @@ 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() + //t.Logf("json response body: %s\n", respBody) + //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() + + //var buf bytes.Buffer + marsh, err := json.Marshal(listing.Items) + if err != nil { + t.Fatalf("Unable to Marshal the listing ") + } + //t.Logf("json value: %s\n", string(marsh)) + expectedJsonString := string(marsh) + if actualJsonResponseString != expectedJsonString { + t.Errorf("Json response string doesnt match the expected Json response ") + } } From e3cea042d63a7f4eb8fb7151c8634b812eea917a Mon Sep 17 00:00:00 2001 From: karthic rao Date: Sun, 30 Aug 2015 19:00:35 +0530 Subject: [PATCH 2/2] Left over comments removed Redundant comments in the code removed --- middleware/browse/browse_test.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/middleware/browse/browse_test.go b/middleware/browse/browse_test.go index 13894a8d..9f91f34e 100644 --- a/middleware/browse/browse_test.go +++ b/middleware/browse/browse_test.go @@ -1,8 +1,7 @@ package browse import ( - //"bytes" - "encoding/json" + "encoding/json" "github.com/mholt/caddy/middleware" "net/http" "net/http/httptest" @@ -191,7 +190,7 @@ func TestBrowseJson(t *testing.T) { } actualJsonResponseString := rec.Body.String() - //t.Logf("json response body: %s\n", respBody) + //generating the listing to compare it with the response body file, err := os.Open(b.Root + req.URL.Path) if err != nil { @@ -233,12 +232,10 @@ func TestBrowseJson(t *testing.T) { listing.Order = "asc" listing.applySort() - //var buf bytes.Buffer marsh, err := json.Marshal(listing.Items) if err != nil { t.Fatalf("Unable to Marshal the listing ") } - //t.Logf("json value: %s\n", string(marsh)) expectedJsonString := string(marsh) if actualJsonResponseString != expectedJsonString { t.Errorf("Json response string doesnt match the expected Json response ")