github.com/m3db/m3@v1.5.0/src/query/api/v1/handler/json/write_test.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package json
    22  
    23  import (
    24  	"bytes"
    25  	"fmt"
    26  	"io/ioutil"
    27  	"net/http"
    28  	"net/http/httptest"
    29  	"strings"
    30  	"testing"
    31  
    32  	"github.com/m3db/m3/src/query/api/v1/options"
    33  	"github.com/m3db/m3/src/query/models"
    34  	"github.com/m3db/m3/src/query/test/m3"
    35  
    36  	"github.com/golang/mock/gomock"
    37  	"github.com/stretchr/testify/require"
    38  )
    39  
    40  func TestFailingJSONWriteParsing(t *testing.T) {
    41  	badJSON := `{
    42  		   "tags": { "t
    43  			 "timestamp": "1534952005",
    44  			 "value": 10.0
    45  					}`
    46  
    47  	req, _ := http.NewRequest("POST", WriteJSONURL, strings.NewReader(badJSON))
    48  	_, err := parseRequest(req)
    49  	require.Error(t, err)
    50  }
    51  
    52  func generateJSONWriteRequest() string {
    53  	return `{
    54  		   "tags": { "tag_one": "val_one", "tag_two": "val_two" },
    55  			 "timestamp": "1534952005",
    56  			 "value": 10.0
    57  		      }`
    58  }
    59  
    60  func TestJSONWriteParsing(t *testing.T) {
    61  	jsonReq := generateJSONWriteRequest()
    62  	req := httptest.NewRequest("POST", WriteJSONURL, strings.NewReader(jsonReq))
    63  
    64  	r, err := parseRequest(req)
    65  	require.Nil(t, err, "unable to parse request")
    66  	require.Equal(t, 10.0, r.Value)
    67  	require.Equal(t, map[string]string{"tag_one": "val_one", "tag_two": "val_two"}, r.Tags)
    68  }
    69  
    70  func TestJSONWrite(t *testing.T) {
    71  	ctrl := gomock.NewController(t)
    72  	defer ctrl.Finish()
    73  
    74  	storage, session := m3.NewStorageAndSession(t, ctrl)
    75  	session.EXPECT().
    76  		WriteTagged(gomock.Any(), gomock.Any(), gomock.Any(),
    77  			gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
    78  		AnyTimes()
    79  	session.EXPECT().IteratorPools().
    80  		Return(nil, nil).AnyTimes()
    81  
    82  	opts := options.EmptyHandlerOptions().
    83  		SetTagOptions(models.NewTagOptions()).
    84  		SetStorage(storage)
    85  	handler := NewWriteJSONHandler(opts).(*WriteJSONHandler)
    86  
    87  	jsonReq := generateJSONWriteRequest()
    88  	req, err := http.NewRequest(JSONWriteHTTPMethod, WriteJSONURL,
    89  		strings.NewReader(jsonReq))
    90  	require.NoError(t, err)
    91  
    92  	resp := httptest.NewRecorder()
    93  	handler.ServeHTTP(resp, req)
    94  
    95  	require.Equal(t, http.StatusOK, resp.Code)
    96  }
    97  
    98  func TestJSONWriteError(t *testing.T) {
    99  	ctrl := gomock.NewController(t)
   100  	defer ctrl.Finish()
   101  
   102  	expectedErr := fmt.Errorf("an error")
   103  
   104  	storage, session := m3.NewStorageAndSession(t, ctrl)
   105  	session.EXPECT().
   106  		WriteTagged(gomock.Any(), gomock.Any(), gomock.Any(),
   107  			gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
   108  		AnyTimes().
   109  		Return(expectedErr)
   110  	session.EXPECT().IteratorPools().
   111  		Return(nil, nil).AnyTimes()
   112  
   113  	opts := options.EmptyHandlerOptions().SetStorage(storage)
   114  	jsonWrite := NewWriteJSONHandler(opts).(*WriteJSONHandler)
   115  
   116  	jsonReq := generateJSONWriteRequest()
   117  	req, err := http.NewRequest(JSONWriteHTTPMethod, WriteJSONURL,
   118  		strings.NewReader(jsonReq))
   119  	require.NoError(t, err)
   120  
   121  	writer := httptest.NewRecorder()
   122  	jsonWrite.ServeHTTP(writer, req)
   123  	resp := writer.Result()
   124  	require.Equal(t, http.StatusInternalServerError, resp.StatusCode)
   125  
   126  	body, err := ioutil.ReadAll(resp.Body)
   127  	require.NoError(t, err)
   128  
   129  	require.True(t, bytes.Contains(body, []byte(expectedErr.Error())),
   130  		fmt.Sprintf("body: %s", body))
   131  }