forked from hoanhan101/ultimate-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_test.go
50 lines (43 loc) · 1.41 KB
/
table_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
// ----------
// Table test
// ----------
// Set up a data structure of input to expected output.
// This way we don't need a separate function for each one of these. We just have 1 test function.
// As we go along, we just add more to the table.
package main
import (
"net/http"
"testing"
)
// TestTable validates the http Get function can download content and
// handles different status conditions properly.
func TestTable(t *testing.T) {
// This table is a slice of anonymous struct type. It is the URL we are gonna call and
// statusCode are what we expect.
tests := []struct {
url string
statusCode int
}{
{"https://www.goinggo.net/post/index.xml", http.StatusOK},
{"http://rss.cnn.com/rss/cnn_topstorie.rss", http.StatusNotFound},
}
t.Log("Given the need to test downloading different content.")
{
for i, tt := range tests {
t.Logf("\tTest: %d\tWhen checking %q for status code %d", i, tt.url, tt.statusCode)
{
resp, err := http.Get(tt.url)
if err != nil {
t.Fatalf("\t%s\tShould be able to make the Get call : %v", failed, err)
}
t.Logf("\t%s\tShould be able to make the Get call.", succeed)
defer resp.Body.Close()
if resp.StatusCode == tt.statusCode {
t.Logf("\t%s\tShould receive a %d status code.", succeed, tt.statusCode)
} else {
t.Errorf("\t%s\tShould receive a %d status code : %v", failed, tt.statusCode, resp.StatusCode)
}
}
}
}
}