Exemple #1
0
func TestMiddlewareShareSameContextInstance(t *testing.T) {
	router := createTestRouter()

	testUser := users.NewUser("TestUsername", "testpassword", "*****@*****.**")
	authMiddleware := MiddlewareFunc(func(w http.ResponseWriter, r *http.Request, context *Context, next HandlerFunc) {
		context.User = testUser
		next(w, r, context)
	})
	router.Use(authMiddleware)

	var userMatches bool
	handler := func(w http.ResponseWriter, r *http.Request, context *Context) {
		userMatches = (context.User == testUser)
	}

	route := createTestRoute(router)
	route.HandlerFunc(handler)

	writer := httptest.NewRecorder()
	request := createTestRequest()
	router.ServeHTTP(writer, request)

	if !userMatches {
		t.Errorf("Context was not passed as expected from middleware to handler")
	}
}
Exemple #2
0
func extractUserFromRegistrationRequest(r *http.Request) *users.User {
	username := r.FormValue("username")
	password := r.FormValue("password")
	email := r.FormValue("email")
	return users.NewUser(username, password, email)
}
Exemple #3
0
	"github.com/crob1140/codewiz-server/routes"
	_ "github.com/mattn/go-sqlite3"
	"net/http"
	"net/http/httptest"
	"os"
	paths "path"
	"strings"
	"testing"
)

const (
	testAPIPath = "/api/v1"
)

var (
	testUser   = users.NewUser("TestUser", "testpassword", "*****@*****.**")
	testAdmin  = users.NewUser("TestAdmin", "testpassword", "*****@*****.**")
	testRouter *routes.Router
)

func TestMain(m *testing.M) {
	testRouter = createTestRouter(testAPIPath)
	retCode := m.Run()
	os.Exit(retCode)
}

func TestGetUser_AsVisitor(t *testing.T) {

	requestPath := paths.Join(testAPIPath, "/users/1")
	request := createTestRequest("GET", requestPath, "")
	writer := httptest.NewRecorder()