fleet/server/service/transport_labels_test.go
Zachary Wasserman 715d908613 Update go-kit to 0.4.0 (#1411)
Notable refactoring:
- Use stdlib "context" in place of "golang.org/x/net/context"
- Go-kit no longer wraps errors, so we remove the unwrap in transport_error.go
- Use MakeHandler when setting up endpoint tests (fixes test bug caught during
  this refactoring)

Closes #1411.
2017-03-15 08:55:30 -07:00

70 lines
1.8 KiB
Go

package service
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)
func TestDecodeCreateLabelRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/labels", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeCreateLabelRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(createLabelRequest)
assert.Equal(t, "foo", *params.payload.Name)
assert.Equal(t, "select * from foo;", *params.payload.Query)
assert.Equal(t, "darwin", *params.payload.Platform)
}).Methods("POST")
var body bytes.Buffer
body.Write([]byte(`{
"name": "foo",
"query": "select * from foo;",
"platform": "darwin"
}`))
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("POST", "/api/v1/kolide/labels", &body),
)
}
func TestDecodeDeleteLabelRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/labels/{id}", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeDeleteLabelRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(deleteLabelRequest)
assert.Equal(t, uint(1), params.ID)
}).Methods("DELETE")
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("DELETE", "/api/v1/kolide/labels/1", nil),
)
}
func TestDecodeGetLabelRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/labels/{id}", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeGetLabelRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(getLabelRequest)
assert.Equal(t, uint(1), params.ID)
}).Methods("GET")
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("GET", "/api/v1/kolide/labels/1", nil),
)
}