github.com/rohankumardubey/aresdb@v0.0.2-0.20190517170215-e54e3ca06b9c/api/data_handler_test.go (about)

     1  //  Copyright (c) 2017-2018 Uber Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package api
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"net/http"
    22  	"net/http/httptest"
    23  
    24  	"github.com/uber/aresdb/memstore"
    25  	memCom "github.com/uber/aresdb/memstore/common"
    26  	memMocks "github.com/uber/aresdb/memstore/mocks"
    27  	metaCom "github.com/uber/aresdb/metastore/common"
    28  
    29  	"github.com/gorilla/mux"
    30  	"github.com/onsi/ginkgo"
    31  	. "github.com/onsi/gomega"
    32  	"github.com/stretchr/testify/mock"
    33  )
    34  
    35  var _ = ginkgo.Describe("DataHandler", func() {
    36  	var testServer *httptest.Server
    37  	var testSchema = memstore.NewTableSchema(&metaCom.Table{
    38  		Name:        "abc",
    39  		IsFactTable: false,
    40  		Config: metaCom.TableConfig{
    41  			BatchSize: 10,
    42  		},
    43  	})
    44  
    45  	var memStore *memMocks.MemStore
    46  	ginkgo.BeforeEach(func() {
    47  		memStore = CreateMemStore(testSchema, 0, nil, CreateMockDiskStore())
    48  		memStore.On("HandleIngestion", "abc", 0, mock.Anything).Return(nil)
    49  		dataHandler := NewDataHandler(memStore)
    50  		testRouter := mux.NewRouter()
    51  		dataHandler.Register(testRouter.PathPrefix("/data").Subrouter())
    52  
    53  		testServer = httptest.NewUnstartedServer(WithPanicHandling(testRouter))
    54  		testServer.Start()
    55  	})
    56  
    57  	ginkgo.AfterEach(func() {
    58  		testServer.Close()
    59  	})
    60  
    61  	ginkgo.It("PostData fails on invalid request", func() {
    62  		hostPort := testServer.Listener.Addr().String()
    63  		resp, err := http.Post(fmt.Sprintf("http://%s/data/abc/0", hostPort), "application/upsert-data", bytes.NewBuffer([]byte{}))
    64  		Ω(err).Should(BeNil())
    65  		_, err = ioutil.ReadAll(resp.Body)
    66  		Ω(err).Should(BeNil())
    67  		Ω(resp.StatusCode).Should(Equal(http.StatusBadRequest))
    68  	})
    69  
    70  	ginkgo.It("PostData should succeed on empty data post", func() {
    71  		buffer, _ := memCom.NewUpsertBatchBuilder().ToByteArray()
    72  		hostPort := testServer.Listener.Addr().String()
    73  		resp, err := http.Post(fmt.Sprintf("http://%s/data/abc/0", hostPort), "application/upsert-data", bytes.NewBuffer(buffer))
    74  		Ω(err).Should(BeNil())
    75  		_, err = ioutil.ReadAll(resp.Body)
    76  		Ω(err).Should(BeNil())
    77  		Ω(resp.StatusCode).Should(Equal(http.StatusOK))
    78  	})
    79  })