github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/rpc/http_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:42</date>
    10  //</624450108846510080>
    11  
    12  
    13  package rpc
    14  
    15  import (
    16  	"net/http"
    17  	"net/http/httptest"
    18  	"strings"
    19  	"testing"
    20  )
    21  
    22  func TestHTTPErrorResponseWithDelete(t *testing.T) {
    23  	testHTTPErrorResponse(t, http.MethodDelete, contentType, "", http.StatusMethodNotAllowed)
    24  }
    25  
    26  func TestHTTPErrorResponseWithPut(t *testing.T) {
    27  	testHTTPErrorResponse(t, http.MethodPut, contentType, "", http.StatusMethodNotAllowed)
    28  }
    29  
    30  func TestHTTPErrorResponseWithMaxContentLength(t *testing.T) {
    31  	body := make([]rune, maxRequestContentLength+1)
    32  	testHTTPErrorResponse(t,
    33  		http.MethodPost, contentType, string(body), http.StatusRequestEntityTooLarge)
    34  }
    35  
    36  func TestHTTPErrorResponseWithEmptyContentType(t *testing.T) {
    37  	testHTTPErrorResponse(t, http.MethodPost, "", "", http.StatusUnsupportedMediaType)
    38  }
    39  
    40  func TestHTTPErrorResponseWithValidRequest(t *testing.T) {
    41  	testHTTPErrorResponse(t, http.MethodPost, contentType, "", 0)
    42  }
    43  
    44  func testHTTPErrorResponse(t *testing.T, method, contentType, body string, expected int) {
    45  request := httptest.NewRequest(method, "http://url.com“,字符串.newreader(body)
    46  	request.Header.Set("content-type", contentType)
    47  	if code, _ := validateRequest(request); code != expected {
    48  		t.Fatalf("response code should be %d not %d", expected, code)
    49  	}
    50  }
    51