Restore Ely faces API

This commit is contained in:
ErickSkrauch 2018-02-16 18:37:06 +03:00
parent d7bc77e5a7
commit 31cd75ffa7
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
3 changed files with 82 additions and 0 deletions

27
http/face.go Normal file
View File

@ -0,0 +1,27 @@
package http
import (
"net/http"
"github.com/gorilla/mux"
)
const defaultHash = "default"
func (cfg *Config) Face(response http.ResponseWriter, request *http.Request) {
cfg.Logger.IncCounter("faces.request", 1)
username := parseUsername(mux.Vars(request)["username"])
rec, err := cfg.SkinsRepo.FindByUsername(username)
var hash string
if err != nil || rec.SkinId == 0 {
hash = defaultHash
} else {
hash = rec.Hash
}
http.Redirect(response, request, buildFaceUrl(hash), 301)
}
func buildFaceUrl(hash string) string {
return "http://ely.by/minecraft/skin_buffer/faces/" + hash + ".png"
}

53
http/face_test.go Normal file
View File

@ -0,0 +1,53 @@
package http
import (
"net/http/httptest"
"testing"
"github.com/golang/mock/gomock"
testify "github.com/stretchr/testify/assert"
"github.com/elyby/chrly/db"
)
func TestConfig_Face(t *testing.T) {
assert := testify.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config, mocks := setupMocks(ctrl)
mocks.Skins.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil)
mocks.Log.EXPECT().IncCounter("faces.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins/mock_user/face.png", nil)
w := httptest.NewRecorder()
config.CreateHandler().ServeHTTP(w, req)
resp := w.Result()
assert.Equal(301, resp.StatusCode)
assert.Equal("http://ely.by/minecraft/skin_buffer/faces/55d2a8848764f5ff04012cdb093458bd.png", resp.Header.Get("Location"))
}
func TestConfig_Face2(t *testing.T) {
assert := testify.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config, mocks := setupMocks(ctrl)
mocks.Skins.EXPECT().FindByUsername("mock_user").Return(nil, &db.SkinNotFoundError{"mock_user"})
mocks.Log.EXPECT().IncCounter("faces.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins/mock_user/face.png", nil)
w := httptest.NewRecorder()
config.CreateHandler().ServeHTTP(w, req)
resp := w.Result()
assert.Equal(301, resp.StatusCode)
assert.Equal("http://ely.by/minecraft/skin_buffer/faces/default.png", resp.Header.Get("Location"))
}

View File

@ -55,6 +55,8 @@ func (cfg *Config) CreateHandler() http.Handler {
router.HandleFunc("/cloaks/{username}", cfg.Cape).Methods("GET").Name("cloaks")
router.HandleFunc("/textures/{username}", cfg.Textures).Methods("GET")
router.HandleFunc("/textures/signed/{username}", cfg.SignedTextures).Methods("GET")
router.HandleFunc("/skins/{username}/face", cfg.Face).Methods("GET")
router.HandleFunc("/skins/{username}/face.png", cfg.Face).Methods("GET")
// Legacy
router.HandleFunc("/skins", cfg.SkinGET).Methods("GET")
router.HandleFunc("/cloaks", cfg.CapeGET).Methods("GET")