Skip to content

Commit

Permalink
Disable autogenerated image filed if animation URL is set (#75)
Browse files Browse the repository at this point in the history
* Disable autogenerated image filed if animation URL is set

* Extend error message

* Push minor version
  • Loading branch information
cxkoda authored Jan 17, 2023
1 parent ecf778f commit 658008f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
2 changes: 1 addition & 1 deletion erc721/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
78 changes: 78 additions & 0 deletions erc721/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
})
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 658008f

Please sign in to comment.