Example #1
0
func TestPatch(t *testing.T) {
	m := minion.New(minion.Options{
		UnauthenticatedRoutes: []string{"*"}},
	)

	type user struct {
		ID    string `json:"id"`
		Name  string `json:"name"`
		Email string `json:"email"`
	}

	userHandler := func(ctx *minion.Context) {
		dbUser := &user{"1", "John", "*****@*****.**"}
		user := new(user)
		ctx.GetResource(user)

		dbUser.Email = user.Email
		ctx.JSON(200, dbUser)
	}

	m.Patch("/users/:id", userHandler)

	ts := httptest.NewServer(m)
	defer ts.Close()

	var j, body string
	var status int

	payload := `{"email":"*****@*****.**"}`
	status, body = tst.Request(t, ts, "PATCH", "/users/1", nil, bytes.NewBuffer([]byte(payload)))
	tst.AssertEqual(t, 200, status)

	j = `{"id":"1","name":"John","email":"*****@*****.**"}`
	tst.AssertEqual(t, j, body)
}
Example #2
0
func TestDelete(t *testing.T) {
	m := minion.New(minion.Options{
		UnauthenticatedRoutes: []string{"*"}},
	)

	userHandler := func(ctx *minion.Context) {
		j := struct {
			Message string `json:"message"`
		}{
			"ok",
		}
		ctx.JSON(200, j)
	}

	m.Delete("/users/:id", userHandler)

	ts := httptest.NewServer(m)
	defer ts.Close()

	var j, body string
	var status int

	status, body = tst.Request(t, ts, "DELETE", "/users/1", nil, nil)
	tst.AssertEqual(t, 200, status)

	j = `{"message":"ok"}`
	tst.AssertEqual(t, j, body)
}
Example #3
0
func TestPost(t *testing.T) {
	m := minion.New(minion.Options{
		UnauthenticatedRoutes: []string{"*"}},
	)

	usersHandler := func(ctx *minion.Context) {
		user := &struct {
			ID    string `json:"id"`
			Name  string `json:"name"`
			Email string `json:"email"`
		}{}

		ctx.GetResource(user)

		user.ID = "1"

		ctx.JSON(200, user)
	}

	m.Post("/users", usersHandler)

	ts := httptest.NewServer(m)
	defer ts.Close()

	var j, body string
	var status int

	payload := `{"name":"John","email":"*****@*****.**"}`
	status, body = tst.Request(t, ts, "POST", "/users", nil, bytes.NewBuffer([]byte(payload)))
	tst.AssertEqual(t, 200, status)

	j = `{"id":"1","name":"John","email":"*****@*****.**"}`
	tst.AssertEqual(t, j, body)
}
Example #4
0
func TestGetResource(t *testing.T) {
	user := struct {
		ID    string `json:"id"`
		Name  string `json:"name"`
		Email string `json:"email"`
	}{
		"1",
		"John Doe",
		"*****@*****.**",
	}

	body := bytes.NewBufferString(`{"id":"1","name":"John Doe","email":"*****@*****.**"}`)

	m := minion.New(minion.Options{})
	m.Post("/users", nil)

	ts := httptest.NewServer(m)
	defer ts.Close()

	req, _ := http.NewRequest("POST", ts.URL+"/users", body)

	var resource struct {
		ID    string `json:"id"`
		Name  string `json:"name"`
		Email string `json:"email"`
	}

	ctx := &minion.Context{Req: req}
	err := ctx.GetResource(&resource)

	tst.AssertNoError(t, err)
	tst.AssertEqual(t, user, resource)
}
Example #5
0
func TestHead(t *testing.T) {
	m := minion.New(minion.Options{
		UnauthenticatedRoutes: []string{"*"}},
	)

	userHandler := func(ctx *minion.Context) {
		ctx.Text(200, "")
	}

	m.Head("/users", userHandler)

	ts := httptest.NewServer(m)
	defer ts.Close()

	var body string
	var status int

	status, body = tst.Request(t, ts, "HEAD", "/users", nil, nil)
	tst.AssertEqual(t, 200, status)

	tst.AssertEqual(t, "", body)
}
Example #6
0
func TestNamespace(t *testing.T) {

	usersHandler := func(ctx *minion.Context) {
		ctx.JSON(200, nil)
	}

	m := minion.New(minion.Options{
		UnauthenticatedRoutes: []string{"*"},
		Namespace:             "/v1"},
	)

	m.Get("/users", usersHandler)

	ts := httptest.NewServer(m)
	defer ts.Close()

	var status int

	status, _ = tst.Request(t, ts, "GET", "/v1/users", nil, nil)
	tst.AssertEqual(t, 200, status)

	status, _ = tst.Request(t, ts, "GET", "/users", nil, nil)
	tst.AssertEqual(t, 404, status)
}
Example #7
0
func TestGet(t *testing.T) {
	john := struct {
		Name  string `json:"name"`
		Email string `json:"email"`
	}{
		"John Doe",
		"*****@*****.**",
	}

	foo := struct {
		Name  string `json:"name"`
		Email string `json:"email"`
	}{
		"Foo Bar",
		"*****@*****.**",
	}

	usersHandler := func(ctx *minion.Context) {
		users := struct {
			Users []interface{} `json:"users"`
		}{
			[]interface{}{john, foo},
		}
		ctx.JSON(200, users)
	}

	userHandler := func(ctx *minion.Context) {
		id := ctx.ByGet("id")
		doe := struct {
			ID    string `json:"id"`
			Name  string `json:"name"`
			Email string `json:"email"`
		}{
			id,
			"John Doe",
			"*****@*****.**",
		}
		ctx.JSON(200, doe)
	}

	m := minion.New(minion.Options{
		UnauthenticatedRoutes: []string{"*"}},
	)

	m.Get("/users", usersHandler)
	m.Get("/user/:id", userHandler)

	ts := httptest.NewServer(m)
	defer ts.Close()

	var j, body string
	var status int

	status, body = tst.Request(t, ts, "GET", "/users", nil, nil)
	tst.AssertEqual(t, 200, status)

	j = `{"users":[{"name":"John Doe","email":"*****@*****.**"},{"name":"Foo Bar","email":"*****@*****.**"}]}`
	tst.AssertEqual(t, j, body)

	status, body = tst.Request(t, ts, "GET", "/user/1", nil, nil)
	tst.AssertEqual(t, 200, status)

	j = `{"id":"1","name":"John Doe","email":"*****@*****.**"}`
	tst.AssertEqual(t, j, body)
}
Example #8
0
func TestNew(t *testing.T) {
	m := minion.New(minion.Options{})

	tst.AssertNotNil(t, m)
	tst.AssertNotNil(t, m.Router)
}