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