diff --git a/instance_test.go b/instance_test.go new file mode 100644 index 0000000..6724b53 --- /dev/null +++ b/instance_test.go @@ -0,0 +1,40 @@ +package mastodon + +import ( + "context" + "fmt" + "net/http" + "net/http/httptest" + "testing" +) + +func TestGetInstance(t *testing.T) { + canErr := true + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if canErr { + canErr = false + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + fmt.Fprintln(w, `{"title": "mastodon"}`) + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + _, err := client.GetInstance(context.Background()) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + ins, err := client.GetInstance(context.Background()) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if ins.Title != "mastodon" { + t.Fatalf("want %q but %q", "mastodon", ins.Title) + } +}