1. Yann Malet
  2. gowebexp

Commits

Yann Malet  committed d7bd5c9

Added test to exercise the handlers.

  • Participants
  • Parent commits 5d5d927
  • Branches master

Comments (0)

Files changed (3)

File cmd/server.go

View file
 )
 
 func main() {
-	// Create an instance of a MemStorage
-	fmt.Println("Loading the storage backend")
-
-	// request multiplexer
-	web.App.Router.HandleFunc("/inspect/{slug}/", web.RequestInspector).Name("inspector")
-	web.App.Router.HandleFunc("/pages/{slug}/", web.PageDetail).Name("page_detail")
-	web.App.Router.HandleFunc("/pages/", web.PageList).Name("page_list")
-	web.App.Router.HandleFunc("/", web.Index).Name("index")
+	web.App.SetRoute()
 
 	// Start serving
 	fmt.Println("Starting server on : http://127.0.0.1:8080")
 	http.Handle("/", web.App.Router)
+
 	// Serving static file
 	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(web.App.Path+"/web/static/"))))
 

File web/app.go

View file
 	"github.com/gorilla/mux"
 	"html/template"
 	"net/http"
-	"os"
+	//"os"
 	"path/filepath"
 )
 
 	if err != nil {
 		fmt.Println("An error occured while creating the storage : ", err)
 	}
-	path, err := os.Getwd()
-	if err != nil {
-		fmt.Println("An error occured while trying to retireve the current path", err)
-	}
+	//path, err := os.Getwd()
+	//if err != nil {
+	//	fmt.Println("An error occured while trying to retireve the current path", err)
+	//}
+	path := "/home/yml/Dropbox/Devs/golang/gorilla_on_web/src/gowiki/"
 
 	return &WebApp{
 		Router:  mux.NewRouter(),
 	return filepath.Join(app.Path, "web", "templates", name)
 }
 
+func (app *WebApp) SetRoute() {
+	// request multiplexer
+	app.Router.HandleFunc("/inspect/{slug}/", RequestInspector).Name("inspector")
+	app.Router.HandleFunc("/pages/{slug}/", PageDetail).Name("page_detail")
+	app.Router.HandleFunc("/pages/", PageList).Name("page_list")
+	app.Router.HandleFunc("/", Index).Name("index")
+}
+
 // Initialize my app object
 var App = NewWebApp()
 
 		return
 	}
 	ctx := make(map[string]interface{})
-	ctx["page"], err = App.Storage.GetPageBySlug(mux.Vars(r)["slugs"])
+	ctx["page"], err = App.Storage.GetPageBySlug(mux.Vars(r)["slug"])
 	if err != nil {
 		http.NotFound(w, r)
 		return

File web/app_test.go

View file
+package web
+
+import (
+	"fmt"
+	//"io/ioutil"
+	"net/http"
+	"net/http/httptest"
+	"net/url"
+	"regexp"
+	"testing"
+)
+
+func TestHandlers(t *testing.T) {
+	fmt.Println("running test from web pkg")
+	tests := []struct {
+		Description string
+		Handler     func(http.ResponseWriter, *http.Request)
+		Path        string
+		Params      url.Values
+		Status      int
+		Match       map[string]bool
+	}{
+		{
+			Description: "RequestInspector",
+			Handler:     RequestInspector,
+			Path:        "/",
+			Status:      http.StatusOK,
+			Match: map[string]bool{
+				"URL": true,
+			},
+		},
+		//{
+		//	Description: "Index",
+		//	Handler:     Index,
+		//	Path:        "/",
+		//	Status:      http.StatusOK},
+	}
+
+	for _, test := range tests {
+		record := httptest.NewRecorder()
+		req := &http.Request{
+			Method: "GET",
+			URL:    &url.URL{Path: test.Path},
+			Form:   test.Params,
+		}
+		test.Handler(record, req)
+		fmt.Println("Description : ", test.Description)
+		fmt.Println("StatusCode : ", record.Code)
+		fmt.Println("Body : ", record.Body)
+
+		if got, want := record.Code, test.Status; got != want {
+			t.Errorf("%s: response code = %d, want %d", test.Description, got, want)
+		}
+		for re, match := range test.Match {
+			if got := regexp.MustCompile(re).Match(record.Body.Bytes()); got != match {
+				t.Errorf("%s: %q ~ /%s/ = %v, want %v", test.Description, record.Body, re, got, match)
+			}
+		}
+	}
+}
+
+func TestAppHandler(t *testing.T) {
+	App.SetRoute()
+	srv := httptest.NewServer(App.Router)
+	defer srv.Close()
+	srvURL, err := url.Parse(srv.URL)
+	if err != nil {
+		t.Fatal(err)
+	}
+	fmt.Println("Test server URL", srvURL)
+	tests := []struct {
+		Path       string
+		Method     string
+		StatusCode int
+	}{
+		{
+			Path:       srv.URL + "/pages/foo/",
+			Method:     "GET",
+			StatusCode: 200,
+		},
+		{
+			Path:       srv.URL + "/pages/",
+			Method:     "GET",
+			StatusCode: 200,
+		},
+		{
+			Path:       srv.URL + "/",
+			Method:     "GET",
+			StatusCode: 200,
+		},
+		{
+			Path:       srv.URL + "/NotFound/",
+			Method:     "GET",
+			StatusCode: 404,
+		},
+	}
+
+	for _, test := range tests {
+		fmt.Println("Request : ", test.Method, test.Path, test.StatusCode)
+		req, err := http.NewRequest(test.Method, test.Path, nil)
+		if err != nil {
+			t.Fatal(err)
+		}
+		client := &http.Client{}
+		resp, err := client.Do(req)
+		if err != nil {
+			t.Fatal(err)
+		}
+		defer resp.Body.Close()
+		if resp.StatusCode != test.StatusCode {
+			t.Error("Error the status code do not match the expectation")
+		}
+		//body, _ := ioutil.ReadAll(resp.Body)
+		//fmt.Println("Body : ", string(body))
+	}
+}