-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_test.go
75 lines (70 loc) · 1.78 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestInvalidDomain(t *testing.T) {
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "localhost/service/ip?domain=dsfkfs", nil)
if err != nil {
t.Fatalf(err.Error())
}
ipHandler(w, r)
s := `{"Error":"Invalid domain address."}`
msg := strings.TrimSpace(w.Body.String())
if msg != s {
t.Fatalf("Return body (%s) does not match expected (%s)\n", msg, s)
}
}
func TestEmptyDomain(t *testing.T) {
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "localhost/service/ip", nil)
if err != nil {
t.Fatalf(err.Error())
}
ipHandler(w, r)
s := `{"Error":"Empty domain parameter"}`
msg := strings.TrimSpace(w.Body.String())
if msg != s {
t.Fatalf("Return body (%s) does not match expected (%s)\n", msg, s)
}
}
func TestNodomain(t *testing.T) {
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "localhost/service/ip?domain=", nil)
if err != nil {
t.Fatalf(err.Error())
}
ipHandler(w, r)
s := `{"Error":"Empty domain parameter"}`
msg := strings.TrimSpace(w.Body.String())
if msg != s {
t.Fatalf("Return body (%s) does not match expected (%s)\n", msg, s)
}
}
func TestIndex(t *testing.T) {
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "localhost/service/ip?domain=localhost", nil)
if err != nil {
t.Fatalf(err.Error())
}
ipHandler(w, r)
s := `{"IPs":["127.0.0.1"]}`
msg := strings.TrimSpace(w.Body.String())
if msg != s {
t.Fatalf("Return body (%s) does not match expected (%s)\n", msg, s)
}
}
func TestWeb(t *testing.T) {
s := httptest.NewServer(createBaseHandler())
defer s.Close()
resp, err := http.Get(s.URL)
if err != nil {
t.Fatal("Failed to query index", err)
}
if resp.StatusCode != 200 {
t.Fatal("StatusCode is not 200")
}
}