Skip to content

Commit

Permalink
Improve FrankerFaceZ tests (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada authored Mar 26, 2022
1 parent 13b901e commit f05cba0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Dev: Improve migration tests. (#290)
- Dev: Improve Twitter tests. (#293)
- Dev: Improve SevenTV tests. (#294)
- Dev: Improve FrankerFaceZ tests. (#295)

## 1.2.3

Expand Down
2 changes: 1 addition & 1 deletion internal/resolvers/frankerfacez/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func testServer() *httptest.Server {

w.Header().Set("Content-Type", "application/json")

if emote == "bad_json" {
if emote == "696969" {
w.Write([]byte("xD"))
} else if e, ok := data[emote]; ok {
response.Emote = *e
Expand Down
11 changes: 2 additions & 9 deletions internal/resolvers/frankerfacez/emote_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
Expand Down Expand Up @@ -117,19 +116,13 @@ func (l *EmoteLoader) Load(ctx context.Context, emoteID string, r *http.Request)
return emoteNotFoundResponse, cache.NoSpecialDur, nil
}

// Read response into a string
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return resolver.Errorf("FrankerFaceZ HTTP body read error: %s", err)
}

// Parse response into a predefined JSON blob (see EmoteAPIResponse struct above)
var temp struct {
Emote EmoteAPIResponse `json:"emote"`
}

if err := json.Unmarshal(body, &temp); err != nil {
return resolver.Errorf("FrankerFaceZ API unmarshal error: %s", err)
if err := json.NewDecoder(resp.Body).Decode(&temp); err != nil {
return resolver.Errorf("FrankerFaceZ API response decode error: %s", err)
}
jsonResponse := temp.Emote

Expand Down
8 changes: 8 additions & 0 deletions internal/resolvers/frankerfacez/emote_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ func TestEmoteResolver(t *testing.T) {
expectedBytes: []byte(`{"status":404,"message":"No FrankerFaceZ emote with this id found"}`),
expectedError: nil,
},
{
label: "Matching link - bad json",
inputURL: utils.MustParseURL("https://frankerfacez.com/emoticon/696969-badjson"),
inputEmoteHash: "696969",
inputReq: nil,
expectedBytes: []byte(`{"status":500,"message":"FrankerFaceZ API response decode error: invalid character \u0026#39;x\u0026#39; looking for beginning of value"}`),
expectedError: nil,
},
}

const q = `SELECT value FROM cache WHERE key=$1`
Expand Down

0 comments on commit f05cba0

Please sign in to comment.