diff --git a/status_test.go b/status_test.go index 2cac4b8..b3f5e8c 100644 --- a/status_test.go +++ b/status_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "os" + "strings" "testing" ) @@ -37,6 +38,45 @@ func TestGetFavourites(t *testing.T) { } } +func TestGetFavouritesSavedJSON(t *testing.T) { + ourJSON := `[{"content": "foo"}, {"content": "bar"}]` + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, ourJSON) + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + + client.SaveJSON = true + + favs, err := client.GetFavourites(context.Background(), nil) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if len(favs) != 2 { + t.Fatalf("result should be two: %d", len(favs)) + } + if favs[0].Content != "foo" { + t.Fatalf("want %q but %q", "foo", favs[0].Content) + } + if favs[1].Content != "bar" { + t.Fatalf("want %q but %q", "bar", favs[1].Content) + } + + // We get a trailing `\n` from the API which we need to trim + // off before we compare it with our literal above. + theirJSON := strings.TrimSpace(string(client.LastJSON)) + + if theirJSON != ourJSON { + t.Fatalf("want %q but %q", ourJSON, theirJSON) + } +} + func TestGetBookmarks(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, `[{"content": "foo"}, {"content": "bar"}]`)