-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathfocus_server_test.go
More file actions
90 lines (79 loc) · 2.38 KB
/
focus_server_test.go
File metadata and controls
90 lines (79 loc) · 2.38 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"encoding/json"
"net/http/httptest"
"strings"
"testing"
)
func TestHandleRoundComplete_RejectedInRange(t *testing.T) {
s, sess := newTestServer(t)
sess.mu.Lock()
sess.Focus = Focus{Kind: FocusRange, BaseSHA: "b", HeadSHA: "h", DiffScope: DiffScopeLayer}
sess.mu.Unlock()
req := httptest.NewRequest("POST", "/api/round-complete", nil)
w := httptest.NewRecorder()
s.ServeHTTP(w, req)
if w.Code != 409 {
t.Errorf("status = %d, want 409", w.Code)
}
var body map[string]string
if err := json.NewDecoder(w.Body).Decode(&body); err != nil {
t.Fatal(err)
}
if !strings.Contains(body["error"], "round-complete is not meaningful in range mode") {
t.Errorf("error body: %q", body["error"])
}
}
func TestHandleFocus_FullStackRejectedWithoutDefaultSHA(t *testing.T) {
s, _ := newTestServer(t)
body := `{"kind":"range","base_sha":"b","head_sha":"h","diff_scope":"full_stack"}`
req := httptest.NewRequest("POST", "/api/focus", strings.NewReader(body))
w := httptest.NewRecorder()
s.ServeHTTP(w, req)
if w.Code != 400 {
t.Errorf("status = %d, want 400 (got body: %q)", w.Code, w.Body.String())
}
}
func TestHandleFocus_MethodNotAllowed(t *testing.T) {
s, _ := newTestServer(t)
req := httptest.NewRequest("GET", "/api/focus", nil)
w := httptest.NewRecorder()
s.ServeHTTP(w, req)
if w.Code != 405 {
t.Errorf("status = %d, want 405", w.Code)
}
}
func TestHandleFocus_BadJSON(t *testing.T) {
s, _ := newTestServer(t)
req := httptest.NewRequest("POST", "/api/focus", strings.NewReader(`not json`))
w := httptest.NewRecorder()
s.ServeHTTP(w, req)
if w.Code != 400 {
t.Errorf("status = %d, want 400", w.Code)
}
}
func TestSessionInfo_IncludesFocus(t *testing.T) {
s, sess := newTestServer(t)
sess.mu.Lock()
sess.Focus = Focus{Kind: FocusRange, BaseSHA: "b", HeadSHA: "h", DiffScope: DiffScopeLayer, IsStacked: true}
sess.mu.Unlock()
req := httptest.NewRequest("GET", "/api/session", nil)
w := httptest.NewRecorder()
s.ServeHTTP(w, req)
if w.Code != 200 {
t.Fatalf("status = %d", w.Code)
}
var info SessionInfo
if err := json.NewDecoder(w.Body).Decode(&info); err != nil {
t.Fatal(err)
}
if info.Focus.Kind != FocusRange {
t.Errorf("Focus.Kind = %q, want range", info.Focus.Kind)
}
if info.Focus.HeadSHA != "h" {
t.Errorf("Focus.HeadSHA = %q, want h", info.Focus.HeadSHA)
}
if !info.Focus.IsStacked {
t.Errorf("Focus.IsStacked = false, want true")
}
}