adding server tests

This commit is contained in:
David Lawrence 2015-04-20 16:09:44 -07:00
parent fa38bc7ba9
commit 2ec64fcec6
4 changed files with 43 additions and 3 deletions

View File

@ -14,6 +14,8 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
// TODO: This is just for PoC. The real DB should be injected as part of
// the context for a final version.
var db = util.GetSqliteDB() var db = util.GetSqliteDB()
// MainHandler is the default handler for the server // MainHandler is the default handler for the server
@ -24,7 +26,7 @@ func MainHandler(ctx utils.IContext, w http.ResponseWriter, r *http.Request) *er
w.Write([]byte("{server_error: 'Could not parse error message'}")) w.Write([]byte("{server_error: 'Could not parse error message'}"))
} }
} else { } else {
w.WriteHeader(http.StatusNotFound) //w.WriteHeader(http.StatusNotFound)
return &errors.HTTPError{ return &errors.HTTPError{
HTTPStatus: http.StatusNotFound, HTTPStatus: http.StatusNotFound,
Code: 9999, Code: 9999,

View File

@ -1 +1,38 @@
package handlers package handlers
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/endophage/go-tuf/signed"
"github.com/docker/vetinari/utils"
)
func TestMainHandlerGet(t *testing.T) {
hand := utils.RootHandlerFactory(&utils.InsecureAuthorizer{}, utils.ContextFactory, &signed.Ed25519{})
handler := hand(MainHandler)
ts := httptest.NewServer(handler)
defer ts.Close()
_, err := http.Get(ts.URL)
if err != nil {
t.Fatalf("Received error on GET /: %s", err.Error())
}
}
func TestMainHandlerNotGet(t *testing.T) {
hand := utils.RootHandlerFactory(&utils.InsecureAuthorizer{}, utils.ContextFactory, &signed.Ed25519{})
handler := hand(MainHandler)
ts := httptest.NewServer(handler)
defer ts.Close()
res, err := http.Head(ts.URL)
if err != nil {
t.Fatalf("Received error on GET /: %s", err.Error())
}
if res.StatusCode != http.StatusNotFound {
t.Fatalf("Expected 404, received %d", res.StatusCode)
}
}

View File

@ -72,7 +72,7 @@ func TestRunGoodCancel(t *testing.T) {
err := Run(ctx, config) err := Run(ctx, config)
if _, ok := err.(*net.OpError); !ok { if _, ok := err.(*net.OpError); !ok {
t.Fatal("Received unexpected err: %s", err.Error()) t.Fatalf("Received unexpected err: %s", err.Error())
} }
if !strings.Contains(err.Error(), "use of closed network connection") { if !strings.Contains(err.Error(), "use of closed network connection") {
t.Fatalf("Received unexpected err: %s", err.Error()) t.Fatalf("Received unexpected err: %s", err.Error())

View File

@ -8,8 +8,9 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/docker/vetinari/errors"
"github.com/endophage/go-tuf/signed" "github.com/endophage/go-tuf/signed"
"github.com/docker/vetinari/errors"
) )
func MockBetterHandler(ctx IContext, w http.ResponseWriter, r *http.Request) *errors.HTTPError { func MockBetterHandler(ctx IContext, w http.ResponseWriter, r *http.Request) *errors.HTTPError {