tangled
alpha
login
or
join now
nandi.latha.org
/
godemo
0
fork
atom
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
Add unit tests
nandi
3 weeks ago
12b75fa5
59ea6023
0/1
build.yml
failed
10s
+56
1 changed file
expand all
collapse all
unified
split
main_test.go
+56
main_test.go
···
1
1
+
package main
2
2
+
3
3
+
import (
4
4
+
"fmt"
5
5
+
"net/http"
6
6
+
"net/http/httptest"
7
7
+
"testing"
8
8
+
)
9
9
+
10
10
+
func TestRootHandler(t *testing.T) {
11
11
+
req, err := http.NewRequest("GET", "/", nil)
12
12
+
if err != nil {
13
13
+
t.Fatal(err)
14
14
+
}
15
15
+
16
16
+
rr := httptest.NewRecorder()
17
17
+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
18
18
+
fmt.Fprintf(w, "Hello from Tangled! 🌀\n")
19
19
+
fmt.Fprintf(w, "This is a simple Go demo app.\n")
20
20
+
})
21
21
+
22
22
+
handler.ServeHTTP(rr, req)
23
23
+
24
24
+
if status := rr.Code; status != http.StatusOK {
25
25
+
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
26
26
+
}
27
27
+
28
28
+
expected := "Hello from Tangled! 🌀\nThis is a simple Go demo app.\n"
29
29
+
if rr.Body.String() != expected {
30
30
+
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
31
31
+
}
32
32
+
}
33
33
+
34
34
+
func TestHealthHandler(t *testing.T) {
35
35
+
req, err := http.NewRequest("GET", "/health", nil)
36
36
+
if err != nil {
37
37
+
t.Fatal(err)
38
38
+
}
39
39
+
40
40
+
rr := httptest.NewRecorder()
41
41
+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
42
42
+
w.WriteHeader(http.StatusOK)
43
43
+
fmt.Fprintf(w, "OK\n")
44
44
+
})
45
45
+
46
46
+
handler.ServeHTTP(rr, req)
47
47
+
48
48
+
if status := rr.Code; status != http.StatusOK {
49
49
+
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
50
50
+
}
51
51
+
52
52
+
expected := "OK\n"
53
53
+
if rr.Body.String() != expected {
54
54
+
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
55
55
+
}
56
56
+
}