github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/rpc/http_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestHTTPErrorResponseWithDelete(t *testing.T) {
    11  	testHTTPErrorResponse(t, http.MethodDelete, contentType, "", http.StatusMethodNotAllowed)
    12  }
    13  
    14  func TestHTTPErrorResponseWithPut(t *testing.T) {
    15  	testHTTPErrorResponse(t, http.MethodPut, contentType, "", http.StatusMethodNotAllowed)
    16  }
    17  
    18  func TestHTTPErrorResponseWithMaxContentLength(t *testing.T) {
    19  	body := make([]rune, maxRequestContentLength+1)
    20  	testHTTPErrorResponse(t,
    21  		http.MethodPost, contentType, string(body), http.StatusRequestEntityTooLarge)
    22  }
    23  
    24  func TestHTTPErrorResponseWithEmptyContentType(t *testing.T) {
    25  	testHTTPErrorResponse(t, http.MethodPost, "", "", http.StatusUnsupportedMediaType)
    26  }
    27  
    28  func TestHTTPErrorResponseWithValidRequest(t *testing.T) {
    29  	testHTTPErrorResponse(t, http.MethodPost, contentType, "", 0)
    30  }
    31  
    32  func testHTTPErrorResponse(t *testing.T, method, contentType, body string, expected int) {
    33  	request := httptest.NewRequest(method, "http://url.com", strings.NewReader(body))
    34  	request.Header.Set("content-type", contentType)
    35  	if code, _ := validateRequest(request); code != expected {
    36  		t.Fatalf("response code should be %d not %d", expected, code)
    37  	}
    38  }