forked from SVyatkin/predix-go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredix-go_test.go
76 lines (63 loc) · 1.78 KB
/
predix-go_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestWelcome(t *testing.T) {
// Create GET request
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
// We create a ResponseRecorder
rr := httptest.NewRecorder()
handler := http.HandlerFunc(welcome)
// Run welcome function using created request and get result in rr - ResponseRecorder.
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body is what we expect.
expected := `<html>
<body>
<br>
<div>Welcome to Golang Predix Microservice Template</div>
<br>
<a href='/env' target='_blank'>Environment variables</a>
</body>
</html>`
// Check response
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
}
func TestEnv(t *testing.T) {
// Create GET request
req, err := http.NewRequest("GET", "/env", nil)
if err != nil {
t.Fatal(err)
}
// We create a ResponseRecorder
rr := httptest.NewRecorder()
handler := http.HandlerFunc(env)
// Run env function using created request and get result in rr - ResponseRecorder.
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body is what we expect.
expected := `<div class="envs">`
// Check response
s := rr.Body.String()[:18]
if s != expected {
//if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
}