use http.NewServeMux() for testing server
@@ -222,25 +222,26 @@ func TestGitHubPullRequest_Post_Flash_mock(t *testing.T) {
func TestGitHubPullRequest_Post_Flash_review_api(t *testing.T) {
apiCalled := 0
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ mux := http.NewServeMux()
+ mux.HandleFunc("/repos/o/r/pulls/14/comments", func(w http.ResponseWriter, r *http.Request) {
apiCalled++
- switch r.Method {
- case "GET":
- if r.URL.String() != "/repos/o/r/pulls/14/comments" {
+ if r.Method != "GET" {
t.Errorf("unexpected access: %v %v", r.Method, r.URL)
}
cs := []*github.PullRequestComment{
{
Path: github.String("reviewdog.go"),
Position: github.Int(1),
- Body: github.String(bodyPrefix + "\n[reviewdog] already commented"),
+ Body: github.String(bodyPrefix + "\nalready commented"),
},
}
if err := json.NewEncoder(w).Encode(cs); err != nil {
t.Fatal(err)
}
- case "POST":
- if r.URL.String() != "/repos/o/r/pulls/14/reviews" {
+ })
+ mux.HandleFunc("/repos/o/r/pulls/14/reviews", func(w http.ResponseWriter, r *http.Request) {
+ apiCalled++
+ if r.Method != "POST" {
t.Errorf("unexpected access: %v %v", r.Method, r.URL)
}
var req github.PullRequestReviewRequest
@@ -257,20 +258,21 @@ func TestGitHubPullRequest_Post_Flash_review_api(t *testing.T) {
{
Path: github.String("reviewdog.go"),
Position: github.Int(14),
- Body: github.String(bodyPrefix + "\n[reviewdog] new comment"),
+ Body: github.String(bodyPrefix + "\nnew comment"),
},
}
if diff := pretty.Compare(want, req.Comments); diff != "" {
t.Errorf("req.Comments diff: (-got +want)\n%s", diff)
}
- default:
- t.Errorf("unexpected access: %v %v", r.Method, r.URL)
- }
- }))
+ })
+ ts := httptest.NewServer(mux)
defer ts.Close()
+
+ // modify githubAPIHost to use GitHub Review API
defer func(h string) { githubAPIHost = h }(githubAPIHost)
u, _ := url.Parse(ts.URL)
githubAPIHost = u.Host
+
cli := github.NewClient(nil)
cli.BaseURL, _ = url.Parse(ts.URL)
g := NewGitHubPullReqest(cli, "o", "r", 14, "")
@@ -280,14 +282,14 @@ func TestGitHubPullRequest_Post_Flash_review_api(t *testing.T) {
Path: "reviewdog.go",
},
LnumDiff: 1,
- Body: "[reviewdog] already commented",
+ Body: "already commented",
},
{
CheckResult: &CheckResult{
Path: "reviewdog.go",
},
LnumDiff: 14,
- Body: "[reviewdog] new comment",
+ Body: "new comment",
},
}
for _, c := range comments {
