forked from vechain/thor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test coverage for api/utils package (vechain#676)
* test: add test coverage for api/utils package * refactor: rename http_test package to utils_test
- Loading branch information
1 parent
1ed5a6a
commit b8fc907
Showing
1 changed file
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Copyright (c) 2018 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package utils_test | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/vechain/thor/v2/api/utils" | ||
) | ||
|
||
func TestWrapHandlerFunc(t *testing.T) { | ||
handlerFunc := func(w http.ResponseWriter, r *http.Request) error { | ||
return nil | ||
} | ||
wrapped := utils.WrapHandlerFunc(handlerFunc) | ||
|
||
response := callWrappedFunc(&wrapped) | ||
|
||
assert.Equal(t, http.StatusOK, response.Code) | ||
assert.Equal(t, "", response.Body.String()) | ||
} | ||
|
||
func TestWrapHandlerFuncWithGenericError(t *testing.T) { | ||
genericErrorMsg := "This is a generic error request" | ||
handlerFunc := func(w http.ResponseWriter, r *http.Request) error { | ||
return errors.New(genericErrorMsg) | ||
} | ||
wrapped := utils.WrapHandlerFunc(handlerFunc) | ||
|
||
response := callWrappedFunc(&wrapped) | ||
|
||
assert.Equal(t, http.StatusInternalServerError, response.Code) | ||
assert.Equal(t, genericErrorMsg, strings.TrimSpace(response.Body.String())) | ||
} | ||
|
||
func TestWrapHandlerFuncWithBadRequestError(t *testing.T) { | ||
badMsg := "This is a bad request" | ||
handlerFunc := func(w http.ResponseWriter, r *http.Request) error { | ||
return utils.BadRequest(errors.New(badMsg)) | ||
} | ||
wrapped := utils.WrapHandlerFunc(handlerFunc) | ||
|
||
response := callWrappedFunc(&wrapped) | ||
|
||
assert.Equal(t, http.StatusBadRequest, response.Code) | ||
assert.Equal(t, badMsg, strings.TrimSpace(response.Body.String())) | ||
} | ||
|
||
func TestWrapHandlerFuncWithForbiddenError(t *testing.T) { | ||
forbiddenMsg := "This is a forbidden request" | ||
handlerFunc := func(w http.ResponseWriter, r *http.Request) error { | ||
return utils.Forbidden(errors.New(forbiddenMsg)) | ||
} | ||
wrapped := utils.WrapHandlerFunc(handlerFunc) | ||
|
||
response := callWrappedFunc(&wrapped) | ||
|
||
assert.Equal(t, http.StatusForbidden, response.Code) | ||
assert.Equal(t, forbiddenMsg, strings.TrimSpace(response.Body.String())) | ||
} | ||
|
||
func TestWrapHandlerFuncWithNilCauseError(t *testing.T) { | ||
errorStatus := http.StatusTeapot | ||
handlerFunc := func(w http.ResponseWriter, r *http.Request) error { | ||
return utils.HTTPError(nil, errorStatus) | ||
} | ||
wrapped := utils.WrapHandlerFunc(handlerFunc) | ||
|
||
response := callWrappedFunc(&wrapped) | ||
|
||
assert.Equal(t, errorStatus, response.Code) | ||
assert.Equal(t, "", response.Body.String()) | ||
} | ||
|
||
func callWrappedFunc(wrapped *http.HandlerFunc) *httptest.ResponseRecorder { | ||
req := httptest.NewRequest("GET", "http://example.com", nil) | ||
|
||
responseRec := httptest.NewRecorder() | ||
wrapped.ServeHTTP(responseRec, req) | ||
|
||
return responseRec | ||
} | ||
|
||
type mockReader struct { | ||
Id int | ||
Body string | ||
} | ||
|
||
func TestParseJSON(t *testing.T) { | ||
var parsedRes mockReader | ||
body := mockReader{Id: 1, Body: "test"} | ||
jsonBody, _ := json.Marshal(body) | ||
req := httptest.NewRequest("GET", "http://example.com", bytes.NewReader(jsonBody)) | ||
|
||
err := utils.ParseJSON(req.Body, &parsedRes) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, body, parsedRes) | ||
} | ||
|
||
func TestWriteJSON(t *testing.T) { | ||
rr := httptest.NewRecorder() | ||
var body mockReader | ||
|
||
err := utils.WriteJSON(rr, body) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, rr.Code) | ||
assert.Equal(t, utils.JSONContentType, rr.Header().Get("Content-Type")) | ||
|
||
respObj := mockReader{Id: 1, Body: "test"} | ||
err = json.NewDecoder(rr.Body).Decode(&respObj) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, body.Id, respObj.Id) | ||
assert.Equal(t, body.Body, respObj.Body) | ||
} |