github.com/mistwind/reviewdog@v0.0.0-20230322024206-9cfa11856d58/doghouse/client/client_test.go (about) 1 package client 2 3 import ( 4 "context" 5 "net/http" 6 "net/http/httptest" 7 "net/url" 8 "testing" 9 10 "github.com/mistwind/reviewdog/doghouse" 11 ) 12 13 func TestDogHouseClient_Check(t *testing.T) { 14 mux := http.NewServeMux() 15 mux.HandleFunc("/check", func(w http.ResponseWriter, r *http.Request) { 16 if r.Method != http.MethodPost { 17 t.Errorf("unexpected access: %v %v", r.Method, r.URL) 18 } 19 w.Write([]byte(`{"report_url": "http://report_url"}`)) 20 }) 21 ts := httptest.NewServer(mux) 22 defer ts.Close() 23 24 cli := New(nil) 25 cli.BaseURL, _ = url.Parse(ts.URL) 26 27 req := &doghouse.CheckRequest{} 28 resp, err := cli.Check(context.Background(), req) 29 if err != nil { 30 t.Fatal(err) 31 } 32 if resp.ReportURL != "http://report_url" { 33 t.Errorf("got unexpected response: %v", resp) 34 } 35 } 36 37 func TestDogHouseClient_Check_failure(t *testing.T) { 38 mux := http.NewServeMux() 39 mux.HandleFunc("/check", func(w http.ResponseWriter, r *http.Request) { 40 w.WriteHeader(http.StatusBadRequest) 41 w.Write([]byte(`bad request!`)) 42 }) 43 ts := httptest.NewServer(mux) 44 defer ts.Close() 45 46 cli := New(nil) 47 cli.BaseURL, _ = url.Parse(ts.URL) 48 49 req := &doghouse.CheckRequest{} 50 _, err := cli.Check(context.Background(), req) 51 if err == nil { 52 t.Error("got no error, but want bad request error") 53 } 54 }