2019-11-24 06:37:56 +05:30
|
|
|
package mojangtextures
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2020-02-08 15:58:10 +05:30
|
|
|
. "net/url"
|
2019-11-24 06:37:56 +05:30
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/h2non/gock"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type remoteApiUuidsProviderTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
Provider *RemoteApiUuidsProvider
|
2020-02-08 15:58:10 +05:30
|
|
|
Emitter *mockEmitter
|
2019-11-24 06:37:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *remoteApiUuidsProviderTestSuite) SetupSuite() {
|
|
|
|
client := &http.Client{}
|
|
|
|
gock.InterceptClient(client)
|
|
|
|
|
|
|
|
HttpClient = client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *remoteApiUuidsProviderTestSuite) SetupTest() {
|
2020-02-08 15:58:10 +05:30
|
|
|
suite.Emitter = &mockEmitter{}
|
2019-11-24 06:37:56 +05:30
|
|
|
suite.Provider = &RemoteApiUuidsProvider{
|
2020-02-08 15:58:10 +05:30
|
|
|
Emitter: suite.Emitter,
|
2019-11-24 06:37:56 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *remoteApiUuidsProviderTestSuite) TearDownTest() {
|
2020-02-08 15:58:10 +05:30
|
|
|
suite.Emitter.AssertExpectations(suite.T())
|
2019-11-24 06:37:56 +05:30
|
|
|
gock.Off()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteApiUuidsProvider(t *testing.T) {
|
|
|
|
suite.Run(t, new(remoteApiUuidsProviderTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *remoteApiUuidsProviderTestSuite) TestGetUuidForValidUsername() {
|
2020-02-08 15:58:10 +05:30
|
|
|
suite.Emitter.On("Emit", "mojang_textures:remote_api_uuids_provider:before_request", "http://example.com/subpath/username").Once()
|
|
|
|
suite.Emitter.On("Emit",
|
|
|
|
"mojang_textures:remote_api_uuids_provider:after_request",
|
|
|
|
mock.AnythingOfType("*http.Response"),
|
|
|
|
nil,
|
|
|
|
).Once()
|
2019-11-24 06:37:56 +05:30
|
|
|
|
|
|
|
gock.New("http://example.com").
|
|
|
|
Get("/subpath/username").
|
|
|
|
Reply(200).
|
|
|
|
JSON(map[string]interface{}{
|
|
|
|
"id": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
|
"name": "username",
|
|
|
|
})
|
|
|
|
|
|
|
|
suite.Provider.Url = shouldParseUrl("http://example.com/subpath")
|
|
|
|
result, err := suite.Provider.GetUuid("username")
|
|
|
|
|
|
|
|
assert := suite.Assert()
|
|
|
|
if assert.NoError(err) {
|
|
|
|
assert.Equal("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", result.Id)
|
|
|
|
assert.Equal("username", result.Name)
|
|
|
|
assert.False(result.IsLegacy)
|
|
|
|
assert.False(result.IsDemo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *remoteApiUuidsProviderTestSuite) TestGetUuidForNotExistsUsername() {
|
2020-02-08 15:58:10 +05:30
|
|
|
suite.Emitter.On("Emit", "mojang_textures:remote_api_uuids_provider:before_request", "http://example.com/subpath/username").Once()
|
|
|
|
suite.Emitter.On("Emit",
|
|
|
|
"mojang_textures:remote_api_uuids_provider:after_request",
|
|
|
|
mock.AnythingOfType("*http.Response"),
|
|
|
|
nil,
|
|
|
|
).Once()
|
2019-11-24 06:37:56 +05:30
|
|
|
|
|
|
|
gock.New("http://example.com").
|
|
|
|
Get("/subpath/username").
|
|
|
|
Reply(204)
|
|
|
|
|
|
|
|
suite.Provider.Url = shouldParseUrl("http://example.com/subpath")
|
|
|
|
result, err := suite.Provider.GetUuid("username")
|
|
|
|
|
|
|
|
assert := suite.Assert()
|
|
|
|
assert.Nil(result)
|
|
|
|
assert.Nil(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *remoteApiUuidsProviderTestSuite) TestGetUuidForNon20xResponse() {
|
2020-02-08 15:58:10 +05:30
|
|
|
suite.Emitter.On("Emit", "mojang_textures:remote_api_uuids_provider:before_request", "http://example.com/subpath/username").Once()
|
|
|
|
suite.Emitter.On("Emit",
|
|
|
|
"mojang_textures:remote_api_uuids_provider:after_request",
|
|
|
|
mock.AnythingOfType("*http.Response"),
|
|
|
|
nil,
|
|
|
|
).Once()
|
2019-11-24 06:37:56 +05:30
|
|
|
|
|
|
|
gock.New("http://example.com").
|
|
|
|
Get("/subpath/username").
|
|
|
|
Reply(504).
|
|
|
|
BodyString("504 Gateway Timeout")
|
|
|
|
|
|
|
|
suite.Provider.Url = shouldParseUrl("http://example.com/subpath")
|
|
|
|
result, err := suite.Provider.GetUuid("username")
|
|
|
|
|
|
|
|
assert := suite.Assert()
|
|
|
|
assert.Nil(result)
|
|
|
|
assert.EqualError(err, "Unexpected remote api response")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *remoteApiUuidsProviderTestSuite) TestGetUuidForNotSuccessRequest() {
|
2020-02-08 15:58:10 +05:30
|
|
|
suite.Emitter.On("Emit", "mojang_textures:remote_api_uuids_provider:before_request", "http://example.com/subpath/username").Once()
|
|
|
|
suite.Emitter.On("Emit",
|
|
|
|
"mojang_textures:remote_api_uuids_provider:after_request",
|
|
|
|
mock.AnythingOfType("*http.Response"),
|
|
|
|
mock.AnythingOfType("*url.Error"),
|
|
|
|
).Once()
|
2019-11-24 06:37:56 +05:30
|
|
|
|
|
|
|
expectedError := &net.OpError{Op: "dial"}
|
|
|
|
|
|
|
|
gock.New("http://example.com").
|
|
|
|
Get("/subpath/username").
|
|
|
|
ReplyError(expectedError)
|
|
|
|
|
|
|
|
suite.Provider.Url = shouldParseUrl("http://example.com/subpath")
|
|
|
|
result, err := suite.Provider.GetUuid("username")
|
|
|
|
|
|
|
|
assert := suite.Assert()
|
|
|
|
assert.Nil(result)
|
|
|
|
if assert.Error(err) {
|
2020-02-08 15:58:10 +05:30
|
|
|
assert.IsType(&Error{}, err)
|
|
|
|
casterErr, _ := err.(*Error)
|
2019-11-24 06:37:56 +05:30
|
|
|
assert.Equal(expectedError, casterErr.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *remoteApiUuidsProviderTestSuite) TestGetUuidForInvalidSuccessResponse() {
|
2020-02-08 15:58:10 +05:30
|
|
|
suite.Emitter.On("Emit", "mojang_textures:remote_api_uuids_provider:before_request", "http://example.com/subpath/username").Once()
|
|
|
|
suite.Emitter.On("Emit",
|
|
|
|
"mojang_textures:remote_api_uuids_provider:after_request",
|
|
|
|
mock.AnythingOfType("*http.Response"),
|
|
|
|
nil,
|
|
|
|
).Once()
|
2019-11-24 06:37:56 +05:30
|
|
|
|
|
|
|
gock.New("http://example.com").
|
|
|
|
Get("/subpath/username").
|
|
|
|
Reply(200).
|
|
|
|
BodyString("completely not json")
|
|
|
|
|
|
|
|
suite.Provider.Url = shouldParseUrl("http://example.com/subpath")
|
|
|
|
result, err := suite.Provider.GetUuid("username")
|
|
|
|
|
|
|
|
assert := suite.Assert()
|
|
|
|
assert.Nil(result)
|
|
|
|
assert.Error(err)
|
|
|
|
}
|
|
|
|
|
2020-02-08 15:58:10 +05:30
|
|
|
func shouldParseUrl(rawUrl string) URL {
|
|
|
|
url, err := Parse(rawUrl)
|
2019-11-24 06:37:56 +05:30
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return *url
|
|
|
|
}
|