From 658008fe5daca0eb29ff02ccbeb1f8d4ea977241 Mon Sep 17 00:00:00 2001 From: cxkoda Date: Tue, 17 Jan 2023 12:05:37 +0100 Subject: [PATCH] Disable autogenerated image filed if animation URL is set (#75) * Disable autogenerated image filed if animation URL is set * Extend error message * Push minor version --- erc721/server.go | 2 +- erc721/server_test.go | 78 +++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/erc721/server.go b/erc721/server.go index 8c188e6..9214a79 100644 --- a/erc721/server.go +++ b/erc721/server.go @@ -202,7 +202,7 @@ func (s *Server) metadata(handler MetadataHandler) httprouter.Handle { return nil, "", code, err } - if md.Image == "" && len(s.Image) > 0 { + if md.Image == "" && len(s.Image) > 0 && md.AnimationURL == "" { img := *s.BaseURL img.Path = strings.ReplaceAll(s.Image[0].Path, fullTokenIDParam, id.Text(s.tokenIDBase())) md.Image = img.String() diff --git a/erc721/server_test.go b/erc721/server_test.go index 0242a4b..bbb2cbe 100644 --- a/erc721/server_test.go +++ b/erc721/server_test.go @@ -333,3 +333,81 @@ func TestMultipleMetadataEndpoints(t *testing.T) { } } } + +func TestInternalImage(t *testing.T) { + tests := []struct { + name string + metadataHandler MetadataHandler + wantImage func(baseURL string, id int) string + }{ + { + name: "Empty", + metadataHandler: func(_ Interface, id *TokenID, _ httprouter.Params) (*Metadata, int, error) { + md := Metadata{ + Name: fmt.Sprintf("DEFAULT %s", id), + } + return &md, 200, nil + }, + wantImage: func(baseURL string, id int) string { + return fmt.Sprintf("%s/images/%d", baseURL, id) + }, + }, + { + name: "With animation", + metadataHandler: func(_ Interface, id *TokenID, _ httprouter.Params) (*Metadata, int, error) { + md := Metadata{ + Name: fmt.Sprintf("DEFAULT %s", id), + AnimationURL: fmt.Sprintf("foo://bar/%s", id), + } + return &md, 200, nil + }, + wantImage: func(baseURL string, id int) string { + return "" + }, + }, + { + name: "With image", + metadataHandler: func(_ Interface, id *TokenID, _ httprouter.Params) (*Metadata, int, error) { + md := Metadata{ + Name: fmt.Sprintf("DEFAULT %s", id), + Image: fmt.Sprintf("foo://bar/%s", id), + } + return &md, 200, nil + }, + wantImage: func(baseURL string, id int) string { + return fmt.Sprintf("foo://bar/%d", id) + }, + }, + } + + for _, tt := range tests { + + t.Run(tt.name, func(t *testing.T) { + srv := &Server{ + Metadata: []MetadataEndpoint{ + { + Path: "/metadata/:tokenId", + Handler: tt.metadataHandler, + }, + }, + Image: []ImageEndpoint{ + { + Path: "/images/:tokenId", + }, + }, + } + baseURL := start(t, srv) + + for id := 0; id < 20; id++ { + t.Run(fmt.Sprintf("token %d", id), func(t *testing.T) { + url := fmt.Sprintf("%s/metadata/%d", baseURL, id) + got := metadataFromResponse(t, httpGet(t, url)) + + if diff := cmp.Diff(tt.wantImage(baseURL, id), got.Image); diff != "" { + t.Errorf("Metadata.Image (-want +got):\n%s", diff) + } + }) + } + }) + } +} diff --git a/package.json b/package.json index 3b1cba9..3dc14af 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@divergencetech/ethier", - "version": "0.47.0", + "version": "0.48.0", "description": "Golang and Solidity SDK to make Ethereum development ethier", "main": "\"\"", "scripts": {