Browse code

Add tests for the api

Victor Vieux authored on 2013/07/18 23:54:58
Showing 2 changed files
... ...
@@ -89,6 +89,44 @@ func TestGetInfo(t *testing.T) {
89 89
 	}
90 90
 }
91 91
 
92
+func TestGetEvents(t *testing.T) {
93
+	runtime := mkRuntime(t)
94
+	srv := &Server{
95
+		runtime:   runtime,
96
+		events:    make([]utils.JSONMessage, 0, 64),
97
+		listeners: make(map[string]chan utils.JSONMessage),
98
+	}
99
+
100
+	srv.LogEvent("fakeaction", "fakeid")
101
+	srv.LogEvent("fakeaction2", "fakeid")
102
+
103
+	req, err := http.NewRequest("GET", "/events?since=1", nil)
104
+	if err != nil {
105
+		t.Fatal(err)
106
+	}
107
+
108
+	r := httptest.NewRecorder()
109
+	setTimeout(t, "", 500*time.Millisecond, func() {
110
+		if err := getEvents(srv, APIVERSION, r, req, nil); err != nil {
111
+			t.Fatal(err)
112
+		}
113
+	})
114
+
115
+	dec := json.NewDecoder(r.Body)
116
+	for i := 0; i < 2; i++ {
117
+		var jm utils.JSONMessage
118
+		if err := dec.Decode(&jm); err == io.EOF {
119
+			break
120
+		} else if err != nil {
121
+			t.Fatal(err)
122
+		}
123
+		if jm != srv.events[i] {
124
+			t.Fatalf("Event received it different than expected")
125
+		}
126
+	}
127
+
128
+}
129
+
92 130
 func TestGetImagesJSON(t *testing.T) {
93 131
 	runtime := mkRuntime(t)
94 132
 	defer nuke(runtime)
... ...
@@ -38,7 +38,7 @@ func setTimeout(t *testing.T, msg string, d time.Duration, f func()) {
38 38
 		f()
39 39
 		c <- false
40 40
 	}()
41
-	if <-c {
41
+	if <-c && msg != "" {
42 42
 		t.Fatal(msg)
43 43
 	}
44 44
 }