github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/rpc/http_test.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package rpc 13 14 import ( 15 "net/http" 16 "net/http/httptest" 17 "strings" 18 "testing" 19 ) 20 21 func TestHTTPErrorResponseWithDelete(t *testing.T) { 22 testHTTPErrorResponse(t, http.MethodDelete, contentType, "", http.StatusMethodNotAllowed) 23 } 24 25 func TestHTTPErrorResponseWithPut(t *testing.T) { 26 testHTTPErrorResponse(t, http.MethodPut, contentType, "", http.StatusMethodNotAllowed) 27 } 28 29 func TestHTTPErrorResponseWithMaxContentLength(t *testing.T) { 30 body := make([]rune, maxHTTPRequestContentLength+1) 31 testHTTPErrorResponse(t, 32 http.MethodPost, contentType, string(body), http.StatusRequestEntityTooLarge) 33 } 34 35 func TestHTTPErrorResponseWithEmptyContentType(t *testing.T) { 36 testHTTPErrorResponse(t, http.MethodPost, "", "", http.StatusUnsupportedMediaType) 37 } 38 39 func TestHTTPErrorResponseWithValidRequest(t *testing.T) { 40 testHTTPErrorResponse(t, http.MethodPost, contentType, "", 0) 41 } 42 43 func testHTTPErrorResponse(t *testing.T, method, contentType, body string, expected int) { 44 request := httptest.NewRequest(method, "http://url.com", strings.NewReader(body)) 45 request.Header.Set("content-type", contentType) 46 if code, _ := validateRequest(request); code != expected { 47 t.Fatalf("response code should be %d not %d", expected, code) 48 } 49 }