fix mock and tests

This commit is contained in:
Pedro Nasser
2016-08-11 02:18:54 -03:00
parent 5e892034e7
commit 9472575303
3 changed files with 21 additions and 6 deletions

View File

@@ -40,3 +40,11 @@ func (m *Mock) StoreRoute(route *models.Route) (*models.Route, error) {
func (m *Mock) RemoveRoute(app, route string) error { func (m *Mock) RemoveRoute(app, route string) error {
return nil return nil
} }
func (m *Mock) Put(key, value []byte) error {
return nil
}
func (m *Mock) Get(key []byte) ([]byte, error) {
return []byte{}, nil
}

View File

@@ -8,15 +8,19 @@ import (
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"github.com/Sirupsen/logrus" "golang.org/x/net/context"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/iron-io/functions/api/models" "github.com/iron-io/functions/api/models"
titancommon "github.com/iron-io/titan/common"
) )
func testRouter() *gin.Engine { func testRouter() *gin.Engine {
r := gin.Default() r := gin.Default()
ctx := context.Background()
r.Use(func(c *gin.Context) { r.Use(func(c *gin.Context) {
c.Set("log", logrus.WithFields(logrus.Fields{})) ctx, _ := titancommon.LoggerWithFields(ctx, extractFields(c))
c.Set("ctx", ctx)
c.Next() c.Next()
}) })
bindHandlers(r) bindHandlers(r)

View File

@@ -2,6 +2,7 @@ package server
import ( import (
"bytes" "bytes"
"fmt"
"net/http" "net/http"
"strings" "strings"
"testing" "testing"
@@ -20,7 +21,7 @@ func TestRouteRunnerGet(t *testing.T) {
expectedCode int expectedCode int
expectedError error expectedError error
}{ }{
{"/route", "", http.StatusNotFound, models.ErrRunnerRouteNotFound}, {"/route", "", http.StatusBadRequest, models.ErrAppsNotFound},
{"/r/app/route", "", http.StatusNotFound, models.ErrRunnerRouteNotFound}, {"/r/app/route", "", http.StatusNotFound, models.ErrRunnerRouteNotFound},
{"/route?payload=test", "", http.StatusBadRequest, models.ErrInvalidJSON}, {"/route?payload=test", "", http.StatusBadRequest, models.ErrInvalidJSON},
{"/r/app/route?payload=test", "", http.StatusBadRequest, models.ErrInvalidJSON}, {"/r/app/route?payload=test", "", http.StatusBadRequest, models.ErrInvalidJSON},
@@ -55,7 +56,7 @@ func TestRouteRunnerPost(t *testing.T) {
}{ }{
{"/route", `payload`, http.StatusBadRequest, models.ErrInvalidJSON}, {"/route", `payload`, http.StatusBadRequest, models.ErrInvalidJSON},
{"/r/app/route", `payload`, http.StatusBadRequest, models.ErrInvalidJSON}, {"/r/app/route", `payload`, http.StatusBadRequest, models.ErrInvalidJSON},
{"/route", `{ "payload": "" }`, http.StatusNotFound, models.ErrRunnerRouteNotFound}, {"/route", `{ "payload": "" }`, http.StatusBadRequest, models.ErrAppsNotFound},
{"/r/app/route", `{ "payload": "" }`, http.StatusNotFound, models.ErrRunnerRouteNotFound}, {"/r/app/route", `{ "payload": "" }`, http.StatusNotFound, models.ErrRunnerRouteNotFound},
} { } {
body := bytes.NewBuffer([]byte(test.body)) body := bytes.NewBuffer([]byte(test.body))
@@ -68,8 +69,10 @@ func TestRouteRunnerPost(t *testing.T) {
if test.expectedError != nil { if test.expectedError != nil {
resp := getErrorResponse(t, rec) resp := getErrorResponse(t, rec)
respMsg := resp.Error.Message
if !strings.Contains(resp.Error.Message, test.expectedError.Error()) { expMsg := test.expectedError.Error()
fmt.Println(respMsg == expMsg)
if respMsg != expMsg && !strings.Contains(respMsg, expMsg) {
t.Errorf("Test %d: Expected error message to have `%s`", t.Errorf("Test %d: Expected error message to have `%s`",
i, test.expectedError.Error()) i, test.expectedError.Error())
} }