-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix codecov for `rpc/jsonrpc/server: return an error in WriteRPCRespo…
…nseHTTP(Error) (bp #6204) (#6230)`
- Loading branch information
Showing
6 changed files
with
315 additions
and
13 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
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,114 @@ | ||
package server | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/line/ostracon/libs/log" | ||
"github.com/line/ostracon/rpc/jsonrpc/types" | ||
) | ||
|
||
var ( | ||
TestJSONIntID = types.JSONRPCIntID(-1) | ||
TestRPCError = &types.RPCError{} | ||
TestRawMSG = json.RawMessage(`{"p1":"v1"}`) | ||
TestText = "foo" | ||
ErrFoo = errors.New(TestText) | ||
|
||
TestRPCFunc = NewRPCFunc( | ||
func(ctx *types.Context, s string, i int) (string, error) { return TestText, nil }, "s,i") | ||
TestRPCErrorFunc = NewRPCFunc( | ||
func(ctx *types.Context, s string, i int) (string, error) { return "", ErrFoo }, "s,i") | ||
TestWSRPCFunc = NewWSRPCFunc( | ||
func(ctx *types.Context, s string, i int) (string, error) { return TestText, nil }, "s,i") | ||
|
||
TestFuncMap = map[string]*RPCFunc{"c": TestRPCFunc} | ||
TestGoodBody = `{"jsonrpc": "2.0", "method": "c", "id": "0", "params": null}` | ||
TestBadParams = `{"jsonrpc": "2.0", "method": "c", "id": "0", "params": "s=a,i=b"}` | ||
) | ||
|
||
type FailManager struct { | ||
counter int | ||
failedCounter int | ||
throwPanic bool | ||
} | ||
|
||
func (fm *FailManager) checkAndDo( | ||
encounter func() (int, error), | ||
throwing func(), | ||
) (int, error) { | ||
if fm.counter == fm.failedCounter { | ||
fmt.Println("FailManager:do encounter") | ||
return encounter() | ||
} | ||
fm.counter++ | ||
if fm.throwPanic { | ||
fmt.Println("FailManager:do throwing") | ||
throwing() | ||
} | ||
return 0, nil | ||
} | ||
|
||
type FailedWriteResponseWriter struct { | ||
header http.Header | ||
fm *FailManager | ||
code int | ||
error error | ||
} | ||
|
||
func NewFailedWriteResponseWriter() FailedWriteResponseWriter { | ||
return FailedWriteResponseWriter{ | ||
header: make(http.Header), | ||
fm: &FailManager{}, | ||
code: -1, | ||
error: fmt.Errorf("error"), | ||
} | ||
} | ||
func (frw FailedWriteResponseWriter) Header() http.Header { | ||
return frw.header | ||
} | ||
func (frw FailedWriteResponseWriter) Write(buf []byte) (int, error) { | ||
fmt.Println("FailedWriteResponseWriter:" + strconv.Itoa(frw.fm.counter) + ":" + string(buf)) | ||
return frw.fm.checkAndDo( | ||
func() (int, error) { | ||
return frw.code, frw.error | ||
}, | ||
func() { | ||
res := types.RPCResponse{} | ||
res.UnmarshalJSON(buf) // nolint: errcheck | ||
panic(res) | ||
}, | ||
) | ||
} | ||
func (frw FailedWriteResponseWriter) WriteHeader(code int) { | ||
frw.header.Set(http.StatusText(code), strconv.Itoa(code)) | ||
} | ||
|
||
type FailedLogger struct { | ||
fm *FailManager | ||
} | ||
|
||
func NewFailedLogger() FailedLogger { | ||
return FailedLogger{ | ||
fm: &FailManager{}, | ||
} | ||
} | ||
func (l *FailedLogger) Info(msg string, keyvals ...interface{}) { | ||
fmt.Println("FailedLogger.Info:" + msg) | ||
} | ||
func (l *FailedLogger) Debug(msg string, keyvals ...interface{}) { | ||
fmt.Println("FailedLogger.Debug:" + msg) | ||
} | ||
func (l *FailedLogger) Error(msg string, keyvals ...interface{}) { | ||
fmt.Println("FailedLogger.Error:" + strconv.Itoa(l.fm.counter) + ":" + msg) | ||
l.fm.checkAndDo( // nolint: errcheck | ||
func() (int, error) { panic(l.fm.counter) }, | ||
func() {}, | ||
) | ||
} | ||
func (l *FailedLogger) With(keyvals ...interface{}) log.Logger { | ||
return l | ||
} |
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
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
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,68 @@ | ||
package server | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/line/ostracon/libs/log" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMakeHTTPHandler(t *testing.T) { | ||
handlerFunc := makeHTTPHandler(TestRPCFunc, log.TestingLogger()) | ||
req, _ := http.NewRequest("GET", "http://localhost/", strings.NewReader(TestGoodBody)) | ||
rec := httptest.NewRecorder() | ||
handlerFunc(rec, req) | ||
res := rec.Result() | ||
require.Equal(t, http.StatusOK, res.StatusCode) | ||
res.Body.Close() | ||
} | ||
|
||
func TestMakeHTTPHandler_WS_WriteRPCResponseHTTPError_error(t *testing.T) { | ||
handlerFunc := makeHTTPHandler(TestWSRPCFunc, log.TestingLogger()) | ||
req, _ := http.NewRequest("GET", "http://localhost/", nil) | ||
rec := NewFailedWriteResponseWriter() | ||
handlerFunc(rec, req) | ||
assert.Equal(t, | ||
strconv.Itoa(http.StatusNotFound), | ||
rec.Header().Get(http.StatusText(http.StatusNotFound))) | ||
} | ||
|
||
func TestMakeHTTPHandler_httpParamsToArgs_WriteRPCResponseHTTPError_error(t *testing.T) { | ||
handlerFunc := makeHTTPHandler(TestRPCFunc, log.TestingLogger()) | ||
// httpParamsToArgs error | ||
req, _ := http.NewRequest("GET", "http://localhost/c?s=1", nil) | ||
// WriteRPCResponseHTTPError error | ||
rec := NewFailedWriteResponseWriter() | ||
handlerFunc(rec, req) | ||
assert.Equal(t, | ||
strconv.Itoa(http.StatusInternalServerError), | ||
rec.Header().Get(http.StatusText(http.StatusInternalServerError))) | ||
} | ||
|
||
func TestMakeHTTPHandler_unreflectResult_WriteRPCResponseHTTPError_error(t *testing.T) { | ||
// unreflectResult error | ||
handlerFunc := makeHTTPHandler(TestRPCErrorFunc, log.TestingLogger()) | ||
req, _ := http.NewRequest("GET", "http://localhost/", nil) | ||
// WriteRPCResponseHTTPError error | ||
rec := NewFailedWriteResponseWriter() | ||
handlerFunc(rec, req) | ||
assert.Equal(t, | ||
strconv.Itoa(http.StatusInternalServerError), | ||
rec.Header().Get(http.StatusText(http.StatusInternalServerError))) | ||
} | ||
|
||
func TestMakeHTTPHandler_last_WriteRPCResponseHTTP_error(t *testing.T) { | ||
handlerFunc := makeHTTPHandler(TestRPCFunc, log.TestingLogger()) | ||
req, _ := http.NewRequest("GET", "http://localhost/", strings.NewReader(TestGoodBody)) | ||
// WriteRPCResponseHTTP error | ||
rec := NewFailedWriteResponseWriter() | ||
handlerFunc(rec, req) | ||
assert.Equal(t, | ||
strconv.Itoa(http.StatusOK), | ||
rec.Header().Get(http.StatusText(http.StatusOK))) | ||
} |
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