-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
52 lines (45 loc) · 971 Bytes
/
main_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
package main
import (
"io/ioutil"
"net/http/httptest"
"testing"
)
func TestHelloUser(t *testing.T) {
testCases := []struct {
name string
query string
expected string
}{
{
name: "Without name",
query: "",
expected: "Hello User!",
},
{
name: "With name",
query: "?name=Malma",
expected: "Hello Malma!",
},
{
name: "With invalid name 1",
query: "?name",
expected: "Hello User!",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
request := httptest.NewRequest("GET", "/"+testCase.query, nil)
recorder := httptest.NewRecorder()
index(recorder, request)
result := recorder.Result()
defer result.Body.Close()
body, err := ioutil.ReadAll(result.Body)
if err != nil {
t.Fatal(err)
}
if string(body) != testCase.expected {
t.Fatalf("Expected \"%v\" but got \"%v\" instead", testCase.expected, string(body))
}
})
}
}