Commit bb2e0cbb authored by santiaago's avatar santiaago
Browse files

better test coverage and add caching tests

parent f9ed00a4
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
package isogrids

import (
	"net/http"
	"testing"

	"github.com/taironas/route"
	tgTesting "github.com/taironas/tinygraphs/testing"
)

func TestRandomMirror(t *testing.T) {

	r := new(route.Router)
	r.HandleFunc("/isogrids/random-mirror", RandomMirror)

	test := tgTesting.GenerateHandlerFunc(t, RandomMirror)
	for _, p := range tgTesting.GoodParams {
		recorder := test("/isogrids/random-mirror", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}

	for _, p := range tgTesting.BadParams {
		recorder := test("/isogrids/random-mirror", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}
}
+30 −0
Original line number Diff line number Diff line
package isogrids

import (
	"net/http"
	"testing"

	"github.com/taironas/route"
	tgTesting "github.com/taironas/tinygraphs/testing"
)

func TestRandom(t *testing.T) {

	r := new(route.Router)
	r.HandleFunc("/isogrids/random", Random)

	test := tgTesting.GenerateHandlerFunc(t, Random)
	for _, p := range tgTesting.GoodParams {
		recorder := test("/isogrids/random", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}

	for _, p := range tgTesting.BadParams {
		recorder := test("/isogrids/random", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}
}
+0 −1
Original line number Diff line number Diff line
@@ -37,7 +37,6 @@ func Square(w http.ResponseWriter, r *http.Request) {
	e := `"` + theme + key + `"`
	w.Header().Set("Etag", e)
	w.Header().Set("Cache-Control", "max-age=2592000") // 30 days

	if match := r.Header.Get("If-None-Match"); match != "" {
		if strings.Contains(match, e) {
			w.WriteHeader(http.StatusNotModified)
+20 −2
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ package squares

import (
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/taironas/route"
@@ -18,15 +19,32 @@ func TestSquares(t *testing.T) {
	for _, p := range tgTesting.GoodParams {
		recorder := test("/squares/test", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusBadRequest)
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}

	for _, p := range tgTesting.BadParams {
		recorder := test("/squares/test", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusBadRequest)
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}

	// test caching:
	recorder := test("/squares/cache", "GET", nil, r)
	if recorder.Code != http.StatusOK {
		t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
	}

	if req, err := http.NewRequest("GET", "/squares/cache", nil); err != nil {
		t.Errorf("%v", err)
	} else {
		req.Header.Set("Content-Type", "application/json")
		req.Header.Set("If-None-Match", recorder.Header().Get("Etag"))
		recorder2 := httptest.NewRecorder()
		r.ServeHTTP(recorder2, req)
		if recorder2.Code != http.StatusNotModified {
			t.Errorf("returned %v. Expected %v.", recorder2.Code, http.StatusNotModified)
		}
	}
}