github.com/line/ostracon@v1.0.10-0.20230328032236-7f20145f065d/rpc/jsonrpc/server/http_uri_handler_test.go (about)

     1  package server
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"strconv"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/line/ostracon/libs/log"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestMakeHTTPHandler(t *testing.T) {
    16  	handlerFunc := makeHTTPHandler(TestRPCFunc, log.TestingLogger())
    17  	req, _ := http.NewRequest("GET", "http://localhost/", strings.NewReader(TestGoodBody))
    18  	rec := httptest.NewRecorder()
    19  	handlerFunc(rec, req)
    20  	res := rec.Result()
    21  	require.Equal(t, http.StatusOK, res.StatusCode)
    22  	res.Body.Close()
    23  }
    24  
    25  func TestMakeHTTPHandler_WS_WriteRPCResponseHTTPError_error(t *testing.T) {
    26  	handlerFunc := makeHTTPHandler(TestWSRPCFunc, log.TestingLogger())
    27  	req, _ := http.NewRequest("GET", "http://localhost/", nil)
    28  	rec := NewFailedWriteResponseWriter()
    29  	handlerFunc(rec, req)
    30  	assert.Equal(t,
    31  		strconv.Itoa(http.StatusNotFound),
    32  		rec.Header().Get(http.StatusText(http.StatusNotFound)))
    33  }
    34  
    35  func TestMakeHTTPHandler_httpParamsToArgs_WriteRPCResponseHTTPError_error(t *testing.T) {
    36  	handlerFunc := makeHTTPHandler(TestRPCFunc, log.TestingLogger())
    37  	// httpParamsToArgs error
    38  	req, _ := http.NewRequest("GET", "http://localhost/c?s=1", nil)
    39  	// WriteRPCResponseHTTPError error
    40  	rec := NewFailedWriteResponseWriter()
    41  	handlerFunc(rec, req)
    42  	assert.Equal(t,
    43  		strconv.Itoa(http.StatusInternalServerError),
    44  		rec.Header().Get(http.StatusText(http.StatusInternalServerError)))
    45  }
    46  
    47  func TestMakeHTTPHandler_unreflectResult_WriteRPCResponseHTTPError_error(t *testing.T) {
    48  	// unreflectResult error
    49  	handlerFunc := makeHTTPHandler(TestRPCErrorFunc, log.TestingLogger())
    50  	req, _ := http.NewRequest("GET", "http://localhost/", nil)
    51  	// WriteRPCResponseHTTPError error
    52  	rec := NewFailedWriteResponseWriter()
    53  	handlerFunc(rec, req)
    54  	assert.Equal(t,
    55  		strconv.Itoa(http.StatusInternalServerError),
    56  		rec.Header().Get(http.StatusText(http.StatusInternalServerError)))
    57  }
    58  
    59  func TestMakeHTTPHandler_last_WriteRPCResponseHTTP_error(t *testing.T) {
    60  	handlerFunc := makeHTTPHandler(TestRPCFunc, log.TestingLogger())
    61  	req, _ := http.NewRequest("GET", "http://localhost/", strings.NewReader(TestGoodBody))
    62  	// WriteRPCResponseHTTP error
    63  	rec := NewFailedWriteResponseWriter()
    64  	handlerFunc(rec, req)
    65  	assert.Equal(t,
    66  		strconv.Itoa(http.StatusOK),
    67  		rec.Header().Get(http.StatusText(http.StatusOK)))
    68  }