github.com/braveheart12/insolar-09-08-19@v0.8.7/functest/common_test.go (about)

     1  // +build functest
     2  
     3  /*
     4   *    Copyright 2019 Insolar Technologies
     5   *
     6   *    Licensed under the Apache License, Version 2.0 (the "License");
     7   *    you may not use this file except in compliance with the License.
     8   *    You may obtain a copy of the License at
     9   *
    10   *        http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *    Unless required by applicable law or agreed to in writing, software
    13   *    distributed under the License is distributed on an "AS IS" BASIS,
    14   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   *    See the License for the specific language governing permissions and
    16   *    limitations under the License.
    17   */
    18  
    19  package functest
    20  
    21  import (
    22  	"bytes"
    23  	"encoding/json"
    24  	"io/ioutil"
    25  	"net/http"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestWrongUrl(t *testing.T) {
    32  	jsonValue, _ := json.Marshal(postParams{})
    33  	testURL := HOST + "/not_api"
    34  	postResp, err := http.Post(testURL, "application/json", bytes.NewBuffer(jsonValue))
    35  	require.NoError(t, err)
    36  	require.Equal(t, http.StatusNotFound, postResp.StatusCode)
    37  }
    38  
    39  func TestGetRequest(t *testing.T) {
    40  	postResp, err := http.Get(TestCallUrl)
    41  	require.NoError(t, err)
    42  	require.Equal(t, http.StatusOK, postResp.StatusCode)
    43  	body, err := ioutil.ReadAll(postResp.Body)
    44  	require.NoError(t, err)
    45  
    46  	getResponse := &response{}
    47  	unmarshalCallResponse(t, body, getResponse)
    48  	require.NotNil(t, getResponse.Error)
    49  
    50  	require.Equal(t, "[ UnmarshalRequest ] Empty body", getResponse.Error)
    51  	require.Nil(t, getResponse.Result)
    52  }
    53  
    54  func TestWrongJson(t *testing.T) {
    55  	postResp, err := http.Post(TestCallUrl, "application/json", bytes.NewBuffer([]byte("some not json value")))
    56  	require.NoError(t, err)
    57  	require.Equal(t, http.StatusOK, postResp.StatusCode)
    58  	body, err := ioutil.ReadAll(postResp.Body)
    59  	require.NoError(t, err)
    60  
    61  	response := &response{}
    62  	unmarshalCallResponse(t, body, response)
    63  	require.NotNil(t, response.Error)
    64  
    65  	require.Equal(t, "[ UnmarshalRequest ] Can't unmarshal input params: invalid character 's' looking for beginning of value", response.Error)
    66  	require.Nil(t, response.Result)
    67  }