-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Coverage for: api/gin + addressGroup #5175
Merged
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8f3646a
extended coverage on api/gin and api/groups/addressGroup
sstanculeanu ac362a9
fix linter
sstanculeanu 0d4d07b
extra tests for addressGroupOptions
sstanculeanu 212584d
fix test name
sstanculeanu 792647c
fix after review, error name
sstanculeanu 1c684e9
Merge branch 'rc/v1.5.0' into api_tests
sstanculeanu a136b77
Merge branch 'rc/v1.6.0' of https://github.com/multiversx/mx-chain-go…
sstanculeanu 3ba18fa
Merge branch 'rc/v1.6.0' into api_tests
sstanculeanu 1c56539
fixes after review
sstanculeanu c117bbf
Merge branch 'rc/v1.6.0' into api_tests
sstanculeanu 2b3af3c
Merge branch 'rc/v1.6.0' into api_tests
sstanculeanu a354a35
Merge branch 'rc/v1.6.0' into api_tests
sstanculeanu File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,17 +1,92 @@ | ||
package gin | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/multiversx/mx-chain-core-go/core/check" | ||
"github.com/multiversx/mx-chain-go/api/errors" | ||
apiErrors "github.com/multiversx/mx-chain-go/api/errors" | ||
"github.com/multiversx/mx-chain-go/testscommon/api" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewHttpServer_NilServerShouldErr(t *testing.T) { | ||
func TestNewHttpServer(t *testing.T) { | ||
t.Parallel() | ||
|
||
hs, err := NewHttpServer(nil) | ||
require.Equal(t, errors.ErrNilHttpServer, err) | ||
require.True(t, check.IfNil(hs)) | ||
t.Run("nil server should error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
hs, err := NewHttpServer(nil) | ||
require.Equal(t, apiErrors.ErrNilHttpServer, err) | ||
require.Nil(t, hs) | ||
}) | ||
t.Run("should work", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
hs, err := NewHttpServer(&api.ServerStub{}) | ||
require.NoError(t, err) | ||
require.NotNil(t, hs) | ||
}) | ||
} | ||
|
||
func TestHttpServer_Start(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("server starts", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
wasCalled := false | ||
serverStub := &api.ServerStub{ | ||
ListenAndServeCalled: func() error { | ||
return nil | ||
}, | ||
ShutdownCalled: func(ctx context.Context) error { | ||
wasCalled = true | ||
return nil | ||
}, | ||
} | ||
hs, _ := NewHttpServer(serverStub) | ||
require.NotNil(t, hs) | ||
|
||
hs.Start() | ||
require.NoError(t, hs.Close()) | ||
require.True(t, wasCalled) | ||
}) | ||
t.Run("server is closed", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
serverStub := &api.ServerStub{ | ||
ListenAndServeCalled: func() error { | ||
return http.ErrServerClosed | ||
}, | ||
} | ||
hs, _ := NewHttpServer(serverStub) | ||
require.NotNil(t, hs) | ||
|
||
hs.Start() | ||
}) | ||
t.Run("server returns other error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
serverStub := &api.ServerStub{ | ||
ListenAndServeCalled: func() error { | ||
return errors.New("other error") | ||
}, | ||
} | ||
hs, _ := NewHttpServer(serverStub) | ||
require.NotNil(t, hs) | ||
|
||
hs.Start() | ||
}) | ||
} | ||
|
||
func TestHttpServer_IsInterfaceNil(t *testing.T) { | ||
t.Parallel() | ||
|
||
var hs *httpServer | ||
require.True(t, hs.IsInterfaceNil()) | ||
|
||
hs, _ = NewHttpServer(&api.ServerStub{}) | ||
require.False(t, hs.IsInterfaceNil()) | ||
} |
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 |
---|---|---|
@@ -1,6 +1,13 @@ | ||
package gin | ||
|
||
import "context" | ||
|
||
type resetHandler interface { | ||
Reset() | ||
IsInterfaceNil() bool | ||
} | ||
|
||
type server interface { | ||
ListenAndServe() error | ||
Shutdown(ctx context.Context) error | ||
} |
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,176 @@ | ||
package gin | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
apiErrors "github.com/multiversx/mx-chain-go/api/errors" | ||
"github.com/multiversx/mx-chain-go/api/middleware" | ||
"github.com/multiversx/mx-chain-go/api/mock" | ||
"github.com/multiversx/mx-chain-go/api/shared" | ||
"github.com/multiversx/mx-chain-go/config" | ||
"github.com/multiversx/mx-chain-go/facade" | ||
"github.com/multiversx/mx-chain-go/testscommon/api" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func createMockArgsNewWebServer() ArgsNewWebServer { | ||
return ArgsNewWebServer{ | ||
Facade: &mock.FacadeStub{ | ||
PprofEnabledCalled: func() bool { | ||
return true // coverage | ||
}, | ||
}, | ||
ApiConfig: config.ApiRoutesConfig{ | ||
Logging: config.ApiLoggingConfig{ | ||
LoggingEnabled: true, | ||
ThresholdInMicroSeconds: 10, | ||
}, | ||
APIPackages: map[string]config.APIPackageConfig{ | ||
"log": {Routes: []config.RouteConfig{ | ||
{ | ||
Name: "/log", | ||
Open: true, | ||
}, | ||
}}, | ||
}, | ||
}, | ||
AntiFloodConfig: config.WebServerAntifloodConfig{ | ||
WebServerAntifloodEnabled: true, | ||
SimultaneousRequests: 1, | ||
SameSourceRequests: 1, | ||
SameSourceResetIntervalInSec: 1, | ||
}, | ||
} | ||
} | ||
|
||
func TestNewGinWebServerHandler(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("nil facade should error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
args := createMockArgsNewWebServer() | ||
args.Facade = nil | ||
|
||
ws, err := NewGinWebServerHandler(args) | ||
require.True(t, errors.Is(err, apiErrors.ErrCannotCreateGinWebServer)) | ||
require.True(t, strings.Contains(err.Error(), apiErrors.ErrNilFacadeHandler.Error())) | ||
require.Nil(t, ws) | ||
}) | ||
t.Run("should work", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ws, err := NewGinWebServerHandler(createMockArgsNewWebServer()) | ||
require.Nil(t, err) | ||
require.NotNil(t, ws) | ||
}) | ||
} | ||
|
||
func TestWebServer_StartHttpServer(t *testing.T) { | ||
t.Run("RestApiInterface returns DefaultRestPortOff", func(t *testing.T) { | ||
args := createMockArgsNewWebServer() | ||
args.Facade = &mock.FacadeStub{ | ||
RestApiInterfaceCalled: func() string { | ||
return facade.DefaultRestPortOff | ||
}, | ||
} | ||
|
||
ws, _ := NewGinWebServerHandler(args) | ||
require.NotNil(t, ws) | ||
|
||
err := ws.StartHttpServer() | ||
require.Nil(t, err) | ||
}) | ||
t.Run("createMiddlewareLimiters returns error due to middleware.NewSourceThrottler error", func(t *testing.T) { | ||
args := createMockArgsNewWebServer() | ||
args.AntiFloodConfig.SameSourceRequests = 0 | ||
ws, _ := NewGinWebServerHandler(args) | ||
require.NotNil(t, ws) | ||
|
||
err := ws.StartHttpServer() | ||
require.Equal(t, middleware.ErrInvalidMaxNumRequests, err) | ||
}) | ||
t.Run("createMiddlewareLimiters returns error due to middleware.NewGlobalThrottler error", func(t *testing.T) { | ||
args := createMockArgsNewWebServer() | ||
args.AntiFloodConfig.SimultaneousRequests = 0 | ||
ws, _ := NewGinWebServerHandler(args) | ||
require.NotNil(t, ws) | ||
|
||
err := ws.StartHttpServer() | ||
require.Equal(t, middleware.ErrInvalidMaxNumRequests, err) | ||
}) | ||
t.Run("should work", func(t *testing.T) { | ||
ws, _ := NewGinWebServerHandler(createMockArgsNewWebServer()) | ||
require.NotNil(t, ws) | ||
|
||
err := ws.StartHttpServer() | ||
require.Nil(t, err) | ||
|
||
time.Sleep(2 * time.Second) | ||
|
||
client := &http.Client{} | ||
req, err := http.NewRequest("GET", "http://127.0.0.1:8080/log", nil) | ||
require.Nil(t, err) | ||
|
||
req.Header.Set("Sec-Websocket-Version", "13") | ||
req.Header.Set("Connection", "upgrade") | ||
req.Header.Set("Upgrade", "websocket") | ||
req.Header.Set("Sec-Websocket-Key", "key") | ||
|
||
resp, err := client.Do(req) | ||
require.Nil(t, err) | ||
|
||
err = resp.Body.Close() | ||
require.Nil(t, err) | ||
|
||
time.Sleep(2 * time.Second) | ||
err = ws.Close() | ||
require.Nil(t, err) | ||
}) | ||
} | ||
|
||
func TestWebServer_UpdateFacade(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("update with nil facade should error", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ws, _ := NewGinWebServerHandler(createMockArgsNewWebServer()) | ||
require.NotNil(t, ws) | ||
|
||
err := ws.UpdateFacade(nil) | ||
require.Equal(t, apiErrors.ErrNilFacadeHandler, err) | ||
}) | ||
t.Run("should work - one of the groupHandlers returns err", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
args := createMockArgsNewWebServer() | ||
args.Facade = &mock.FacadeStub{ | ||
RestApiInterfaceCalled: func() string { | ||
return "provided interface" | ||
}, | ||
} | ||
|
||
ws, _ := NewGinWebServerHandler(args) | ||
require.NotNil(t, ws) | ||
|
||
ws.groups = make(map[string]shared.GroupHandler) | ||
ws.groups["first"] = &api.GroupHandlerStub{ | ||
UpdateFacadeCalled: func(newFacade interface{}) error { | ||
return errors.New("error") | ||
}, | ||
} | ||
ws.groups["second"] = &api.GroupHandlerStub{ | ||
UpdateFacadeCalled: func(newFacade interface{}) error { | ||
return nil | ||
}, | ||
} | ||
|
||
err := ws.UpdateFacade(&mock.FacadeStub{}) | ||
require.Nil(t, err) | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's remove the err handler usage and have this as: